You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows环境下Docker容器内容列表命令执行失败问题排查

问题描述

我有一个正在运行的容器,可通过docker exec -it mycontainer bash进入并正常操作,但执行以下命令时均失败:

  • 执行命令:
    docker exec mycontainer bash ls /
    
    报错:/usr/bin/ls: /usr/bin/ls: cannot execute binary file
  • 执行命令:
    docker exec mycontainer ls /
    
    报错:cannot access 'C:/Program Files/Git/': No such file or directory

更新需求

希望通过单条命令测试容器内scripts/populate-args文件是否存在,执行命令:

docker exec mycontainer bash test -f scripts/populate-args && echo 'file exists'

报错:/usr/bin/test: /usr/bin/test: cannot execute binary file

备注:已拥有该文件读写权限,进入容器后可正常操作。

解决办法

针对bash ls/bash test类报错

你写错了bash的调用方式——bash执行外部命令必须用-c参数指定完整命令字符串,否则bash会把ls、test当成要加载的脚本文件,而非内置/外部命令,导致“无法执行二进制文件”的错误。

修正后的命令:

  • 列出容器根目录内容:
    docker exec mycontainer bash -c "ls /"
    
  • 测试文件存在性并输出提示:
    docker exec mycontainer bash -c "test -f scripts/populate-args && echo 'file exists'"
    

针对路径被解析为C:/Program Files/Git/的报错

该问题出在Git Bash环境下,Git Bash会自动将Linux风格的路径/转换为Windows路径。解决方式二选一:

  1. 用双引号包裹路径:
docker exec mycontainer ls "/"
  1. 临时禁用Git Bash的路径转换:
MSYS_NO_PATHCONV=1 docker exec mycontainer ls /

内容的提问来源于stack exchange,提问作者Inspiraller

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 02:05:01