惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyberwarzone
Cyberwarzone
V
Vulnerabilities – Threatpost
T
Tenable Blog
Forbes - Security
Forbes - Security
Simon Willison's Weblog
Simon Willison's Weblog
AWS News Blog
AWS News Blog
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
S
Securelist
C
Cybersecurity and Infrastructure Security Agency CISA
Project Zero
Project Zero
C
CXSECURITY Database RSS Feed - CXSecurity.com
V
Visual Studio Blog
WordPress大学
WordPress大学
Latest news
Latest news
K
Kaspersky official blog
T
Tailwind CSS Blog
T
Threat Research - Cisco Blogs
B
Blog RSS Feed
C
Cisco Blogs
博客园 - 聂微东
Martin Fowler
Martin Fowler
T
The Blog of Author Tim Ferriss
小众软件
小众软件
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
L
LINUX DO - 热门话题
Stack Overflow Blog
Stack Overflow Blog
罗磊的独立博客
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
P
Privacy International News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
CERT Recently Published Vulnerability Notes
Cisco Talos Blog
Cisco Talos Blog
S
SegmentFault 最新的问题
Security Latest
Security Latest
Y
Y Combinator Blog
爱范儿
爱范儿
aimingoo的专栏
aimingoo的专栏
P
Privacy & Cybersecurity Law Blog
L
LINUX DO - 最新话题
月光博客
月光博客
The GitHub Blog
The GitHub Blog
博客园 - 三生石上(FineUI控件)
S
Security Affairs
P
Proofpoint News Feed
D
DataBreaches.Net
有赞技术团队
有赞技术团队
云风的 BLOG
云风的 BLOG

jobcher on 打工人日志

2023-12-21 打工人日报 2023-12-20 打工人日报 2023-12-19 打工人日报 2023-12-18 打工人日报 2023-12-17 打工人日报 2023-12-16 打工人日报 2023-12-15 打工人日报 2023-12-14 打工人日报 2023-12-13 打工人日报 2023-12-12 打工人日报 2023-12-11 打工人日报 2023-12-10 打工人日报 2023-12-09 打工人日报 2023-12-08 打工人日报 2023-12-07 打工人日报 2023-12-06 打工人日报 2023-12-05 打工人日报 2023-12-04 打工人日报 2023-12-03 打工人日报 2023-12-02 打工人日报 2023-12-01 打工人日报 2023-11-30 打工人日报 2023-11-29 打工人日报 2023-11-28 打工人日报 2023-11-27 打工人日报 2023-11-26 打工人日报 2023-11-25 打工人日报 2023-11-24 打工人日报 2023-11-23 打工人日报 2023-11-22 打工人日报 2023-11-21 打工人日报 2023-11-20 打工人日报 2023-11-19 打工人日报 2023-11-17 打工人日报 2023-11-16 打工人日报 2023-11-15 打工人日报 2023-11-14 打工人日报 2023-11-13 打工人日报 2023-11-12 打工人日报 2023-11-11 打工人日报 2023-11-10 打工人日报 2023-11-09 打工人日报 2023-11-08 打工人日报 2023-11-07 打工人日报 2023-11-06 打工人日报 2023-11-05 打工人日报 2023-11-04 打工人日报 2023-11-03 打工人日报 2023-11-02 打工人日报 2023-11-01 打工人日报 2023-10-31 打工人日报 2023-10-30 打工人日报 2023-10-29 打工人日报 2023-10-28 打工人日报 2023-10-27 打工人日报 2023-10-26 打工人日报 2023-10-25 打工人日报 2023-10-24 打工人日报 2023-10-23 打工人日报 2023-10-22 打工人日报 2023-10-21 打工人日报 2023-10-20 打工人日报 2023-10-19 打工人日报 2023-10-18 打工人日报 2023-10-17 打工人日报 2023-10-16 打工人日报 2023-10-15 打工人日报 2023-10-14 打工人日报 2023-10-13 打工人日报 2023-10-12 打工人日报 2023-10-11 打工人日报 2023-10-10 打工人日报 2023-10-09 打工人日报 2023-10-07 打工人日报 2023-10-06 打工人日报 2023-10-05 打工人日报 2023-10-04 打工人日报 2023-10-03 打工人日报 2023-10-02 打工人日报 2023-10-01 打工人日报 2023-09-30 打工人日报 2023-09-29 打工人日报 2023-09-28 打工人日报 2023-09-27 打工人日报 2023-09-26 打工人日报 2023-09-25 打工人日报 2023-09-24 打工人日报 2023-09-23 打工人日报 2023-09-22 打工人日报 2023-09-21 打工人日报 2023-09-20 打工人日报 2023-09-19 打工人日报 2023-09-18 打工人日报 2023-09-17 打工人日报 2023-09-16 打工人日报 2023-09-15 打工人日报 2023-09-14 打工人日报 2023-09-13 打工人日报 2023-09-12 打工人日报 2023-09-11 打工人日报
清理Docker的container,image与volume
2022-02-10 · via jobcher on 打工人日志

清理 Docker 的 container,image 与 volume

Docker 的镜像(image)、容器(container)、数据卷(volume), 都是由 daemon 托管的。 因此,在需要清理时,也需要使用其自带的手段。

清理技巧

清理所有停止运行的容器:

1docker container prune
2# or
3docker rm $(docker ps -aq)

清理所有悬挂(<none>)镜像:

1docker image prune
2# or
3docker rmi $(docker images -qf "dangling=true")

清理所有无用数据卷:

由于prune操作是批量删除类的危险操作,所以会有一次确认。 如果不想输入y<CR>来确认,可以添加-f操作。慎用!

清理停止的容器

docker rm -lv CONTAINER -l是清理 link,v是清理 volume。 这里的 CONTAINER 是容器的 name 或 ID,可以是一个或多个。

参数列表:

Name shorthandDefaultDescription
–force,-ffalseForce the removal of a running container (uses SIGKILL)
–link, -lfalseRemove the specified link
–volumes, -vfalseRemove the volumes associated with the container

清理所有停止的容器

通过docker ps可以查询当前运行的容器信息。 而通过docker ps -a,可以查询所有的容器信息,包括已停止的。

在需要清理所有已停止的容器时,通常利用 shell 的特性,组合一下就好。

docker rm $(docker ps -aq) 其中,ps的-q,是只输出容器 ID,方便作为参数让rm使用。 假如给rm指定-f,则可以清理所有容器,包括正在运行的。

这条组合命令,等价于另一条命令:

container子命令,下面包含了所有和容器相关的子命令。 包括docker ps,等价于docker container psdocker container ls。 其余还有start、stop、kill、cp等,一级子命令相当于二级子命令在外面的 alias。 而prune则是特别提供的清理命令,这在其它的管理命令里还可以看到,比如image、volume

按需批量清理容器

清除所有已停止的容器,是比较常用的清理。 但有时会需要做一些特殊过滤。

这时就需要使用docker ps --filter

比如,显示所有返回值为 0,即正常退出的容器:

1docker ps -a --filter 'exited=0'

同理,可以得到其它非正常退出的容器。

目前支持的过滤器有:

id (container’s id)
label (label=<key> or label=<key>=<value>)
name (container’s name)
exited (int - the code of exited containers. Only useful with –all)
status (created|restarting|running|removing|paused|exited|dead)
ancestor (<image-name>[:<tag>], <image id> or <image@digest>) - filters containers that were created from the given image or a descendant.
before (container’s id or name) - filters containers created before given id or name
since (container’s id or name) - filters containers created since given id or name
isolation (default|process|hyperv) (Windows daemon only)
volume (volume name or mount point) - filters containers that mount volumes.
network (network id or name) - filters containers connected to the provided network
health (starting|healthy|unhealthy|none) - filters containers based on healthcheck status

清理失败

如果在清理容器时发生失败,通过重启 Docker 的 Daemon,应该都能解决问题。

1# systemd
2sudo systemctl restart docker.service
3
4# initd
5sudo service docker restart

清理镜像

与清理容器的ps、rm类似,清理镜像也有images、rmi两个子命令。 images用来查看,rmi用来删除。

清理镜像前,应该确保该镜像的容器,已经被清除。

其中,IMAGE 可以是 name 或 ID。 如果是 name,不加 TAG 可以删除所有 TAG。

另外,这两个命令也都属于 alias。 docker images等价于docker image ls,而docker rmi等价于docker image rm

按需批量清理镜像 ¶ 与ps类似,images也支持--filter参数。

与清理相关,最常用的,当属<none>了。

1docker images --filter "dangling=true"

这条命令,可以列出所有悬挂(dangling)的镜像,也就是显示为的那些。

1docker rmi $(docker images -qf "dangling=true")

这条组合命令,如果不写入 Bash 的 alias,几乎无法使用。 不过还有一条等价命令,非常容易使用。

pruneimages类似,也同样支持–filter 参数。 其它的filter有:

dangling (boolean - true or false)
label (label=<key> or label=<key>=<value>)
before (<image-name>[:<tag>], <image id> or <image@digest>) - filter images created before given id or references
since (<image-name>[:<tag>], <image id> or <image@digest>) - filter images created since given id or references
reference (pattern of an image reference) - filter images whose reference matches the specified pattern

清理所有无用镜像

这招要慎用,否则需要重新下载。

清理数据卷

数据卷不如容器或镜像那样显眼,但占的硬盘却可大可小。

数据卷的相关命令,都在docker volume中了。

一般用docker volume ls来查看,用docker volume rm VOLUME来删除一个或多个。

不过,绝大多数情况下,不需要执行这两个命令的组合。 直接执行docker volume prune就好,即可删除所有无用卷。

注意:这是一个危险操作!甚至可以说,这是本文中最危险的操作! 一般真正有价值的运行数据,都在数据卷中。 (当然也可能挂载到了容器外的文件系统里,那就没关系。) 如果在关键服务停止期间,执行这个操作,很可能会丢失所有数据!

从文件系统删除

除配置文件以为,Docker 的内容相关文件,基本都放在/var/lib/docker/目录下。

该目录下有下列子目录,基本可以猜测出用途:

aufs
containers
image
network
plugins
swarm
tmp
trust
volumes

一般不推荐直接操作这些目录,除非一些极特殊情况。 操作不当,后果难料,需要慎重。