Search code examples
bashdockerdocker-composedockerfile

Dockerfile RUN command throws an error but on different machines it works fine


In my Dockerfile there's a RUN command like this:

FROM nvidia/cuda:12.1.1-cudnn8-runtime-ubuntu20.04
...
RUN ./my_script.sh

The first line in the script is #!/bin/bash. My colleagues are able to build the image with no problems, their OS is Linux Ubuntu 22.04. I use WSL2 (Ubuntu 22.04) on Windows 11 to build the image with docker compose build and I get the following error:

ERROR [application 10/11] RUN ./my_script.sh 
0.426 /bin/sh: 1: ./my_script.sh: not found

I have two questions, because I don't understand why this error happens:

  1. why RUN command behaves differently on different machines? The container OS is the same and the RUN command runs inside the container, right?
  2. why the error reports /bin/sh even if in the script /bin/bash is declared?

Solution

  • This frequently happens when Git on Windows is configured to change line endings to CR-LF, instead of the Linux LF standard. That changes the interpreter for the script from /bin/bash to /bin/bash\r. Adjust the Git configuration to preserve the line endings from the code to avoid this issue.