參考教程:https://docs.docker.com/engine/reference/builder/
環境
- virtual box 6.1
- centos 7.8
- docker 19.03
CMD
The CMD
instruction has three forms:
CMD
指令具有三種形式:
-
CMD ["executable","param1","param2"]
(exec form, this is the preferred form) -
CMD ["param1","param2"]
(as default parameters to ENTRYPOINT) -
CMD command param1 param2
(shell form) -
CMD ["executable","param1","param2"]
(exec 形式,這是首選形式) -
CMD [“ param1”,“ param2”]
(作為 ENTRYPOINT 的默認參數) -
CMD command param1 param2
(shell 形式)
There can only be one CMD
instruction in a Dockerfile
. If you list more than one CMD
then only the last CMD
will take effect.
在 Dockerfile
中只能有一個 CMD 指令。如果您列出多個 CMD
,那么只有最后一個 CMD
才會生效。
The main purpose of a CMD
is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT
instruction as well.
CMD
的主要目的是為執行中的容器提供默認值。這些默認值可以包含可執行文件,也可以省略可執行文件,在這種情況下,您還必須指定一條 ENTRYPOINT
指令。
If CMD
is used to provide default arguments for the ENTRYPOINT
instruction, both the CMD
and ENTRYPOINT
instructions should be specified with the JSON array format.
如果使用 CMD
為 ENTRYPOINT
指令提供默認參數,則 CMD
和 ENTRYPOINT
指令均應使用 JSON 數組格式指定。
Note
The exec form is parsed as a JSON array, which means that you must use double-quotes (") around words not single-quotes (').
注意
exec 形式被解析為 JSON 數組,這意味着您必須使用雙引號(")而非單引號(')包圍單詞。
Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, CMD [ "echo", "$HOME" ]
will not do variable substitution on $HOME
. If you want shell processing then either use the shell form or execute a shell directly, for example: CMD [ "sh", "-c", "echo $HOME" ]
. When using the exec form and executing a shell directly, as in the case for the shell form, it is the shell that is doing the environment variable expansion, not docker.
與 shell 格式不同,exec 格式不調用命令外殼程序。這意味着不會進行常規的外殼處理。例如,CMD [ "echo", "$HOME" ]
不會在 $HOME
上進行變量替換。如果要進行 shell 處理,則可以使用 shell 形式或直接執行 shell,例如: CMD [ "sh", "-c", "echo $HOME" ]
。當使用 exec 格式並直接執行 shell 時,是由 shell 進行環境變量擴展,而不是 docker。
When used in the shell or exec formats, the CMD
instruction sets the command to be executed when running the image.
當以 shell 或 exec 格式使用時, CMD
指令設置運行鏡像時要執行的命令。
If you use the shell form of the CMD
, then the <command>
will execute in /bin/sh -c
:
如果使用 CMD
的 shell 形式,則 <command>
將在 /bin/sh -c
中執行:
FROM ubuntu
CMD echo "This is a test." | wc -
If you want to run your <command>
without a shell then you must express the command as a JSON array and give the full path to the executable. This array form is the preferred format of CMD
. Any additional parameters must be individually expressed as strings in the array:
如果要在沒有外殼的情況下運行您的 <command>
,則必須將該命令表示為 JSON 數組,並提供可執行文件的完整路徑。 數組形式是 CMD
的首選格式。 任何其他參數必須在數組中分別表示為字符串:
FROM ubuntu
CMD ["/usr/bin/wc","--help"]
If you would like your container to run the same executable every time, then you should consider using ENTRYPOINT
in combination with CMD
. See ENTRYPOINT.
如果您希望容器每次都運行相同的可執行文件,則應考慮將 ENTRYPOINT
與 CMD
結合使用。請參閱ENTRYPOINT。
If the user specifies arguments to docker run
then they will override the default specified in CMD
.
如果用戶為 docker run
指定了參數,則它們將覆蓋 CMD
中指定的默認值。
Note
Do not confuse
RUN
withCMD
.RUN
actually runs a command and commits the result;CMD
does not execute anything at build time, but specifies the intended command for the image.
注意
不要將
RUN
和CMD
混淆。RUN
實際上運行命令並提交結果。CMD
在生成時不執行任何操作,但是指定了鏡像的預期命令。
示例
Dockerfile
FROM busybox
CMD echo jiangbo
結果
[root@master env]# docker build -t jiangbo:0.0.1 .
Sending build context to Docker daemon 3.584kB
Step 1/2 : FROM busybox
---> dc3bacd8b5ea
Step 2/2 : CMD echo jiangbo
---> Running in 8eb219d5669f
Removing intermediate container 8eb219d5669f
---> c1f4d3207e37
Successfully built c1f4d3207e37
Successfully tagged jiangbo:0.0.1
[root@master env]# docker run -it jiangbo:0.0.1
jiangbo
總結
介紹了 Dockerfile 中 RUN 指令的使用。