hotmeatballsoup
hotmeatballsoup

Reputation: 625

Docker can't find script living in home dir

I have the following Dockerfile:

FROM ubuntu:21.04

COPY keep-alive.sh $HOME/keep-alive.sh

CMD ["$HOME/keep-alive.sh"]

Yes I know its really useless but I am learning.

When I run this:

$ docker run -d --name linux-worker myorg/linux-worker
71cfc9ff7072688d1758f2ac98a8293ed2bcf77bf68f980da20237c9961aca6c
docker: Error response from daemon: OCI runtime create failed: container_linux.go:344: starting container process caused "exec: \"$HOME/keep-alive.sh\": stat $HOME/keep-alive.sh: no such file or directory": unknown.

All I want the container to do is start up and run $HOME/keep-alive.sh. Can anyone spot where I'm going awry?

Upvotes: 1

Views: 930

Answers (1)

cam
cam

Reputation: 5218

The $HOME environment variable will not be set when running the COPY instruction in your Dockerfile so your script was likely placed at /keep-alive.sh instead of /root/keep-alive.sh where your CMD instruction expects it to be.

Check the logs of your build and you'll likely see that line executed like:

=> [3/3] COPY keep-alive.sh /keep-alive.sh

instead of:

=> [3/3] COPY keep-alive.sh /root/keep-alive.sh

To fix this, you can explicitly set an environment variable to use in this command:

FROM ubuntu:21.04

ENV DIR /root

COPY keep-alive.sh $DIR/keep-alive.sh

CMD ["/bin/bash", "-c", "$DIR/keep-alive.sh"]

Another change that had to be made was to specify that you want the script to be run in a shell so it will expand environment variables. See this answer for more details about this issue.

Alternatively, if you didn't want to use environment variables at all, you could change that line to this if you know the script's path:

CMD [ "/root/keep-alive.sh" ]

Upvotes: 2

Related Questions