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

前端 未结 16 1754
自闭症患者
自闭症患者 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条回答
  •  旧时难觅i
    2020-11-21 23:21

    Difference between CMD and ENTRYPOINT by intuition:

    • ENTRYPOINT: command to run when container starts.
    • CMD: command to run when container starts or arguments to ENTRYPOINT if specified.

    Yes, it's mixing up.

    You can override any of them when running docker run.

    Difference between CMD and ENTRYPOINT by example:

    docker run -it --rm yourcontainer /bin/bash            <-- /bin/bash overrides CMD
                                                           <-- /bin/bash does not override ENTRYPOINT
    docker run -it --rm --entrypoint ls yourcontainer      <-- overrides ENTRYPOINT with ls
    docker run -it --rm --entrypoint ls yourcontainer  -la  <-- overrides ENTRYPOINT with ls and overrides CMD with -la
    

    More on difference between CMD and ENTRYPOINT:

    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]. The args at the end of docker run [args] are provided as arguments to ENTRYPOINT. In this way we can create a container which is like a normal binary such as ls.

    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.

提交回复
热议问题