標籤 : dockergit
[TOC]github
本文主要對Docker commands和Dockerfile的相關知識進行整理docker
官網傳送門:shell
首先,固然是配置命令自動補全,只須要把一個文件用curl下載copy到特定路徑便可,具體操做參考Command-line Completionbash
其實docker有很完備的命令幫助提示,對哪一個指令不清楚,只須要在後面加--help
就能看到幫助說明。例如:app
docker --help
能夠看到全部可執行的命令。run
命令,則輸入docker run --help
又能看到run
的相關幫助了。經常使用命令:curl
docker images
docker run [OPTIONS] IMAGE [COMMAND] [ARG...] //i.e. docker run image docker run -it image /bin/bash
經常使用的一些參數:ide
--rm
:container退出後自動刪除post
-i
和-t
經常一塊兒用,-it
:以超級管理員權限打開一個命令行窗口網站
-d
: 後臺運行container
--name
:給container命名
查看當前container
docker ps -a
docker rm $(docker ps -a -q)
docker exec -it ${container_id} /bin/bash
docker inspect ${container_id}
另外, 在以上指令中,容器名和容器的container_id都是能夠使用的,若是用戶沒有指定容器名,docker會默認給每一個容器分配一個比較友好的隨機名稱,像fervent_perlman,high_galileo等等
官網傳送門:
感受文檔裏說了很全了,這裏稍微提幾個容易困惑的點
1.exec form vs shell form
在CMD
和ENTRYPOINT
都涉及到着兩種形式(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 ,以爲這二者特別類似,對這二者有什麼區別和聯繫仍是有些困惑,閱讀下面這篇文章:
簡而言之,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(preferred)
參考連接