What is the difference between CMD and ENTRYPOINT in a Dockerfile?

前端 未结 16 1928
自闭症患者
自闭症患者 2020-11-21 22:31

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 -

16条回答
  •  春和景丽
    2020-11-21 23:14

    Comments on EntryPoint function in code

    // ENTRYPOINT /usr/sbin/nginx.

    // Set the entrypoint (which defaults to sh -c) to /usr/sbin/nginx.

    // Will accept the CMD as the arguments to /usr/sbin/nginx.

    Another reference from documents

    You can use the exec form of ENTRYPOINT to set fairly stable default commands and arguments and then use CMD to set additional defaults that are more likely to be changed.

    Example:

    FROM ubuntu:14.04.3
    ENTRYPOINT ["/bin/ping"]
    CMD ["localhost", "-c", "2"]
    

    Build: sudo docker build -t ent_cmd .

    CMD arguments are easy to override.
    
    NO argument (sudo docker -it ent_cmd)                :  ping localhost 
    argument    (sudo docker run -it ent_cmd google.com) :  ping google.com
    

    .

    To override EntryPoint argument, you need to supply entrypoint
    sudo docker run -it --entrypoint="/bin/bash" ent_cmdd
    

    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.

提交回复
热议问题