exec /entrypoint.sh: exec 格式错误:/usr/bin/env: 'bash\r': 没有那个文件或目录
exec /entrypoint.sh: exec format error: /usr/bin/env: 'bash\r': No such file or directory
ID: docker/entrypoint-script-dos-line-endings
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Docker 20.10.22 | active | — | — | — |
| Docker 24.0.4 | active | — | — | — |
| Docker 25.0.0 | active | — | — | — |
根因分析
入口点脚本具有 Windows 风格的 CRLF 换行符(\r\n)而不是 Unix 风格的 LF(\n),导致 shebang 解释器尝试执行 'bash\r'(包含回车符)时失败。
English
The entrypoint script has Windows-style CRLF line endings (\r\n) instead of Unix-style LF (\n), causing the shebang interpreter to fail when it tries to execute 'bash\r' (with a carriage return character).
官方文档
https://docs.docker.com/engine/reference/builder/#entrypoint解决方案
-
在主机上使用 'dos2unix' 将入口点脚本转换为 Unix 换行符:'dos2unix entrypoint.sh',或在构建镜像前使用 'sed -i 's/\r$//' entrypoint.sh'。
-
在 Dockerfile 中,在 COPY 之后添加 RUN 命令修复换行符:'RUN sed -i 's/\r$//' /entrypoint.sh'。
-
配置 Git 在检出时自动转换换行符:在主机上设置 'git config core.autocrlf input',然后重新克隆或检出仓库。
无效尝试
常见但无效的做法:
-
90% 失败
The issue is not the shell type but the carriage return character in the shebang line; changing the shebang does not remove the \r character.
-
95% 失败
The base image does not affect the line endings of the script; the problem is in the script file itself, which is copied from the host.
-
100% 失败
The container fails to start due to the entrypoint error, so 'docker exec' cannot be used because the container is not running.