Home > front end >  Dockerfile can't run shell scripts inside Jenkins
Dockerfile can't run shell scripts inside Jenkins

Time:09-17

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.

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 states, that ./build.sh was not found, although RUN ls shows that build.sh exists. Why is it the case?

CodePudding user response:

build.sh was not found because it had an invalid character in the shell. I created the file on windows (which has different line endings than linux), that is why I'm getting the error message. So I just made 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

So to fix this, I ran this command sed -i 's/\r//' build.sh which removes \r from build.sh if present.

  • Related