Docker commands和Dockerfile

Docker commands和Dockerfile

標籤 : docker



本文主要對Docker commands和Dockerfile的相關知識進行整理

Docker commands

官網傳送門:

首先,當然是配置命令自動補全,只需要把一個文件用curl下載copy到特定路徑即可,具體操作參考Command-line Completion

其實docker有很完備的命令幫助提示,對哪個指令不清楚,只需要在後面加--help就能看到幫助說明。例如:

  • 輸入docker --help可以看到所有可執行的命令。
  • 隨便挑一個,比如run命令,則輸入docker run --help又能看到run的相關幫助了。

常用命令:

  • 查看本地images:
docker images
  • 運行image
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
//i.e.
docker run image
docker run -it image /bin/bash

常用的一些參數:

  • --rm:container退出後自動刪除
  • -i-t常常一起用,-it:以超級管理員權限打開一個命令行窗口
  • -d: 後臺運行container
  • --name:給container命名

  • 查看當前container

docker ps -a
  • 刪除所有狀態的container
docker rm $(docker ps -a -q)
  • 通過另外的tty查看已經運行的容器
docker exec -it ${container_id} /bin/bash
  • 查看容器的信息
docker inspect ${container_id}

另外, 在以上指令中,容器名和容器的container_id都是可以使用的,如果用戶沒有指定容器名,docker會默認給每個容器分配一個比較友好的隨機名稱,像fervent_perlman,high_galileo等等

Dockerfile

官網傳送門:

感覺文檔裏說了很全了,這裏稍微提幾個容易困惑的點

1.exec form vs shell form

CMDENTRYPOINT都涉及到着兩種形式(CMD多一種完全作爲參數的形式),例如:

  • CMD ["executable","param1","param2"](exec形式,推薦)
  • CMD command param1 param2 (shell形式)

至於兩種形式的區別,官方的幾點說明挺詳細的,主要就是變量替換,腳本環境等方面有差別:

  • Note: 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.
  • Note: The exec form is parsed as a JSON array, which means that you must use double-quotes (“) around words not single-quotes (‘).
  • Note: 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" ].

2.ENTRYPOINT vs CMD

讀完官方的Understand how CMD and ENTRYPOINT interact
,覺得這兩者特別相似,對這兩者有什麼區別和聯繫還是有些困惑,閱讀下面這篇文章:

Dockerfile: ENTRYPOINT vs CMD

簡而言之,ENTRYPOINT更像一個寫死的可執行指令,CMD更像默認的一個可選項。

一個image只做一個單一的用途,就像一個可執行的命令時,建議使用ENTRYPOINT,把CMD作爲默認參數(第三種形式CMD ["param1","param2"] (as default parameters to ENTRYPOINT))。因爲一般而言,ENTRYPOINT是不被覆蓋的(除非在run時顯式使用–entrypoit),而CMD是defaults的選項,從前文的run命令格式docker run [OPTIONS] IMAGE [COMMAND] [ARG...]可知,用戶可以在運行images時輸入自己的COMMAND來覆蓋默認的CMD。

3.ADD vs COPY

這兩個好像都是把東西從host拷貝到docker的container裏,官方比較如下:

Although ADD and COPY are functionally similar, generally speaking, COPY is preferred. That’s because it’s more transparent than ADD. COPY only supports the basic copying of local files into the container, while ADD has some features (like local-only tar extraction and remote URL support) that are not immediately obvious. Consequently, the best use for ADD is local tar file auto-extraction into the image, as in ADD rootfs.tar.xz /.

簡單來說,主要就兩點區別:

  • COPY只能把本地文件拷貝到container裏;ADD還支持從遠程拷貝(remote URL support)
  • ADD可以自動解壓本地壓縮文件

官方建議用COPY(preferred)

參考鏈接

Reference - ADD or COPY
Stackoverflow - Docker COPY vs ADD


作者@brianway更多文章:個人網站 | CSDN | oschina

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章