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

推荐订阅源

WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
Cloudbric
Cloudbric
P
Palo Alto Networks Blog
T
Threatpost
T
Tor Project blog
T
Tenable Blog
AWS News Blog
AWS News Blog
Project Zero
Project Zero
L
LangChain Blog
Cyberwarzone
Cyberwarzone
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
C
CERT Recently Published Vulnerability Notes
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Latest
Security Latest
云风的 BLOG
云风的 BLOG
I
Intezer
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
MongoDB | Blog
MongoDB | Blog
aimingoo的专栏
aimingoo的专栏
K
Kaspersky official blog
Jina AI
Jina AI
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
D
DataBreaches.Net
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Secure Thoughts
TaoSecurity Blog
TaoSecurity Blog
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
IT之家
IT之家
Forbes - Security
Forbes - Security
The Hacker News
The Hacker News
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
Y
Y Combinator Blog

docker on 打工人日志

DockerHub 加速镜像部署 - 使用cloudflare 代理 docker的零停机部署 清理Docker容器日志 contained 安装及使用 docker 命令(2) docker 和 docker-compose 安装 docker 安装kong 网关 搭建docker registry 镜像仓库 docker image镜像上传 docker进阶使用
清理Docker的container,image与volume
2022-02-10 · via docker 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

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