I have seen some images that based on the inspect
have both CMD
and ENTRYPOINT
set.
What is the idea of this? What use cases are served when both are set?
Asked
Active
Viewed 1,955 times
1

Jim
- 3,845
- 3
- 22
- 47
1 Answers
3
per https://docs.docker.com/engine/reference/builder/#cmd
CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
this means that CMD can be used as a list of parametres to the entrypoint.
UPD 1
the idea here is that you can specify the entrypoint (binary to run at start) and then have CMD parameters to this entrypoint which can be overridden.
UPD 2
Here is an example:
cat Dockerfile
FROM busybox
ENTRYPOINT ["ls"]
CMD ["etc"]
now that if you build the image and run it without any arguments, it will product the list with the content of etc
folder
$ docker run lister
group
hostname
hosts
localtime
mtab
network
passwd
resolv.conf
shadow
if you do specify the arguments though, you can override the CMD and list the content of a different folder
$ docker run lister var
spool
www
so the executable stays the same in both cases, while the CMD in the Dockerfile is defining the default argument to this binary. Then we can override when we run the container.

jabbson
- 4,390
- 1
- 13
- 23
-
I don't understand. I could override the entrypoint anyway. Why define both then? – Jim Oct 31 '21 at 17:49
-
you could, or you could not override it, keep it and only work with arguments – jabbson Oct 31 '21 at 17:51
-
Updated the answer with an example – jabbson Oct 31 '21 at 18:04
-
Ah I see now. So it is only when the `CMD` does not have an executable defined right? – Jim Oct 31 '21 at 18:28
-
correct, this is only for when the executable comes from the entrypoint and cmd defines the default arguments to this executable. – jabbson Oct 31 '21 at 18:31
-
It wasn't easy to understand with the `cmd` and `other cmd` pseudo examples. Could you update with some actual commands so that it is clearer? I'll accept the answer – Jim Oct 31 '21 at 18:35
-
Hope this new example is a bit cleaner. – jabbson Oct 31 '21 at 18:43