Reputation: 5910
This is the Dockerfile of Docker Ubuntu official image:
FROM scratch
ADD ubuntu-focal-oci-amd64-root.tar.gz /
CMD ["bash"]
Can you please help me clarifying the following:
CMD["bash"]
does? I can see no difference by removing it (e.g. by adding in my local Dockerfile: CMD["echo", "hello"]
) when starting a container.CMD bash
or CMD exec bash
?docker exec
command line?I cannot find any reliable answer despite my thorough search...
Upvotes: 3
Views: 15590
Reputation: 158647
When you create an image FROM scratch
, among other things, it has an empty default command, as if you had written
ENTRYPOINT []
CMD []
So CMD
there gives some default command if you docker run ubuntu
. Most images will in fact override that CMD
with something to run the program built into the derived image.
Since that CMD
uses JSON-array syntax, there isn't directly a shell-syntax equivalent of it. The shell syntax automatically inserts a sh -c
wrapper around whatever command you give it, so these two would be equivalent
CMD bash
CMD ["/bin/sh", "-c", "bash"]
There would not be an equivalent docker exec
command. docker exec
is a debugging command that's not normally part of the "build and run an image" workflow. In particular, it does not use the ENTRYPOINT
or CMD
from the image at all.
Upvotes: 5