优秀的编程知识分享平台

网站首页 > 技术文章 正文

删除未使用的Docker容器、映像、卷和网络

nanyue 2024-08-26 17:51:04 技术文章 5 ℃

删除所有未使用的对象

docker system prune会删除所有stopped状态的容器、dangling状态的image和无用网络。

$ docker system prune

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]

-f (--force)可跳过提示。

-a (--all) 慎用,会删除dangling状态和unused状态的image(-a参数会额外清理所有未被容器使用的镜像

$ docker system prune -a

WARNING! This will remove:

- all stopped containers

- all networks not used by at least one container

- all images without at least one container associated to them

- all build cache

Are you sure you want to continue? [y/N]

--volumes 默认情况下,该命令不会删除未使用的卷以防止丢失重要数据,要删除所有未使用的卷,请传递--volumes选项:

$ docker system prune --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

清理所有stopped的容器

$ docker container prune

WARNING! This will remove all stopped containers.

Are you sure you want to continue? [y/N] y

停止并清理所有容器

$ docker container stop $(docker container ls -aq)

$ docker container rm $(docker container ls -aq)

清理dangling镜像

同一个tag多次docker build之后,前面的镜像会变成dangling状态,这些镜像清理是安全的。

$ docker image prune

WARNING! This will remove all dangling images.

Are you sure you want to continue? [y/N] y

清理所有unused镜像

慎用,-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

清理所有unused volumes

要删除所有未使用的卷,运行docker volume prune命令:

$ docker volume prune

WARNING! This will remove all local volumes not used by at least one container.

Are you sure you want to continue? [y/N]

清理所有unused network

要删除所有未使用的网络,运行docker network prune命令:

$ docker network prune

WARNING! This will remove all networks not used by at least one container.

Are you sure you want to continue? [y/N]

最近发表
标签列表