Docker 移除 Containers、Images、Volumes 與 Networks 的方法

相信大家用久了 Docker 都會有一些問題是裡面會有很多以前開過的 Containers 與 Images 等等的東西,久了其實會佔位子,在一開始要移除可能會遇到問題,這篇就來教大家怎麼快速的把這些東西移除掉

移除所有未使用的 Docker 項目

$ docker system prune

如果要不出現提示可以使用 -f 或是 –force

WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all dangling images
        - all build cache
Are you sure you want to continue? [y/N]

如果要移除未使用者的 Volumes 可以加入 –volumes

WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all volumes not used by at least one container
        - all dangling images
        - all build cache
Are you sure you want to continue? [y/N] y

移除 Docker container

除非使用 –rm 啟動 Container,不然停止的時候不會移除 Container

移除一個或是多個 container

docker container ls -a

輸出會看到

CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS                      PORTS               NAMES
cc3f2ff51cab        centos                  "/bin/bash"              2 months ago        Created                                         competent_nightingale
cd20b396a061        solita/ubuntu-systemd   "/bin/bash -c 'exec …"   2 months ago        Exited (137) 2 months ago                       systemd
fb62432cf3c1        ubuntu                  "/bin/bash"              3 months ago        Exited (130) 3 months ago                       jolly_mirzakhani

有了 CONTAINER ID 要移除的 Container,就可以使用 docker container rm 移除方法如:

docker container rm cc3f2ff51cab cd20b396a061

如果出現以下的訊息就是代表這個 Container 有在用就沒辦法移除

Error response from daemon: You cannot remove a running container fc983ebf4771d42a8bd0029df061cb74dc12cb174530b2036987575b83442b47. Stop the container before attempting removal or force remove.

移除所有已經停止的 container

要移除以前可以使用以下指令先看一下沒在使用的 Container 有哪些

docker container ls -a --filter status=exited --filter status=created

要移除所有已經停止的 Container 可以使用以下指令

docker container prune

如果要不出現提示可以使用 -f 或是 –force

WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y

使用 filter 移除 Container

docker container prune 移除指令還可以使用 –filter

如果要移除超過 12 小時以前的 Container 可以使用以下指令

docker container prune --filter "until=12h"

停止並移除所有的 Container

使用指令 docker container ls -aq 可以獲取所有 Container 的 ID,停止 Container 的指令是 docker container stop,這時候就可使用組合技把他們都停止

docker container stop $(docker container ls -aq)

移除 container 的指令是 docker container rm,可以再用上面那招把他們都移除

docker container rm $(docker container ls -aq)

移除 Docker Images

移除一個或是多個 Images

可以使用以下指令列表全部的 Images

docker image ls

會得到如以下的資料

REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
centos                  latest              75835a67d134        7 days ago          200MB
ubuntu                  latest              2a4cca5ac898        2 months ago        111MB
linuxize/fedora         latest              a45d6dca3361        3 months ago        311MB
java                    8-jre               e44d62cf8862        3 months ago        311MB

而找到想要移除的 IMAGE ID 就可以用以下的指令移除

docker image rm 75835a67d134 2a4cca5ac898

移除的時候如果出現以下的錯誤代表這個 Image 有 Container 在使用

Error response from daemon: conflict: unable to remove repository reference "centos" (must force) - container cd20b396a061 is using its referenced image 75835a67d134

移除 dangling images

移除的指令如下:

docker image prune

系統會出現以下提示,如果不想出現警告可以使用 -f or --force

WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y

移除所有沒有在使用的 Images

要移除所以沒在使用的 Images 不是只有 dangling 的 Images,可以在 prune加上-a

docker image prune -a
WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y

使用 filter 移除 Images

docker image prune 移除指令還可以使用 –filter

如果要移除超過 12 小時以前的 image 可以使用以下指令

docker image prune --filter "until=12h"

移除 Docker volume

移除一個或是多個 Docker volume 可以使用 docker volume ls 找到 ID

docker volume ls

輸出的結果

DRIVER              VOLUME NAME
local               4e12af8913af888ba67243dec78419bf18adddc3c7a4b2345754b6db64293163
local               terano

找到 VOLUME NAME 然後使用 docker volume rm 就可以移除了

docker volume rm 4e12af8913af888ba67243dec78419bf18adddc3c7a4b2345754b6db64293163

如果出現以下錯誤代表這個 Volume 有在使用

Error response from daemon: remove 4e12af8913af888ba67243dec78419bf18adddc3c7a4b2345754b6db64293163: volume is in use - [c7188935a38a6c3f9f11297f8c98ce9996ef5ddad6e6187be62bad3001a66c8e]

移除沒有在使用的 Volume 移除的指令如下:

docker volume prune

系統會出現以下提示,如果不想出現警告可以使用 -f or --force

WARNING! This will remove all local volumes not used by at least one container.
Are you sure you want to continue? [y/N]

移除 Docker network

移除一個或是多個 Network

要移除一個或是多個 Network 可以使用 docker network ls 找到 ID

docker network ls

輸出結果

NETWORK ID          NAME                DRIVER              SCOPE
107b8ac977e3        bridge              bridge              local
ab998267377d        host                host                local
c520032c3d31        my-bridge-network   bridge              local
9bc81b63f740        none                null                local

把要移除的 NETWORK ID 給 docker network rm

docker network rm c520032c3d31

如果出現以下錯誤就是這個 network 有在使用

Error response from daemon: network my-bridge-network id 6f5293268bb91ad2498b38b0bca970083af87237784017be24ea208d2233c5aa has active endpoints

移除所有未使用的 Network

使用以下指令可以移除所有未使用的 Network

docker network prune

如果不想出現以下警告可以使用 -f--force

WARNING! This will remove all networks not used by at least one container.
Are you sure you want to continue? [y/N] 

使用 filter 移除 Network

docker network prune 移除指令還可以使用 –filter

如果要移除超過 12 小時以前的 network 可以使用以下指令

docker network prune --filter "until=12h"

結論

希望以上可以幫助大家要移除 Docker 相關支援的時候幫上大家

參考資料