這是一個建立於
的文章,其中的資訊可能已經有所發展或是發生改變。
序
在上一篇基於Docker進行Golang開發文章中對Docker進行了安裝,並對如何用Docker進行Golang開發進行了描述,本文再對Docker命令進行講解。
查看Docker命令
➜ tonny@tonny-pc ~ docker -hUsage: docker [OPTIONS] COMMAND [arg...] docker [ --help | -v | --version ]A self-sufficient runtime for containers.Options: --config=~/.docker Location of client config files -D, --debug Enable debug mode -H, --host=[] Daemon socket(s) to connect to -h, --help Print usage -l, --log-level=info Set the logging level --tls Use TLS; implied by --tlsverify --tlscacert=~/.docker/ca.pem Trust certs signed only by this CA --tlscert=~/.docker/cert.pem Path to TLS certificate file --tlskey=~/.docker/key.pem Path to TLS key file --tlsverify Use TLS and verify the remote -v, --version Print version information and quitCommands: attach Attach to a running container build Build an image from a Dockerfile commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes on a container's filesystem events Get real time events from the server exec Run a command in a running container export Export a container's filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on a container, image or task kill Kill one or more running container load Load an image from a tar archive or STDIN login Log in to a Docker registry. logout Log out from a Docker registry. logs Fetch the logs of a container network Manage Docker networks node Manage Docker Swarm nodes pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart a container rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive (streamed to STDOUT by default) search Search the Docker Hub for images service Manage Docker services start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop one or more running containers swarm Manage Docker Swarm tag Tag an image into a repository top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers version Show the Docker version information volume Manage Docker volumes wait Block until a container stops, then print its exit codeRun 'docker COMMAND --help' for more information on a command.
attach Attach to a running container
--將終端依附到容器上
- 運行一個互動型容器
[root@localhost ~]# docker run -i -t centos /bin/bash [root@f0a02b473067 /]#
- 在另一個視窗上查看該容器的狀態
[root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d4a75f165ce6 centos "/bin/bash" 5 seconds ago Up 5 seconds cranky_mahavira
- 退出第一步中啟動並執行容器
[root@d4a75f165ce6 /]# exit exit
- 查看該容器的狀態
``` bash
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d4a75f165ce6 centos "/bin/bash" 2 minutes ago Exited (0) 23 seconds ago cranky_mahavira
可見此時容器的狀態是Exited,那麼,如何再次運行這個容器呢?可以使用docker start命令。
- 再次運行該容器
[root@localhost ~]# docker start cranky_mahavira cranky_mahavira
- 再次查看該容器的狀態
[root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d4a75f165ce6 centos "/bin/bash" 6 minutes ago Up 29 seconds cranky_mahavira
因為該容器是互動型的,但此刻我們發現沒有具體的終端可以與之互動,這時可使用attach命令。
- 通過attach命令進行互動
[root@localhost ~]# docker attach cranky_mahavira [root@d4a75f165ce6 /]#
build Build an image **from a Dockerfile **
--通過Dockerfile建立鏡像
commit Create a **new image from a container's changes**
--通過容器建立本地鏡像
注意:如果是要push到docker hub中,注意產生鏡像的命名。
[root@localhost ~]# docker commit centos_v1 centos:v168ad49c999496cff25fdda58f0521530a143d3884e61bce7ada09bdc22337638 [root@localhost ~]# docker push centos:v1 You cannot push a "root" repository. Please rename your repository to <user>/<repo> (ex: <user>/centos)
用centos:v1就不行,因為它push到docker hub中時,是推送到相應使用者下,必須指定使用者名稱。譬如我的使用者名稱是ivictor,則新產生的本地鏡像命名為: docker push victor/centos:v1,其中v1是tag,可不寫,預設是latest。
cp Copy file/folders from a container to a HOSTDIR or to STDOUT
--在宿主機和容器之間相互COPY檔案
cp的用法如下:
Usage: docker cp [OPTIONS] CONTAINER:PATH LOCALPATH|- docker cp [OPTIONS] LOCALPATH|- CONTAINER:PATH
create Create a new container
--建立一個新的容器,注意,此時,容器的status只是Created
diff Inspect changes on a container's filesystem
--查看容器內發生改變的檔案,以我的mysql容器為例
events Get real time events from the server --即時輸出Docker伺服器端的事件,包括容器的建立,啟動,關閉等。
exec Run a command in a running container
--用於容器啟動之後,執行其它的任務
通過exec命令可以建立兩種任務:後台型任務和互動型任務
#後台型任務:[root@localhost ~]#docker exec -d cc touch 123 #其中cc是容器名 #互動型任務: [root@localhost ~]# docker exec -i -t cc /bin/bash root@1e5bb46d801b:/# ls 123 bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
export Export a container's filesystem as a tar archive
--將容器的檔案系統打包成tar檔案
有兩種方式:
[root@localhost ~]# docker export -o mysqldb1.tar mysqldb [root@localhost ~]# docker export mysqldb > mysqldb.tar
history Show the history of an image
--顯示鏡像製作的過程,相當於dockfile
images List images
--列出原生所有鏡像
import Import the contents **from a tarball to create a filesystem image **
--根據tar檔案的內容建立一個鏡像,與之前的export命令相對應
info Display system-wide information
--查看docker的系統資訊
inspect Return low-level information on a container or image
--用於查看容器的配置資訊,包含容器名、環境變數、運行命令、主機配置、網路設定和資料卷配置等。
kill Kill a running container
--強制終止容器
關於stop和kill的區別,docker stop命令給容器中的進程發送SIGTERM訊號,預設行為是會導致容器退出,當然, 容器內程式可以捕獲該訊號並自行處理,例如可以選擇忽略。而docker kill則是給容器的進程發送SIGKILL訊號,該訊號將會使容器必然退出。
load Load an image from a tar archive or STDIN
--與下面的save命令相對應,將下面sava命令打包的鏡像通過load命令匯入
login Register or log in to a Docker registry --登入到自己的Docker register,需有Docker Hub的註冊帳號
logout Logout from a Docker registry
--退出登入
logs Fetch the logs of a container
--用於查看容器的日誌,它將輸出到標準輸出的資料作為日誌輸出到docker logs命令的終端上。常用於後台型容器
pause Pause all processes within a container
--暫停容器內的所有進程, 此時,通過docker stats可以觀察到此時的資源使用方式是固定不變的, 通過docker logs -f也觀察不到日誌的進一步輸出。
port List port mappings or a specific mapping for the CONTAINER
--輸出容器連接埠與宿主機連接埠的映射情況
ps List containers
--列出所有容器,其中docker ps用於查看正在啟動並執行容器,ps -a則用於查看所有容器。
pull Pull an image or a repository from a registry
--從docker hub中下載鏡像
push Push an image or a repository to a registry
--將本地的鏡像上傳到docker hub中 前提是你要先用docker login登入上,不然會報錯誤。
rename Rename a container
--更改容器的名字
restart Restart a running container
--重啟容器
rm Remove one or more containers
--刪除容器
注意,不可以刪除一個運行中的容器,必須先用docker stop或docker kill使其停止。 當然可以強制移除,必須加-f參數 如果要一次性刪除所有容器,可使用 docker rm -f docker ps -a -q,其中,-q指的是只列出容器的ID。
rmi Remove one or more images
--刪除鏡像
run Run a command in a new container
--讓建立的容器立刻進入運行狀態,該命令等同於docker create建立容器後再使用docker start啟動容器
save Save an image(s) to a tar archive
--將鏡像打包,與上面的load命令相對應
search Search the Docker Hub for images
--從Docker Hub中搜尋鏡像
start Start one or more stopped containers
--啟動容器
stats Display a live stream of container(s) resource usage statistics
--動態顯示容器的資源消耗情況,包括:CPU、記憶體、網路I/O
stop Stop a running container
--停止一個啟動並執行容器
tag Tag an image into a repository
--對鏡像進行重新命名
top Display the running processes of a container
--查看容器中正在啟動並執行進程
unpause Unpause all processes within a container
--恢複容器內暫停進程,與pause參數相對應。
version Show the Docker version information
--查看docker的版本
wait Block until a container stops, then print its exit code
--捕捉容器停止時的退出碼
執行此命令後,該命令會“hang”在當前終端,直到容器停止,此時,會列印出容器的退出碼。