长时间运行Docker时,系统中有很多图像。如何一次安全删除所有未使用的Docker图像以释放存储空间?
此外,我还想删除具有正确标签
的几个月前拉的图像。
因此,我不是只要求删除未标记的图像。我正在寻找一种删除一般未使用的图像的方法,其中包括未标记的图像和其他图像,例如几个月前使用正确的标签
。
When running Docker for a long time, there are a lot of images in system. How can I remove all unused Docker images at once safety to free up the storage?
In addition, I also want to remove images pulled months ago, which have the correct TAG
.
So, I'm not asking for removing untagged images only. I'm searching for a way to remove general unused images, which includes both untagged and other images such as pulled months ago with correct TAG
.
发布评论
评论(30)
(请参阅下文以获取原始答案)
更新2016年9月:Docker 1.13:“ noreferrer”> 26108 和
docker system prune
将删除所有悬挂数据(容器,网络和图像)。您可以使用- 卷
选项删除所有未使用的卷,并使用-A
选项删除所有未使用的图像(而不仅仅是悬空)。您也有:
docker Container Prune
/docs.docker.com/engine/reference/commandline/network_prune/“ rel =“ noreferrer”>
docker network prune
docker卷Prune
对于 未使用的图像,使用
docker image prune -a prune -a a
(用于删除悬空和未汇总的图像)。警告:'未使用'的意思是“任何容器未引用的图像”:在使用
-A
之前请小心。如 a l '答案,
Docker System Prune -all
将删除所有未使用的图像,而不仅仅是悬挂的图像……这可能太多了。将
docker xxx Prune
与 - noreferrer“> -- 过滤
选项可以是限制修剪的好方法( Docker SDK API 1.28最小值,所以Docker 17.04+ )直到(< timestamp>)
- 仅删除给定时间戳label
(label =< key&gt) ;
,label =< key> =< value>
,label!=< key>
,或label!=< key&gey> ; =< value>
) - 仅删除使用(或 的)的容器,图像,网络和卷使用)指定的标签。请参阅“ Prune Images “
警告:对于那些
Docker XXX Prune
命令,没有“预览”或“- Dry-Run
”选项。请
moby/moby/cody
第30623页自2017年以来但似乎棘手Original answer (Sep. 2016)
I usually do:
I have an [alias for removing those
以这种方式找到未使用的图像,删除了标记图像不再引用的任何中间图像。
对于
AS haridsv 指出在评论中:
jess frazelle(jfrazelle) bashrc函数
/A7FD3DF6AB423E6DD04F27727F653753453DB837/.dockerfunc#l8-l8-l8-l8-l8-l8-l8-l1 github.com/spotify/docker-gc“ rel =“ noreferrer”>
docker-gc
:(see below for original answer)
Update Sept. 2016: Docker 1.13: PR 26108 and commit 86de7c0 introduce a few new commands to help facilitate visualizing how much space the docker daemon data is taking on disk and allowing for easily cleaning up "unneeded" excess.
docker system prune
will delete all dangling data (containers, networks, and images). You can remove all unused volumes with the--volumes
option and remove all unused images (not just dangling) with the-a
option.You also have:
docker container prune
docker image prune
docker network prune
docker volume prune
For unused images, use
docker image prune -a
(for removing dangling and ununsed images).Warning: 'unused' means "images not referenced by any container": be careful before using
-a
.As illustrated in A L's answer,
docker system prune --all
will remove all unused images not just dangling ones... which can be a bit too much.Combining
docker xxx prune
with the--filter
option can be a great way to limit the pruning (docker SDK API 1.28 minimum, so docker 17.04+)until (<timestamp>)
- only remove containers, images, and networks created before given timestamplabel
(label=<key>
,label=<key>=<value>
,label!=<key>
, orlabel!=<key>=<value>
) - only remove containers, images, networks, and volumes with (or without, in caselabel!=...
is used) the specified labels.See "Prune images" for an example.
Warning: there is no "preview" or "
--dry-run
" option for thosedocker xxx prune
commands.This is requested with
moby/moby
issue 30623 since 2017, but seems tricky to be implemented (Aug. 2022)Original answer (Sep. 2016)
I usually do:
I have an [alias for removing those dangling images:
drmi
]13That way, any intermediate image no longer referenced by a labelled image is removed.
I do the same first for exited processes (containers)
As haridsv points out in the comments:
Jess Frazelle (jfrazelle) has the bashrc function:
To remove old images, and not just "unreferenced-dangling" images, you can consider
docker-gc
:更新第二个(2017-07-08)
使用更新的
System Prune
(再次)(再次),请参阅VONC。不耐烦的人可以使用-f,-force
选项跳过提示:不耐烦和鲁ckless 可以另外删除“未使用的图像,而不仅仅是悬挂图像” > -a, - all 选项:
https://docs.docker .com/Engine/reference/commandline/system_prune/
更新
请参阅 vonc的答案使用最近添加的代码>修剪命令。这是相应的壳牌别名便利:
旧答案
删除停止(退出)容器:
删除未使用(悬挂)图像:
如果您对进行了 不可撤销的数据丢失 ,然后您可以删除未使用的(悬挂)卷(v1.9及以上):
在这里它们处于方便的外壳别名:
参考文献
docker ps -f
docker rm
docker images -f
docker rmi
docker卷代码>
Update the second (2017-07-08)
Refer (again) to VonC, using the even more recent
system prune
. The impatient can skip the prompt with the-f, --force
option:The impatient and reckless can additionally remove "unused images not just the dangling ones" with the
-a, --all
option:https://docs.docker.com/engine/reference/commandline/system_prune/
Update
Refer to VonC's answer which uses the recently added
prune
commands. Here is the corresponding shell alias convenience:Old answer
Delete stopped (exited) containers:
Delete unused (dangling) images:
If you have exercised extreme caution with regard to irrevocable data loss, then you can delete unused (dangling) volumes (v1.9 and up):
Here they are in a convenient shell alias:
References
docker ps -f
docker rm
docker images -f
docker rmi
docker volume ls
docker volume rm
其他答案很棒,特别是:
但是我在两个命令的中间需要一些东西,因此
filter
选项是我需要的:供参考: https://docs.docker.com/config/config/pruning/#prune-images
The other answers are great, specifically:
But I needed something in the middle of the two commands so the
filter
option was what I needed:For reference: https://docs.docker.com/config/pruning/#prune-images
删除旧标记的超过一个月的图像:
请注意,要删除由存储库中引用的容器使用的图像的失败具有取决于儿童图像...这可能是您想要的。否则只需添加
-f
标志即可。/etc/cron.daily/docker-gc
脚本的示例:To remove old tagged images that are more than a month old:
Note that it'll fail to remove images that are used by a container, referenced in a repository, has dependent child images... which is probably what you want. Else just add
-f
flag.Example of
/etc/cron.daily/docker-gc
script:根据 doc ,以下命令将删除超过48小时的图像。
According to the doc, the following command will delete images older than 48 hours.
假设您有 docker 1.13 或更高的您只能使用Prune命令。对于专门用于删除旧图像的问题,您想要第一个。
我建议不要习惯使用
docker System Prune
命令。我认为用户会不小心删除他们并不是故意的事情。就个人而言,我将主要使用Docker Image Prune
和Docker Container Prune
命令。Assuming you have Docker 1.13 or higher you can just use the prune commands. For your question specifically for removing old images, you want the first one.
I would recommend not getting used to using the
docker system prune
command. I reckon users will accidentally remove things they don't mean to. Personally, I'm going to mainly be using thedocker image prune
anddocker container prune
commands.到目前为止(Docker版本1.12)我们使用以下命令来删除所有运行的容器。另外,如果要删除卷,我们可以在以下命令中使用其各自的标签-v手动执行此操作。
删除所有退出的容器
删除所有停止的容器
删除所有运行和停止容器
删除所有容器,没有任何条件,
但是,在版本1.13及更高版本中,要进行完整的系统和清理,我们可以直接用户以下命令:
所有未使用的容器,图像,网络和卷都将被删除。我们还可以使用以下命令来完成此操作,以清理各个组件:
Until now (Docker version 1.12) we are using the following command to delete all the running containers. Also, if we want to delete the volumes, we can do that manually using its respective tag -v in the following command.
Delete all Exited Containers
Delete all Stopped Containers
Delete All Running and Stopped Containers
Remove all containers, without any criteria
But, in version 1.13 and above, for complete system and cleanup, we can directly user the following command:
All unused containers, images, networks and volumes will get deleted. We can also do this using the following commands that clean up the individual components:
这对我有用:
This worked for me:
我最近写了一个脚本来解决我的一台服务器:
I recently wrote a script to solve this on one of my servers:
这是一个清理Docker图像并收回空间的脚本。
原始脚本
https://github.com/vishalvsh1/docker-image-image-cleanup
通常将所有临时文件与图像构建和图层相关,
/var/lib/docker
此路径是系统的本地路径,通常在根分区, “/”/“” 。
您可以安装较大的磁盘空间,并将
/var/lib/docker
的内容移至新的安装位置,并创建一个符号链接。这样,即使Docker图像占据了空间,也不会影响您的系统,因为它将使用其他安装位置。
原始文章: 管理Docker Images在本地磁盘上
Here is a script to clean up Docker images and reclaim the space.
Original script
https://github.com/vishalvsh1/docker-image-cleanup
Usually Docker keeps all temporary files related to image building and layers at
/var/lib/docker
This path is local to the system, usually at THE root partition, "/".
You can mount a bigger disk space and move the content of
/var/lib/docker
to the new mount location and make a symbolic link.This way, even if Docker images occupy space, it will not affect your system as it will be using some other mount location.
Original post: Manage Docker images on local disk
我正在使用此命令:
这将删除所有创建时间大于10周前的图像。
I'm using this command:
This will remove all images whose creation time is greater than 10 weeks ago.
如果您想删除数月前删除 x 的图像,则可以尝试以下示例,该示例删除三个月前创建的图像:
If you want to remove images pulled X months ago, you can try the below example which remove images created three months ago:
修剪所有图像和卷
Docker System Prune -af -Volumes
To prune all images and volumes as well
docker system prune -af --volumes
由于您已经有很长的时间跑了很长时间,因此可能有各种物品占用空间,而不仅仅是图像。找出使用该空间的方法:
Docker System DF
请参阅docs在我的情况下大部分是由“构建缓存”使用的,将其删除:
Docker Builder Prune
请参阅文档Since you've had docker running for a long time there might be various items taking up space, not just images. To find out what is using the space:
docker system df
see docsIn my case most of it was used by "Build cache", to remove it:
docker builder prune
see docsDocker System Prune -A
(您被要求确认命令。如果您知道自己在做什么,请使用
-f
强制运行。)docker system prune -a
(You'll be asked to confirm the command. Use
-f
to force run, if you know what you're doing.)@vonc已经给出了一个很好的答案,但是对于完整性来说,这是我一直在使用的一些脚本---也可以在任何差事的码头流程中添加一些脚本:
@VonC already gave a very nice answer, but for completeness here is a little script I have been using---and which also nukes any errand Docker processes should you have some:
要删除未运行容器的标记图像,您将必须使用一个小脚本:
To remove tagged images which have not container running, you will have to use a little script:
几周前卸下旧容器。
docker rm $(docker ps -a | grep“ weeks” | awk'{print $ 1;}')
几周前删除旧图像。当心。这将删除几周前创建的基本图像,但您的新图像可能正在使用。
docker rmi $(docker images | grep'weeks'| awk'{print $ 3;}')
>Remove old containers weeks ago.
docker rm $(docker ps -a | grep "weeks" | awk '{ print $1; }')
Remove old images weeks ago. Be careful. This will remove base images which was created weeks ago but which your new images might be using.
docker rmi $(docker images | grep 'weeks' | awk '{ print $3; }')
如何删除标记的图像
docker rmi标签首先
docker rmi rmi图像。
#可以在一个Docker RMI调用中完成的
Docker rmi&lt; repo:tag&gt; &lt; imageId&gt;
(这项工作2016年11月,Docker版本1.12.2)
例如
,脚本删除了2周以上的任何东西。
How to remove a tagged image
docker rmi the tag first
docker rmi the image.
# that can be done in one docker rmi call e.g.: #
docker rmi <repo:tag> <imageid>
(this works Nov 2016, Docker version 1.12.2)
e.g.
e.g. Scripted remove anything older than 2 weeks.
-f力
-a all
-q在模式下
-f force
-a all
-q in the mode
首先,运行
Docker Images
以查看图像列表,并将图像哈希ID复制到剪贴板中。运行
Docker RMI -F&lt; image&gt;
记住选项
-f
是强制删除。First, run
docker images
to see list of images and copy IMAGE HASH ID into clipboard.Run
docker rmi -f <Image>
Remember option
-f
is force deleting.有时我会遇到Docker会分配并继续使用磁盘空间的问题,即使没有将空间分配给任何特定图像或现有容器。我意外地生成此问题的最新方式是在RHEL 7.1中使用“ Docker-engine” Centos构建而不是“ Docker”。似乎发生的事情有时是容器清理未能成功完成,因此空间永远不会重复使用。当80GB驱动器分配为
/
时,我必须提出一种创造性的方法来解决问题。这是我想到的。首先解决磁盘完整错误:
停止Docker:
SytemCtl停止Docker
分配了一个新驱动器,如SAS
/mnt/docker
。将
/var/lib/docker中的所有文件移动到
/mnt/docker
。我使用了命令:将新驱动器安装到
/var/lib/docker
。在这一点上,我不再有磁盘完全错误,但是我仍在浪费大量空间。接下来的步骤是要解决这个问题。
开始Docker:
SytemCtl开始Docker
保存所有图像:
卸载Docker。
在
/var/lib/docker
中删除所有内容:重新安装docker
启用Docker:
Systemctl启用Docker
start docker:
systemctl start docker
还原图像:
启动您需要运行的任何持久容器。
这使我的磁盘用法从67 GB的Docker降至Docker的6 GB。
我不建议您每天使用。但是,当Docker似乎已经失去了对软件错误或意外重启的途径时,运行很有用。
Occasionally I have run into issues where Docker will allocate and continue to use disk space, even when the space is not allocated to any particular image or existing container. The latest way I generated this issue accidentally was using "docker-engine" centos build instead of "docker" in RHEL 7.1. What seems to happen is sometimes the container clean-ups are not completed successfully and then the space is never reused. When the 80GB drive I allocated as
/
was filled with/var/lib/docker
files I had to come up with a creative way to resolve the issue.Here is what I came up with. First to resolve the disk full error:
Stop docker:
systemctl stop docker
Allocated a new drive mounted as say
/mnt/docker
.Move all the files in
/var/lib/docker
to/mnt/docker
. I used the command:Mount the new drive to
/var/lib/docker
.At this point I no longer had a disk full error, but I was still wasting a huge amount of space. The next steps are to take care of that.
Start Docker:
systemctl start docker
Save the all the images:
Uninstall docker.
Erase everything in
/var/lib/docker
:Reinstall docker
Enable docker:
systemctl enable docker
Start docker:
systemctl start docker
Restore images:
Start any persistent containers you need running.
This dropped my disk usage from 67 GB for docker to 6 GB for docker.
I do not recommend this for everyday use. But it is useful to run when it looks like docker has lost track of used disk space do to software errors, or unexpected reboots.
或者
or
如果您希望自动/定期清理退出的容器,并删除运行容器未使用的图像和量,则可以下载图像
Meltwater/Docker-Cleanup
。刚运行:
默认情况下每30分钟运行一次。但是,您可以在秒内使用此标志(delay_time = 1800选项)来设置延迟时间。
更多详细信息: https://github.com/meltwater/meltwater/meltwater/docker-cleanup/docker-cleanup-cleanup- /blob/master/readme.md
If you wish to automatically/periodically clean up exited containers and remove images and volumes that aren't in use by a running container you can download the image
meltwater/docker-cleanup
.Just run:
It runs every 30 minutes by default. You can however set the delay time by using this flag in seconds (DELAY_TIME=1800 option).
More details: https://github.com/meltwater/docker-cleanup/blob/master/README.md
如果您自己构建这些修剪的图像(来自其他一些较旧的基本图像),请谨慎使用基于
docker image prune
的上述可接受的解决方案,因为该命令是钝的,并且会尝试删除所有依赖项您的最新图像要求(命令可能应重命名为Docker Image* S* Prune
)。我为Docker Image构建管道提出的解决方案(在
yyyymmdd
格式中,有每日构建和标签=日期)是:If you build these pruned images yourself (from some other, older base images) please be careful with the accepted solutions above based on
docker image prune
, as the command is blunt and will try to remove also all dependencies required by your latest images (the command should be probably renamed todocker image*s* prune
).The solution I came up for my docker image build pipelines (where there are daily builds and tags=dates are in the
YYYYMMDD
format) is this:=的官方参考
请参阅将删除:
docker system prune -a
都会做同样的事情,但是除了删除所有悬挂图像,它将更广泛地删除:什么是悬空的图像?
docker图像由多层包装在父母的容器层中``当从dockerfile生成整体容器图像时。悬空的图像是与任何其他标记的图像没有关系的层,因此在构建的任何新容器中都不会使用任何用途。他们不再有目的并消耗磁盘空间。
例如,可以通过以下过程创建悬空图像:
构建一个命名映像
my-image
从dockerfile构建,而无需指定任何标签:更新dockerfile:< /em>
重建图像重复使用上一个名称,而无需指定任何标签:
构建创建了一个新的
my-image
layer。如我们所见,最初创建的图层仍然存在,但其名称和标签设置为&lt; none&gt;:&lt; none&gt;
。该层永远不可能与任何Docker容器层关联,这意味着它是“悬空”什么是没有至少一个容器的图像?
未使用的图像意味着它没有被分配或在容器中使用。例如,
docker ps -a
将列出您的所有运行和停止容器。这些容器中的任何一个都使用的任何图像都是“使用的图像”。运行Docker System Prune -a时,它将删除未使用的图像和悬空图像。与其至少一个容器相关的任何图像都不会受到影响。
See the official reference for docker system prune
docker system prune
will remove:docker system prune -a
will do the same, but in additional to removing all dangling images, it will more broadly remove:What are dangling images?
Docker images consist of multiple layers that get wrapped inside a parent 'container layer' when the overall container image is generated from a Dockerfile. Dangling images are layers that have no relationship to any other tagged images, and will therefore never have any use within any new containers that are built. They no longer serve a purpose and consume disk space.
For example a dangling image can be created by the following process:
Build a named image
my-image
from Dockerfile, without specifying any tag:Update the Dockerfile:
Rebuild image re-using the previous name, without specifying any tag:
The build created a new
my-image
layer. As we can see, the layer that was originally created is still there, but its name and tag are set to<none>:<none>
. It will never be possible for this layer to be associated with any docker container layer, which means it's 'dangling'What are images without at least one container associated to them?
An unused image means that it has not been assigned or used in a container. For example,
docker ps -a
will list all of your running and stopped containers. Any image being used by any of these containers is a "used image".When running docker system prune -a, it will remove both unused and dangling images. Any image with at least one container associated to it will not be affected.
有麻雀插件 docker-remove-dangangling-images 您可以用来清洁上下停止的容器和未使用的(悬挂)图像:
$ SPARROW PLG运行Docker-remove-dangling-Images
它既适用于Linux和Windows OS。
There is sparrow plugin docker-remove-dangling-images you can use to clean up stopped containers and unused (dangling) images:
$ sparrow plg run docker-remove-dangling-images
It works both for Linux and Windows OS.
如果您有很多,那么它可能真的很繁琐,但要删除它们,但是幸运的是,我们Docker有一些命令可以帮助我们消除悬挂的图像。在Docker的较旧版本中(并且今天仍然有效),您可以通过运行
docker rmi -f $(docker&nbsp; images&nbsp; -f“ dandling = true” -q)删除悬挂图像&nbsp;代码>。
If you have a lot of them, it can be really tedious to remove them, but lucky for us Docker has a few commands to help us eliminate dangling images. In older versions of Docker (and this still works today), you can delete dangling images on their own by running
docker rmi -f $(docker images -f "dangling=true" -q)
.我通常会做
Docker RM -F $(Docker PS -A -Q)
和Docker System Prune
以清除所有悬挂容器。I usually do
docker rm -f $(docker ps -a -q)
anddocker system prune
to purge all dangling containers.一个尚未删除所有容器的解决方案:
Docker PS -A |切割-d''-f 1 | Xargs Docker RM
剪切-d''-f 1
返回所有容器的ID。One yet solution for removing all containers:
docker ps -a | cut -d ' ' -f 1 | xargs docker rm
cut -d ' ' -f 1
returns all containers' IDs.