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:
- What exactly
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. - The equivalent shell form is
CMD bash
orCMD exec bash
? - If removing that completely, what is the equivalent
docker exec
command line?
I cannot find any reliable answer despite my thorough search...
CodePudding user response:
When you create an image
FROM scratch
, among other things, it has an empty default command, as if you had writtenENTRYPOINT [] CMD []
So
CMD
there gives some default command if youdocker run ubuntu
. Most images will in fact override thatCMD
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 ash -c
wrapper around whatever command you give it, so these two would be equivalentCMD 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 theENTRYPOINT
orCMD
from the image at all.