In Dockerfiles there are two commands that look similar to me: CMD
and ENTRYPOINT
. But I guess that there is a (subtle?) difference between them - otherwise it would not make any sense to have two commands for the very same thing.
The documentation states for CMD
The main purpose of a CMD is to provide defaults for an executing container.
and for ENTRYPOINT
:
An ENTRYPOINT helps you to configure a container that you can run as an executable.
So, what's the difference between those two commands?
Comments on EntryPoint function in code
Another reference from documents
Example:
Build: sudo docker build -t ent_cmd .
.
p.s: In presence of EntryPoint, CMD will hold arguments to fed to EntryPoint. In absense of EntryPoint, CMD will be the command which will be run.
In a nutshell:
If you need more details or would like to see difference on example, there is a blog post that comprehensively compare CMD and ENTRYPOINT with lots of examples - http://goinbigdata.com/docker-run-vs-cmd-vs-entrypoint/
The accepted answer is fabulous in explaining the history. I find this table explain it very well from official doc on 'how CMD and ENTRYPOINT interact':
CMD:
CMD ["executable","param1","param2"]
:["executable","param1","param2"]
is the first process.CMD command param1 param2
:/bin/sh -c CMD command param1 param2
is the first process.CMD command param1 param2
is forked from the first process.CMD ["param1","param2"]
: This form is used to provide default arguments forENTRYPOINT
.ENTRYPOINT (The following list does not consider the case where CMD and ENTRYPOINT are used together):
ENTRYPOINT ["executable", "param1", "param2"]
:["executable", "param1", "param2"]
is the first process.ENTRYPOINT command param1 param2
:/bin/sh -c command param1 param2
is the first process.command param1 param2
is forked from the first process.As creack said, CMD was developed first. Then ENTRYPOINT was developed for more customization. Since they are not designed together, there are some functionality overlaps between CMD and ENTRYPOINT, which often confuse people.
Yes, that is a good question. I don't understand it fully yet, but:
I understand that
ENTRYPOINT
is the binary that is being executed. You can overide entrypoint by --entrypoint="".CMD is the default argument to container. Without entrypoint, default argument is command that is executed. With entrypoint, cmd is passed to entrypoint as argument. You can emulate a command with entrypoint.
So, main advantage is that with entrypoint you can pass arguments (cmd) to your container. To accomplish this, you need to use both:
and
then you can use:
Difference between CMD and ENTRYPOINT by intuition:
Yes, it's mixing up.
You can override any of them when running docker run.
Difference between CMD and ENTRYPOINT by example:
More on difference between
CMD
andENTRYPOINT
:Argument to
docker run
such as /bin/bash overrides any CMD command we wrote in Dockerfile.ENTRYPOINT cannot be overriden at run time with normal commands such as
docker run [args]
. Theargs
at the end ofdocker run [args]
are provided as arguments to ENTRYPOINT. In this way we can create acontainer
which is like a normal binary such asls
.So CMD can act as default parameters to ENTRYPOINT and then we can override the CMD args from [args].
ENTRYPOINT can be overriden with
--entrypoint
.