docker runtime_error ai_generated true

exec /entrypoint.sh: exec format error: /usr/bin/env: 'bash\r': No such file or directory

ID: docker/entrypoint-script-dos-line-endings

Also available as: JSON · Markdown · 中文
95%Fix Rate
90%Confidence
1Evidence
2023-08-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Docker 20.10.22 active
Docker 24.0.4 active
Docker 25.0.0 active

Root Cause

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).

generic

中文

入口点脚本具有 Windows 风格的 CRLF 换行符(\r\n)而不是 Unix 风格的 LF(\n),导致 shebang 解释器尝试执行 'bash\r'(包含回车符)时失败。

Official Documentation

https://docs.docker.com/engine/reference/builder/#entrypoint

Workarounds

  1. 95% success Convert the entrypoint script to Unix line endings using 'dos2unix' on the host: 'dos2unix entrypoint.sh' before building the image, or use 'sed -i 's/\r$//' entrypoint.sh'.
    Convert the entrypoint script to Unix line endings using 'dos2unix' on the host: 'dos2unix entrypoint.sh' before building the image, or use 'sed -i 's/\r$//' entrypoint.sh'.
  2. 90% success In the Dockerfile, add a RUN command to fix line endings after COPY: 'RUN sed -i 's/\r$//' /entrypoint.sh'.
    In the Dockerfile, add a RUN command to fix line endings after COPY: 'RUN sed -i 's/\r$//' /entrypoint.sh'.
  3. 85% success Configure Git to automatically convert line endings on checkout by setting 'git config core.autocrlf input' on the host, then re-clone or checkout the repository.
    Configure Git to automatically convert line endings on checkout by setting 'git config core.autocrlf input' on the host, then re-clone or checkout the repository.

中文步骤

  1. 在主机上使用 'dos2unix' 将入口点脚本转换为 Unix 换行符:'dos2unix entrypoint.sh',或在构建镜像前使用 'sed -i 's/\r$//' entrypoint.sh'。
  2. 在 Dockerfile 中,在 COPY 之后添加 RUN 命令修复换行符:'RUN sed -i 's/\r$//' /entrypoint.sh'。
  3. 配置 Git 在检出时自动转换换行符:在主机上设置 'git config core.autocrlf input',然后重新克隆或检出仓库。

Dead Ends

Common approaches that don't work:

  1. 90% fail

    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.

  2. 95% fail

    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.

  3. 100% fail

    The container fails to start due to the entrypoint error, so 'docker exec' cannot be used because the container is not running.