1. 程式人生 > 實用技巧 >【Docker】Dockerfile 之 ENTRYPOINT(四)

【Docker】Dockerfile 之 ENTRYPOINT(四)

參考教程:https://docs.docker.com/engine/reference/builder/

環境

  1. virtual box 6.1
  2. centos 7.8
  3. docker 19.03

ENTRYPOINT 和 CMD 的互動

Both CMD and ENTRYPOINT instructions define what command gets executed when running a container. There are few rules that describe their co-operation.
CMDENTRYPOINT 指令均定義了執行容器時執行的命令。有少量的規則描述他們的合作。

  1. Dockerfile should specify at least one of CMD or ENTRYPOINT commands.

  2. Dockerfile 應該至少指定 CMDENTRYPOINT 命令之一。

  3. ENTRYPOINT should be defined when using the container as an executable.

  4. 使用容器作為可執行檔案時,應定義 ENTRYPOINT

  5. 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.

  6. 應該使用 CMD 作為定義 ENTRYPOINT 命令或在容器中執行命令的預設引數的方式。

  7. CMD will be overridden when running the container with alternative arguments.

  8. 當使用其他引數執行容器時,CMD 將被覆蓋。

The table below shows what command is executed for different ENTRYPOINT / CMD combinations:
下表顯示了針對不同的 ENTRYPOINT/CMD 組合執行的命令:

No ENTRYPOINT ENTRYPOINT exec_entry p1_entry ENTRYPOINT [“exec_entry”, “p1_entry”]
No CMD error, not allowed /bin/sh -c exec_entry p1_entry exec_entry p1_entry
CMD [“exec_cmd”, “p1_cmd”] exec_cmd p1_cmd /bin/sh -c exec_entry p1_entry exec_entry p1_entry exec_cmd p1_cmd
CMD [“p1_cmd”, “p2_cmd”] p1_cmd p2_cmd /bin/sh -c exec_entry p1_entry exec_entry p1_entry p1_cmd p2_cmd
CMD exec_cmd p1_cmd /bin/sh -c exec_cmd p1_cmd /bin/sh -c exec_entry p1_entry exec_entry p1_entry /bin/sh -c exec_cmd p1_cmd

Note

If CMD is defined from the base image, setting ENTRYPOINT will reset CMD to an empty value. In this scenario, CMD must be defined in the current image to have a value.

注意

如果從基礎映象定義了 CMD,則設定 ENTRYPOINT 會將 CMD 重置為空值。在這種情況下,必須在當前映象中定義CMD 以具有值。

總結

介紹了 Dockerfile 中 ENTRYPOINT 指令和 CMD 指令的互動。