What is the difference between using ENTRYPOING to execute commands and CMD in Dockerfile?

  docker, question

For example, I usually use CMD to specify, for example:

FROM thelanddownunder
 MAINTAINER ProgrammingLife
 
 CMD ["apt-get install htop"]

But look at some Dockerfile also use ENTRYPOINT to specify, is to change the above CMD to ENTRYPOINT, as if also specify some commands:

FROM thelanddownunder
 MAINTAINER ProgrammingLife
 
 ENTRYPOINT ["apt-get install htop"]

Is there any difference between the two methods? In addition, there are also uses RUN to specify the command, syntax and the above two are not the same, such as this:

FROM thelanddownunder
 MAINTAINER ProgrammingLife
 
 RUN apt-get install htop
 ENTRYPOINT ["apt-get install vim"]

The timing is different.

RUN is run during Build, prior to CMD and ENTRYPOINT. CMD or ENTRYPOINT will be RUN after Build is completed and run is completed.

ENTRYPOINT differs from CMD in the way parameters are passed when docker run is executed. The command specified by CMD can be overwritten by the command passed by docker run. For example, if CMD is used to specify:

...
 CMD ["echo"]

Then run

docker run CONTAINER_NAME echo foo

Then the echo specified in CMD will be overwritten by the newly specified echo, so it is equivalent to running in the end.echo foo, so the final printed result is:

foo

ENTRYPOINT takes everything after the container name as a parameter and passes it to the command it specifies (without overwriting the command), such as:

...
 ENTRYPOINT ["echo"]

Then run

docker run CONTAINER_NAME echo foo

The following after CONTAINER_NAMEecho fooAll passed as parameters to the echo command specified in ENTRYPOING, so it is equivalent to execution

echo "echo foo"

The final printed result is:

echo foo

In addition, in the Dockerfile, ENTRYPOINT specifies parameters before those specified when running docker run, such as:

...
 ENTRYPOINT ["echo", "foo"]

carry out

docker run CONTAINER_NAME bar

This is equivalent to implementing:

echo foo bar

The printed result is:

foo bar

Only one ENTRYPOINT can be specified in the Dockerfile. if many are specified, only the last one is valid.

When executing the docker run command, you can also add the -ENTRYPOINT parameter, which will continue to pass the specified parameter to entrypoint, for example:

...
 ENTRYPOINT ["echo","foo"]

Then execute:

Docker run CONTAINER_NAME bar # note no echo

Then, it is equivalent to executionecho foo barThe end result is

foo bar

I am heredockboard.orgOn the translation of an article《15 Docker Tips in 15 Minutes》There are differences between RUN, CMD and ENTRYPOINT, which you can refer to.

In addition, there is a Docker Quicktips series, in which there is an article about ENTRYPIONT. You can look at it and connect it here:
http://www.tech-d.net/2014/01/27/docker-quicktip-1-entrypoint/

We will also add the translation of this series of articles to it soon.dockboard.orgYes, please pay close attention.

In addition, here are the instructions for entrypoint in the official documents:http://docs.docker.io/en/latest/reference/builder/#entrypoint