如题,我一般是用CMD来指定的,比如:
FROM thelanddownunder
MAINTAINER ProgrammingLife
CMD ["apt-get install htop"]
但是看一些Dockerfile里也有用ENTRYPOINT来指定的,就是把上面的CMD换成ENTRYPOINT,后面好像也是指定一些命令的:
FROM thelanddownunder
MAINTAINER ProgrammingLife
ENTRYPOINT ["apt-get install htop"]
请问这两种方法有什么不一样的吗?另外,还有用RUN来指定命令的,语法和上面两种又不太一样,比如这样:
FROM thelanddownunder
MAINTAINER ProgrammingLife
RUN apt-get install htop
ENTRYPOINT ["apt-get install vim"]
The running timing is different.
RUN is run during Build, before CMD and ENTRYPOINT. After Build is completed and RUN is completed, run CMD or ENTRYPOINT again.
The difference between ENTRYPOINT and CMD lies in the way parameters are passed when executing docker run. The command specified by CMD can be overridden by the command passed by docker run. For example, if specified with CMD:
Then run
Then the echo specified in CMD will be overwritten by the newly specified echo, so it is ultimately equivalent to running
echo foo
, so the final printed result is:And ENTRYPOINT will pass everything after the container name as parameters to the specified command (the command will not be overwritten), such as:
Then run
Then the
echo foo
after CONTAINER_NAME are passed as parameters to the echo command specified in ENTRYPOING, so it is equivalent to executingThe final printed result is:
In addition, in the Dockerfile, the parameters specified by ENTRYPOINT are earlier than the parameters specified when running docker run, such as:
Execute
Equivalent to executing:
The printed result is:
Only one ENTRYPOINT can be specified in the Dockerfile. If many are specified, only the last one will be valid.
When executing the docker run command, you can also add the -entrypoint parameter, which will continue to pass the specified parameters to ENTRYPOINT, for example:
Then execute:
Then, it is equivalent to executing
echo foo bar
, and the final result isI translated an article "15 Docker Tips in 15 Minutes" on dockboard.org, which talks about the differences between RUN, CMD and ENTRYPOINT. You can refer to it.
There is also a Docker Quicktips series, and there is an article in it that also talks about ENTRYPIONT. You can take a look, the link is here:
http://www.tech-d.net/2014/01/27/docker-quicktip-1- entrypoint/
We will add the translations of this series of articles to dockboard.org soon, so stay tuned.
In addition, here is the description of entrypoint in the official documentation: http://docs.docker.io/en/latest/reference/builder/#entrypoint
In the Dockerfile, if ENTRYPOINT and CMD exist, then CMD is the parameter of ENTRYPOINT. If there is no ENTRYPOINT, then CMD is the default execution instruction