15 個管理容器的 Docker 命令 [列出、停止、啟動、刪除等]
已發表: 2021-08-17Docker 是構建和運行容器化應用程序的流行工具。 它可用於多個平台,並用作 Kubernetes 中的後端容器技術之一。
在 Docker 中,您可以構建自己的鏡像以將應用程序作為容器運行,也可以從 Docker 存儲庫中提取和使用數千個公共鏡像並在項目中使用它們。 鏡像準備好後,您可以使用這些鏡像啟動容器。 容器是 Docker 映像的運行實例。
作為管理 Docker 主機/容器的系統管理員,管理 Docker 容器是最重要的方面之一。
在本文中,我們將重點介紹使用 docker 命令管理容器。
運行命令
docker run
命令用於通過指定Image ID
或Repository
和/或Tag
名稱從鏡像運行容器。
$ docker run {image}
例子:
$ docker run nginx
上面的命令在docker 主機上運行<span class="NormalTextRun SpellingErrorV2 SCXW251451022 BCX0">nginx</span>
應用程序的實例,如果它已經存在的話。 如果它在 Docker 主機上不存在,它會輸出到 docker hub(默認情況下)並拉下圖像。 但這是唯一的第一次。 對於後續時間,相同的圖像被重複使用。
如果要運行特定版本的映像,請指定其版本,用冒號分隔。 這稱為Tag
。 如果您不指定任何標籤,docker 將默認將其視為最新的。
此外,如果您想在後台以分離模式運行容器,以便在 Docker 啟動容器後返回提示符,請使用-d
標誌。
例子:
$ docker run nginx Unable to find image 'nginx:latest' locally latest: Pulling from library/nginx 33847f680f63: Pull complete dbb907d5159d: Pull complete 8a268f30c42a: Pull complete b10cf527a02d: Pull complete c90b090c213b: Pull complete 1f41b2f2bf94: Pull complete Digest: sha256:8f335768880da6baf72b70c701002b45f4932acae8d574dedfddaf967fc3ac90 Status: Downloaded newer image for nginx:latest /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh /docker-entrypoint.sh: Configuration complete; ready for start up 2021/08/15 12:13:23 [notice] 1#1: using the "epoll" event method 2021/08/15 12:13:23 [notice] 1#1: nginx/1.21.1 2021/08/15 12:13:23 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6) 2021/08/15 12:13:23 [notice] 1#1: OS: Linux 5.8.0-1039-azure 2021/08/15 12:13:23 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576 2021/08/15 12:13:23 [notice] 1#1: start worker processes 2021/08/15 12:13:23 [notice] 1#1: start worker process 33 2021/08/15 12:13:23 [notice] 1#1: start worker process 34
ps 命令
docker ps
命令列出所有正在運行的容器以及一些關於它們的基本信息。 如容器 ID、鏡像名稱、容器創建時間、當前狀態和容器名稱。 每個容器都有一個隨機名稱(如果沒有明確指定)和 ID。
例子:
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 133f5e0267a5 nginx "/docker-entrypoint.…" 10 seconds ago Up 10 seconds 80/tcp jolly_elion
要一次列出所有正在運行和未運行/退出的容器,您可以使用:
$ docker ps -a
例子:
$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fcec129f0eb4 nginx "/docker-entrypoint.…" 46 hours ago Exited (0) 46 hours ago interesting_ishizaka 6e8b1e441aa6 hello-world "/hello" 2 days ago Exited (0) 2 days ago keen_shirley
ls 命令
與ps
命令一樣, ls
也可用於列出容器。 -a
標誌可用於列出所有容器(不僅僅是正在運行的容器)。
$ docker container ls
例子:
$ docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 15796e91c30b redis "docker-entrypoint.s…" 2 seconds ago Up 1 second 6379/tcp flamboyant_neumann 904390b65d45 nginx "/docker-entrypoint.…" 14 minutes ago Up 14 minutes 80/tcp nginx_new $
停止命令
docker stop
命令用於停止正在運行的容器。 在這裡,我們需要將容器名稱或 ID 與此一起放置。
$ docker stop {container-id}
成功時,它將返回 docker 名稱或 ID。
例子:
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 133f5e0267a5 nginx "/docker-entrypoint.…" 50 seconds ago Up 49 seconds 80/tcp jolly_elion
這將返回可用於停止容器的CONTAINER ID
。
$ docker stop 133f5 133f5
對於這個示例和接下來的示例,請注意您不需要指定CONTAINER ID
的完整值。 它將接受部分,這使得它在其他正在運行的容器中是獨一無二的,因為 Docker 知道要停止哪個容器。
rm 命令
docker rm
命令刪除一個停止或退出的容器。
$ docker rm {CONTAINER NAME or ID}
例子:
$ docker rm 133f5 133f5 $
執行命令
我們可以使用exec
命令進入正在運行的容器中。 這對於調試正在運行的容器或在容器中做一些事情很有用。
$ docker exec –it {container} {command}
例子:
假設您要在名為unruffled_meninsky
的容器中以交互模式啟動bash
shell(假設圖像具有可用的 Bash,您也可以使用其他可用的 shell),請使用:

$ docker exec –it unruffled_meninsky /bin/bash
這應該讓您進入bash
shell 上的容器。 這裡標誌-i
代表交互模式, -t
代表終端。 如果您只想執行一個或多個命令並退出容器,您可以使用:
$ docker exec unruffled_meninsky cat /etc/hosts 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 172.17.0.2 cd2eed4acf34
日誌命令
如果容器以分離模式啟動,並且我們想查看它的日誌,我們可以使用logs
命令查看它的日誌:
$ docker logs {CONTAINER NAME or ID}
例子:
$ docker logs 7da6dcebaf9c /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh /docker-entrypoint.sh: Configuration complete; ready for start up 2021/08/15 12:14:09 [notice] 1#1: using the "epoll" event method 2021/08/15 12:14:09 [notice] 1#1: nginx/1.21.1 2021/08/15 12:14:09 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6) 2021/08/15 12:14:09 [notice] 1#1: OS: Linux 5.8.0-1039-azure 2021/08/15 12:14:09 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576 2021/08/15 12:14:09 [notice] 1#1: start worker processes 2021/08/15 12:14:09 [notice] 1#1: start worker process 31 2021/08/15 12:14:09 [notice] 1#1: start worker process 32 $
cp 命令
要在容器和 localhost 文件系統之間複製文件,可以使用cp
命令。
$ docker container cp {CONTAINER NAME or ID:SRC_PATH} {DEST_PATH}|-
例子:
$ docker container cp quirky_cray:/etc/nginx/nginx.conf nginx.conf.bkp
導出命令
Docker 容器命令提供了將容器的文件系統導出為 TAR 文件的選項。
$ docker container export {CONTAINER NAME or ID}
檢查命令
我們可以使用inspect
命令檢查有關容器的詳細信息,如下所示:
$ docker inspect {CONTAINER NAME or ID}
或者
$ docker container inspect {CONTAINER NAME or ID}
殺死命令
可以使用帶有可選--signal
或-s
標誌的kill
命令殺死正在運行的容器。 可以指定多個容器一次性殺死它們。
$ docker kill {CONTAINER NAME or ID} [--signal VAL]
例子:
$ docker kill cd9005a0b5d2 -s 9 cd9005a0b5d2 $
統計命令
要顯示容器資源使用情況的實時流,您可以使用stats
命令:
$ docker container stats {CONTAINER NAME or ID}
例子:
$ docker container stats thirsty_volhard CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 904390b65d45 thirsty_volhard 0.00% 3.406MiB / 7.775GiB 0.04% 1.02kB / 0B 0B / 8.19kB 3
頂部命令
就像 Linux 中的top
命令一樣,我們可以將它與 Docker 一起使用來獲取正在運行的進程列表。
$ docker container top {CONTAINER NAME or ID}
例子:
$ docker container top thirsty_volhard UID PID PPID C STIME TTY TIME CMD root 2603 2582 0 12:34 ? 00:00:00 nginx: master process nginx -g daemon off; systemd+ 2659 2603 0 12:34 ? 00:00:00 nginx: worker process systemd+ 2660 2603 0 12:34 ? 00:00:00 nginx: worker process $
重命名命令
要重命名現有容器,請使用rename
命令。
$ docker container rename {OLD CONTAINER NAME} {NEW CONTAINER NAME}
例子:
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 904390b65d45 nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 80/tcp nginx_container $ docker container rename nginx_container nginx_new $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 904390b65d45 nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 80/tcp nginx_new $
diff 命令
我們可以使用diff
命令檢查容器文件系統上文件或目錄的更改。
$ docker container diff {CONTAINER NAME or ID}
例子:
$ docker container diff nginx_new C /var C /var/cache C /var/cache/nginx A /var/cache/nginx/uwsgi_temp A /var/cache/nginx/client_temp A /var/cache/nginx/fastcgi_temp A /var/cache/nginx/proxy_temp A /var/cache/nginx/scgi_temp C /etc C /etc/nginx C /etc/nginx/conf.d C /etc/nginx/conf.d/default.conf C /run A /run/nginx.pid $
概括
總而言之,Doker 提供了一套廣泛的命令來管理從創建到銷毀的容器。 我們已經在本文中介紹了一些重要的命令及其用法,這應該可以讓您對管理 docker 容器有一個很好的了解。
接下來,找出一些學習 DevOps 的資源。