【Docker】Dockerfile 最佳實踐-CMD
參考教程:https://docs.docker.com/develop/develop-images/dockerfile_best-practices/
環境
- virtual box 6.1
- centos 7.8
- docker 19.03
CMD
The CMD
instruction should be used to run the software contained in your image, along with any arguments. CMD
should almost always be used in the form of CMD ["executable", "param1", "param2"…]
CMD ["apache2","-DFOREGROUND"]
. Indeed, this form of the instruction is recommended for any service-based image.
應該使用 CMD
指令來執行映象中包含的軟體以及所有引數。CMD
幾乎應始終以 CMD ["executable", "param1", "param2"…]
的形式使用。因此,如果映象用於服務,例如 Apache 和 Rails,則將執行諸如 CMD ["apache2","-DFOREGROUND"]
In most other cases, CMD
should be given an interactive shell, such as bash, python and perl. For example, CMD ["perl", "-de0"]
, CMD ["python"]
, or CMD ["php", "-a"]
. Using this form means that when you execute something like docker run -it python
, you’ll get dropped into a usable shell, ready to go. CMD
CMD ["param", "param"]
in conjunction with ENTRYPOINT
, unless you and your expected users are already quite familiar with how ENTRYPOINT
works.
在大多數其他情況下,應該給 CMD
一個互動式外殼,例如 bash,python 和 perl。例如,CMD ["perl", "-de0"]
,CMD ["python"]
或 CMD ["php", "-a"]
。使用此種格式意味著執行 docker run -it python
之類的操作時,您將進入可用的 shell 中,隨時可以使用。除非您和您的預期使用者已經非常熟悉 ENTRYPOINT
的工作原理,否則 CMD
很少以 CMD ["param", "param"]
的形式與 ENTRYPOINT
結合使用。
總結
介紹了 Dockerfile 的 CMD 指令的最佳實踐。