BadCandy
BadCandy

Reputation: 55

Dockerfile's COPY command does not work normally

I am creating a script, copying it to a container and trying to run. However, it seems that the COPY command does not work properly.

Below is the contents of the Dockerfile.

FROM busybox:latest
COPY random.sh /bin/random.sh
ENTRYPOINT /bin/random.sh

And below is the contents of the script file to be copied.

#!/bin/bash
trap "exit" SIGINT
mkdir /var/htdocs

SET=$(seq 0 999999)

for i in $SET

do
        echo "Running $RANDOM" > /var/htdocs/index.html
        sleep 10
done

And in the directory where the Dockerfile belongs, the script to be copied exists as follows.

-rw-r--r-- 1 pp pp   76 Oct  4 09:19 Dockerfile
-rwxr-xr-x 1 pp pp  154 Oct  4 09:33 random.sh*

Upload the image to Dockerhub by running the following command in the directory where the Dockerfile is located.

docker build -t dockerhubid/repo .
docker push dockerhubid/repo

And start the container by using the command below.

docker start dockerhubid/repo

However, the container does not start with the error below. Why can't I find the copied script?

/bin/bash: line 1: /bin/random.sh: not found

Upvotes: 0

Views: 670

Answers (1)

concision
concision

Reputation: 6387

This is not an issue with Docker's COPY command, but rather the script interpreter shebang at the start of your script. The error is not claiming /bin/random.sh does not exist, but that it cannot find the script interpreter /bin/bash.

The Busybox image does not have a /bin/bash interpreter, only /bin/sh. You either need to install a Bash interpreter if your script is only bash compatible, or change the first line shebang to #!/bin/sh. From a cursory glance, I would think this sample script is /bin/sh compatible.

Another important note when building against Windows hosts is that oftentimes the file uses CRLF (i.e. \r\n) line endings, which is not compatible with Linux shebangs (e.g. it will resolve to /bin/sh\r). File endings need to be set correctly to LF (i.e. \n) for the script to run.

Upvotes: 1

Related Questions