Docker CMD can't find files installed with Dockerfile?

Docker version 20.10.17, build 100c701

My Dockerfile

FROM ubuntu:22.04

COPY /entrypoint.sh .

COPY /script.sh /home/

RUN chmod +x /home/script.sh

VOLUME ["/home/volume/bla"]

EXPOSE 3677 3678

ENTRYPOINT ["/entrypoint.sh"]

CMD ["sh /home/script.sh"]

entrypoint.sh

#!/bin/bash
set -e
echo $@
exec "$@"

I build the project
docker build /home/project/docker/ -t root/project

Then I run it docker run --user root -it --name project-container root/project

It logs $@ then exec $@

sh /home/script.sh
/entrypoint.sh: line 25: /sh /home/script.sh: No such file or directory

Why does it not find the script? this is driving me nuts.

Also tried: CMD ["/usr/bin/sh /home/script.sh"] in the Dockerfile but I get the same result

Hi

Have you seen this:

The CMD instruction has three forms:

CMD ["executable","param1","param2"] (exec form, this is the preferred form)
CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
CMD command param1 param2 (shell form)

I belive you’re mixing 1 & 3

1 Like

Because it does not exist. The answer is what @terpz pointed out. When you use the exec syntax which makes CMD to be the argument of the entrypoint, every entry that you put between quotation marks are considered to be one file path not a command an its arguments.

The correct way is:

CMD ["sh", "/home/script.sh"]

You really want to use the exec form, because the other would not be the argument of the entrypoint and could work differently or not work at all.

And you can probably use the CMD without the “sh” part:

CMD ["/home/script.sh"]

Furthermore, your ENTRYPOINT and CMD combination would result in the command /entrypoints.sh "sh /home/script.sh", as CMD will be the argument to ENTRYPOINT.

If you declare CMD as CMD [/"home/script.sh"], the container will execute /entrypoint.sh /home/script.sh.

Note: make sure both scripts are executable. For /entrypoint.sh you depend on the +x permissions being set on the host, as you don’t set in your Dockerfile explicitly, like you did for /home/script.sh.