【Docker】Dockerfile 之 ENTRYPOINT(四)
參考教程:https://docs.docker.com/engine/reference/builder/
環境
- virtual box 6.1
- centos 7.8
- 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.
CMD
和 ENTRYPOINT
指令均定義了執行容器時執行的命令。有少量的規則描述他們的合作。
-
Dockerfile should specify at least one of
CMD
orENTRYPOINT
commands. -
Dockerfile 應該至少指定
CMD
或ENTRYPOINT
命令之一。 -
ENTRYPOINT
should be defined when using the container as an executable. -
使用容器作為可執行檔案時,應定義
ENTRYPOINT
。 -
CMD
should be used as a way of defining default arguments for anENTRYPOINT
command or for executing an ad-hoc command in a container. -
應該使用
CMD
作為定義ENTRYPOINT
命令或在容器中執行命令的預設引數的方式。 -
CMD
will be overridden when running the container with alternative arguments. -
當使用其他引數執行容器時,
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, settingENTRYPOINT
will resetCMD
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 指令的互動。