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

前端 未结 16 1741
自闭症患者
自闭症患者 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:10

    According to docker docs,

    Both CMD and ENTRYPOINT instructions define what command gets executed when running a container. There are few rules that describe their co-operation.

    1. Dockerfile should specify at least one of CMD or ENTRYPOINT commands.
    2. ENTRYPOINT should be defined when using the container as an executable.
    3. CMD should be used as a way of defining default arguments for an ENTRYPOINT command or for executing an ad-hoc command in a container.
    4. CMD will be overridden when running the container with alternative arguments.

    The tables below shows what command is executed for different ENTRYPOINT / CMD combinations:

    -- No ENTRYPOINT

    ╔════════════════════════════╦═════════════════════════════╗
    ║ No CMD                     ║ error, not allowed          ║
    ╟────────────────────────────╫─────────────────────────────╢
    ║ CMD ["exec_cmd", "p1_cmd"] ║ exec_cmd p1_cmd             ║
    ╟────────────────────────────╫─────────────────────────────╢
    ║ CMD ["p1_cmd", "p2_cmd"]   ║ p1_cmd p2_cmd               ║
    ╟────────────────────────────╫─────────────────────────────╢
    ║ CMD exec_cmd p1_cmd        ║ /bin/sh -c exec_cmd p1_cmd  ║
    ╚════════════════════════════╩═════════════════════════════╝
    

    -- ENTRYPOINT exec_entry p1_entry

    ╔════════════════════════════╦══════════════════════════════════╗
    ║ No CMD                     ║ /bin/sh -c exec_entry p1_entry   ║
    ╟────────────────────────────╫──────────────────────────────────╢
    ║ CMD ["exec_cmd", "p1_cmd"] ║ /bin/sh -c exec_entry p1_entry   ║
    ╟────────────────────────────╫──────────────────────────────────╢
    ║ CMD ["p1_cmd", "p2_cmd"]   ║ /bin/sh -c exec_entry p1_entry   ║
    ╟────────────────────────────╫──────────────────────────────────╢
    ║ CMD exec_cmd p1_cmd        ║ /bin/sh -c exec_entry p1_entry   ║
    ╚════════════════════════════╩══════════════════════════════════╝
    

    -- ENTRYPOINT ["exec_entry", "p1_entry"]

    ╔════════════════════════════╦═════════════════════════════════════════════════╗
    ║ No CMD                     ║ exec_entry p1_entry                             ║
    ╟────────────────────────────╫─────────────────────────────────────────────────╢
    ║ CMD ["exec_cmd", "p1_cmd"] ║ exec_entry p1_entry exec_cmd p1_cmd             ║
    ╟────────────────────────────╫─────────────────────────────────────────────────╢
    ║ CMD ["p1_cmd", "p2_cmd"]   ║ exec_entry p1_entry p1_cmd p2_cmd               ║
    ╟────────────────────────────╫─────────────────────────────────────────────────╢
    ║ CMD exec_cmd p1_cmd        ║ exec_entry p1_entry /bin/sh -c exec_cmd p1_cmd  ║
    ╚════════════════════════════╩═════════════════════════════════════════════════╝
    

提交回复
热议问题