> docker container ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ef2929e5ceb0 httpd "httpd-foreground"8 seconds ago Up 6 seconds 80/tcp iThome
停止容器:
1 2 3
停止容器: > docker container stop iThome iThome
再次查看運作中的容器:
1 2
> docker container ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
沒有東西?沒錯!還記得 docker container ls 與 docker container ps 都是查詢「運作中」的容器嗎。我們剛剛停止了容器,所以必須加上 -a 才找得到剛剛停下來的容器。
1 2 3
> docker container ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ef2929e5ceb0 httpd "httpd-foreground"4 minutes ago Exited (0) 30 seconds ago iThome
我們可以看到,停止下來的容器狀態是 Exited 退出。
跟容器說再見
如果我們想要刪除容器的話,要先讓容器停止運作後,才能夠刪除 rm。
我們可以先啟動剛剛的容器, iThome ,然後嘗試刪除:
1 2 3 4 5 6
>docker container start iThome iThome
>docker container rm iThome Error response from daemon: You cannot remove a running container ef2929e5ceb076f0c836df3c3bbb814450377392430529997e29505813fc6a93. Stop the container before attempting removal or force remove
跑出來的訊息很直接的告訴我們,想要刪除容器就要先停止容器,不然就是要強制刪除。
先停止容器再刪除
所以如果我們想刪除,基本上就是要先停止,然後再刪除:
1 2 3 4 5 6 7 8
> docker container stop iThome iThome
> docker container rm iThome iThome
> docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
> docker container prune WARNING! This will remove all stopped containers. Are you sure you want to continue? [y/N]
docker 向我們確認是不是真的要執行?輸入 y 就可以執行了。
如果加上 -f 或 --force 就能跳過詢問的步驟直接執行刪除:
1 2 3 4 5 6 7 8 9 10 11 12
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a8cef9d77a9b httpd "httpd-foreground"6 seconds ago Created pedantic_meitner 264645d5d5cd httpd "httpd-foreground"8 seconds ago Created pensive_edison 6ac5d76c0fc1 httpd "httpd-foreground"4 minutes ago Exited (0) 14 seconds ago iThome