1. 程式人生 > 實用技巧 >【Docker】Dockerfile 最佳實踐-CMD

【Docker】Dockerfile 最佳實踐-CMD

參考教程:https://docs.docker.com/develop/develop-images/dockerfile_best-practices/

環境

  1. virtual box 6.1
  2. centos 7.8
  3. 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"…]

. Thus, if the image is for a service, such as Apache and Rails, you would run something like 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

should rarely be used in the manner of 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 指令的最佳實踐。