Home  >  Q&A  >  body text

Dockerfile里指定执行命令用ENTRYPOING和用CMD有何不同?

如题,我一般是用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"]
ringa_leeringa_lee2711 days ago760

reply all(2)I'll reply

  • ringa_lee

    ringa_lee2017-04-21 10:57:46

    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:

    ...
    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 ultimately equivalent to running echo foo, so the final printed result is:

    foo
    

    And ENTRYPOINT will pass everything after the container name as parameters to the specified command (the command will not be overwritten), such as:

    ...
    ENTRYPOINT ["echo"]
    

    Then run

    docker run CONTAINER_NAME echo foo
    

    Then the echo foo after CONTAINER_NAME are passed as parameters to the echo command specified in ENTRYPOING, so it is equivalent to executing

    echo "echo foo"
    

    The final printed result is:

    echo foo
    

    In addition, in the Dockerfile, the parameters specified by ENTRYPOINT are earlier than the parameters specified when running docker run, such as:

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

    Execute

    docker run CONTAINER_NAME bar
    

    Equivalent to executing:

    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 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:

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

    Then execute:

    docker run CONTAINER_NAME bar #注意没有echo
    

    Then, it is equivalent to executing echo foo bar, and the final result is

    foo bar
    

    I 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

    reply
    0
  • PHPz

    PHPz2017-04-21 10:57:46

    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

    reply
    0
  • Cancelreply