Dockerfile with .sh not working when running it inside Jenkins

I’m trying to run a shell script file inside a docker container. It’s working as expected when testing it with the command line but not when running it inside Jenkins (Windows Buildserver).

This is my Dockerfile:

FROM ubuntu:latest

RUN apt-get update \
 && apt-get dist-upgrade -y \
 && apt-get install -y \
    build-essential libgtest-dev \
 && apt-get clean

WORKDIR /src

COPY . .

#working when using CLI and Jenkinsfile
RUN echo "test" 

#working when using CLI and Jenkinsfile
RUN ls 

#working when using CLI but NOT with Jenkinsfile
RUN ./build.sh 

CMD ["./run_tests.sh"]

And this is how I run the dockerfile:

docker-build.sh

#!/bin/sh
set -x

#stop and rm old container if any
docker container stop build-fw
docker container rm build-fw

docker build -t build-fw .

docker run --name build-fw -d -it build-fw

docker logs build-fw

docker container stop build-fw
docker container rm build-fw

In Jenkins, I simply created a Freestyle project with the “Execute Shell” build step, where I simply ran the “docker-build.sh” script:

Execute Shell

./docker-build.sh

I got the following error output:

#12 0.586 /bin/sh: 1: ./build.sh: not found
#12 ERROR: executor failed running [/bin/sh -c ./build.sh]: exit code: 127
------
 > [8/8] RUN build.sh:
------
executor failed running [/bin/sh -c ./build.sh]: exit code: 127

The error in Jenkins states, that ./build.sh was not found, although RUN ls shows that build.sh exists.
However this works as expected when using CLI.

How can I fix this issue ?

That is probably not ./build.sh which is not found but you have an invalid character in the shell script. If you created the file on windows or pulled a repository from Git on Windows which changes line breaks, you can get an error message like this. Make sure the line endings are correct, linux compatible line endings. LF and not CR nor CRLF. In other words: \n and not \r nor \r\n.

Here is an example to reproduce the issue in a container:

docker run --rm -it bash
bash-5.1# printf '#!/bin/sh\r\n' > /build.sh
bash-5.1# ./build.sh
bash: ./build.sh: /bin/sh^M: bad interpreter: No such file or directory
bash-5.1# /bin/sh -c ./build.sh
/bin/sh: ./build.sh: not found
1 Like