This question is related to Should I be concerned about excess, non-running, Docker containers?.
这个问题与我是否应该关注过量的、不运行的Docker容器有关?
I'm wondering how to remove old containers. The docker rm 3e552code34a
lets you remove a single one, but I have lots already. docker rm --help
doesn't give a selection option (like all, or by image name).
我想知道如何移除旧的容器。docker rm 3e552code34a让你删除一个,但我已经有很多了。docker rm——帮助不提供选择选项(像所有的,或者用图像名)。
Maybe there is a directory in which these containers are stored where I can delete them easily manually?
可能有一个目录,其中存储了这些容器,我可以手动删除它们?
51 个解决方案
#1
1033
There is a new feature in Docker 1.13.x called Docker container prune: docker container prune
This will do what you want and should work on all platforms the same way.
Docker 1.13中有一个新特性。x被称为Docker容器prune: Docker容器prune这将做你想做的事情,并且应该在所有平台上使用相同的方法。
There is also a docker system prune: docker system prune
, which will clean up containers, images, volumes, and networks all in one command.
还有一个docker系统prune: docker系统prune,它可以在一个命令中清理容器、图像、卷和网络。
Original Answer:
最初的回答:
There has been some talk about a Docker cleanup command. You can find the information on this ticket: https://github.com/dotcloud/docker/issues/928
有一些关于Docker清理命令的讨论。您可以在这张彩票上找到相关信息:https://github.com/dotcloud/docker/es/928。
Until that command is available, you can string Docker commands together with other Unix commands to get what you need. Here is an example on how to clean up old containers that are weeks old:
在该命令可用之前,您可以与其他Unix命令一起使用字符串Docker命令来获得所需的内容。下面是一个如何清理几周大的旧容器的例子:
$ docker ps --filter "status=exited" | grep 'weeks ago' | awk '{print $1}' | xargs --no-run-if-empty docker rm
To give credit, where it is due, this example is from https://twitter.com/jpetazzo/status/347431091415703552.
为了给它提供信用,这个例子来自https://twitter.com/jpetazzo/status/347431091415703552。
#2
570
Another method, which I got from Guillaume J. Charmes (credit where it is due):
另一种方法,是我从Guillaume J. Charmes那里得到的:
docker rm `docker ps --no-trunc -aq`
will remove all containers in an elegant way.
将以优雅的方式移除所有容器。
And by Bartosz Bilicki, for Windows:
以及Bartosz Bilicki,为Windows:
FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i
For PowerShell:
PowerShell:
docker rm @(docker ps -aq)
An update with Docker 1.13 (Q4 2016), credit to VonC (later in this thread):
与Docker 1.13(2016年Q4)的更新,VonC(稍后在此线程中):
docker system prune
will delete ALL unused data (i.e., in order: containers stopped, volumes without containers and images with no containers).
docker系统prune将删除所有未使用的数据(即:,顺序是:容器停止,体积没有容器,图像没有容器。
See PR 26108 and commit 86de7c0, which are introducing 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.
请参阅PR 26108和提交86de7c0,它将引入一些新的命令,以帮助可视化Docker守护进程数据在磁盘上占用了多少空间,并允许轻松清除“不必要的”过量。
docker system prune
WARNING! This will remove:
- all stopped containers
- all volumes not used by at least one container
- all images without at least one container associated to them
Are you sure you want to continue? [y/N] y
#3
374
Updated Answer Use docker system prune
or docker container prune
now. See VonC's updated answer: https://*.com/a/39860665/411229
更新的答案使用docker系统prune或docker容器现在。参见VonC更新的答案:https://*.com/a/39860665/411229。
Previous Answer Composing several different hints above, the most elegant way to remove all non-running containers seems to be:
前面的答案组合了几个不同的提示,最优雅的方法是删除所有非运行的容器:
docker rm $(docker ps -q -f status=exited)
docker rm $(docker ps -q -f状态=退出)
-
-q
prints just the container ids (without column headers) - -q只打印容器id(没有列标题)
-
-f
allows you to filter your list of printed containers (in this case we are filtering to only show exited containers) - -f允许您过滤您的打印容器列表(在本例中,我们过滤的只是显示已退出的容器)
#4
238
The official way is:
官方的方式是:
docker rm `docker ps -aq`
The Docker maintainers have indicated there will be no command for this - and you compose the commands like that:
Docker维护者表示将不会对此进行命令,您可以编写如下命令:
We have discussed this before and prefer users to use the above line without having to add additional code to Docker.
我们之前已经讨论过这个问题,并且希望用户可以使用上面的代码,而不需要向Docker添加额外的代码。
#5
98
It is now possible to use filtering with docker ps
:
现在可以使用docker ps进行过滤:
docker rm $(docker ps -q -f status=exited)
And for images:
和图片:
docker rmi $(docker images -q -f "dangling=true")
However, any of those will cause docker rm
or docker rmi
to throw an error when there are no matching containers. The older docker rm $(docker ps -aq)
trick was even worse as it tried to remove any running container, failing at each one.
但是,如果没有匹配的容器,其中任何一个都会导致docker rm或docker rmi抛出错误。更老的docker rm $(docker ps -aq)技巧更糟糕,因为它试图删除任何运行的容器,每一个都失败了。
Here's a cleaner script to add in your ~/.bashrc
or ~/.profile
:
这里有一个更清晰的脚本可以添加到你的~/中。bashrc或(~ /。简介:
# Use `docker-cleanup --dry-run` to see what would be deleted.
function docker-cleanup {
EXITED=$(docker ps -q -f status=exited)
DANGLING=$(docker images -q -f "dangling=true")
if [ "$1" == "--dry-run" ]; then
echo "==> Would stop containers:"
echo $EXITED
echo "==> And images:"
echo $DANGLING
else
if [ -n "$EXITED" ]; then
docker rm $EXITED
else
echo "No containers to remove."
fi
if [ -n "$DANGLING" ]; then
docker rmi $DANGLING
else
echo "No images to remove."
fi
fi
}
Edit: As noted below, original answer was for removing images, not containers. Updated to answer both, including new links to documentation. Thanks to Adrian (and Ryan's answer) for mentioning the new ps
filtering.
编辑:如下所述,原始答案是删除图片,而不是容器。更新的答案,包括新的文档链接。感谢Adrian(和Ryan的回答)提到了新的ps过滤。
#6
97
With Docker 1.13 (Q4 2016), you now have:
与Docker 1.13 (Q4 2016),您现在有:
docker system prune
will delete ALL unused data (i.e., in order: containers stopped, volumes without containers and images with no containers).
docker系统prune将删除所有未使用的数据(即:,顺序是:容器停止,体积没有容器,图像没有容器。
See PR 26108 and commit 86de7c0, which are introducing 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.
请参阅PR 26108和提交86de7c0,它将引入一些新的命令,以帮助可视化Docker守护进程数据在磁盘上占用了多少空间,并允许轻松清除“不必要的”过量。
docker system prune
WARNING! This will remove:
- all stopped containers
- all volumes not used by at least one container
- all images without at least one container associated to them
Are you sure you want to continue? [y/N] y
As wjv comments,
wjv评论,
There is also
docker {container,image,volume,network} prune
, which may be used to remove unused instances of just one type of object.还有docker {container,image,volume,network} prune,它可以用来删除一种对象的未使用实例。
Introduced in commit 913e5cb, only for Docker 1.13+.
引入了提交913e5cb,只对Docker 1.13+。
docker container prune
#7
79
UPDATED 2017 (NEWEST)
更新2017(最新)
docker container prune
This - 2017 (OLD) way
这是2017年(旧)的方式。
To remove ALL STOPPED CONTAINERS
移除所有已停止的容器。
docker rm $(docker ps -a -q)
To remove ALL CONTAINERS (STOPPED AND NON STOPPED)
移除所有容器(停止和不停止)
docker rm -f $(docker ps -a -q)
#8
46
Remove all stopped containers:
删除所有停止容器:
docker rm $(docker ps -a | grep Exited | awk '{print $1}')
From the comment by pauk960:
从pauk960的评论:
Since version 1.3.0 you can use filters with
docker ps
, instead ofgrep Exited
usedocker ps -a -f status=exited
. And if you use-q
with it you can get container IDs only instead of full output, no need to use awk for that.从1.3.0版本开始,你可以使用docker ps的过滤器,而不是grep退出使用docker ps - - -f状态=退出。如果你使用-q,你只能得到容器id而不是全部输出,没有必要使用awk。
#9
30
If you do not like to remove all containers, you can select all containers created before or after a specific container with docker ps --before <container-ID>
or with docker ps --since <container-ID>
. This feature is at least in docker version 0.6.5.
如果您不喜欢删除所有容器,您可以选择在特定容器之前或之后使用docker ps创建的所有容器——在 <容器id> 或使用docker ps之前——因为 <容器id> 。该特性至少在docker版本0.6.5中。
Let's say You have developed your system, now it is working but there are a number of containers left. You want to remove containers created before that working version. Determine the ID of the working container with docker ps
.
假设你已经开发了你的系统,现在它在工作,但是还有很多的容器。您希望删除在该工作版本之前创建的容器。使用docker ps确定工作容器的ID。
Remove containers created before an other container
删除在另一个容器之前创建的容器。
docker rm $(docker ps --before 9c49c11c8d21 -q)
Another example. You have your database already running on a docker container. You have developed your application to run on an other container and now You have a number of unneeded containers.
另一个例子。您的数据库已经在docker容器上运行了。您已经开发了在另一个容器上运行的应用程序,现在您有了一些不需要的容器。
Remove containers created after a certain container
删除在某个容器后创建的容器。
docker rm $(docker ps --since a6ca4661ec7f -q)
Docker stores containers in /var/lib/docker/containers
in Ubuntu. I think extra containers do no other harm, but take up disk space.
Docker在Ubuntu中存储/var/lib/docker/container中的容器。我认为额外的容器没有其他的危害,但是占用磁盘空间。
#10
29
Update: As of Docker version 1.13 (released January 2017), you can issue the following command to clean up stopped containers, unused volumes, dangling images and unused networks:
更新:在Docker版本1.13(2017年1月发布)中,您可以发出以下命令来清理停止的容器、未使用的卷、悬空图像和未使用的网络:
docker system prune
If you want to insure that you're only deleting containers which have an exited
status, use this:
如果您想确保只删除具有退出状态的容器,请使用以下内容:
docker ps -aq -f status=exited | xargs docker rm
Similarly, if you're cleaning up docker stuff, you can get rid of untagged, unnamed images in this way:
类似地,如果你正在清理docker的东西,你可以用这种方法清除未标记的、未命名的图像:
docker images -q --no-trunc -f dangling=true | xargs docker rmi
#11
19
Here is my docker-cleanup script, which removes untagged containers and images. Please check the source for any updates.
这里是我的docker-cleanup脚本,它删除未标记的容器和图像。请检查源代码是否有任何更新。
#!/bin/sh
# Cleanup docker files: untagged containers and images.
#
# Use `docker-cleanup -n` for a dry run to see what would be deleted.
untagged_containers() {
# Print containers using untagged images: $1 is used with awk's print: 0=line, 1=column 1.
docker ps -a | awk '$2 ~ "[0-9a-f]{12}" {print $'$1'}'
}
untagged_images() {
# Print untagged images: $1 is used with awk's print: 0=line, 3=column 3.
# NOTE: intermediate images (via -a) seem to only cause
# "Error: Conflict, foobarid wasn't deleted" messages.
# Might be useful sometimes when Docker messed things up?!
# docker images -a | awk '$1 == "<none>" {print $'$1'}'
docker images | tail -n +2 | awk '$1 == "<none>" {print $'$1'}'
}
# Dry-run.
if [ "$1" = "-n" ]; then
echo "=== Containers with uncommitted images: ==="
untagged_containers 0
echo
echo "=== Uncommitted images: ==="
untagged_images 0
exit
fi
# Remove containers with untagged images.
echo "Removing containers:" >&2
untagged_containers 1 | xargs --no-run-if-empty docker rm --volumes=true
# Remove untagged images
echo "Removing images:" >&2
untagged_images 3 | xargs --no-run-if-empty docker rmi
Source: https://github.com/blueyed/dotfiles/blob/master/usr/bin/docker-cleanup
来源:https://github.com/blueyed/dotfiles/blob/master/usr/bin/docker-cleanup
#12
10
First, stop running containers before attempting to remove them
首先,在尝试删除容器之前停止运行容器。
Remove Running containers
docker rm $(docker stop -t=1 $(docker ps -q))
You could use kill
instead of stop
. In my case I prefer stop
since I tend to rerun them vs creating a new one every time so I try to shut them down nicely.
你可以用kill代替stop。在我的例子中,我喜欢停下来,因为我倾向于重新运行它们,每次都创建一个新的,所以我试着很好地关闭它们。
Note: Trying to stop a container will give you an error:
注意:试图阻止一个容器会给你一个错误:
Error: Impossible to remove a running container, please stop it first
错误:无法删除正在运行的容器,请先停止。
Remove All containers
docker rm $(docker ps -a -q)
#13
10
Removing all containers from Windows shell:
从Windows shell中删除所有容器:
FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i
#14
7
https://github.com/HardySimpson/docker-cleanup
https://github.com/HardySimpson/docker-cleanup
Docker cleanup
A tiny all-in-one shell, which removes:
一种小型的全合一的外壳,可以移除:
- Containers that not running more than one day ago
- 不超过一天前运行的容器。
- Images that don't belong to any remaining container
- 不属于任何剩余容器的图像。
Intend to run as a crontab job
打算以crontab的方式运行。
Feature
- It will remove all
<none>:<none>
images - 它将删除所有 <没有> : <没有> 图像。
- If the image has multiple repo:tag references to it, it will remove all repo:tag except with running a container. Actually it is a nature of "docker rmi".
- 如果图像有多个repo:标记引用到它,它将删除所有的repo:标签,除了运行一个容器。实际上,这是“docker rmi”的本质。
- Many error message will be show on screen, and you can decide to
2>/dev/null
or not - 许多错误消息将在屏幕上显示,您可以决定2个>/dev/null。
- Learn something from docker-gc, and fix its problem (it can not remove image that has mutliple repo:tag)
- 从docker-gc学习一些东西,并修复它的问题(它不能删除有mutliple repo:标记的图像)
#15
7
Use:
使用:
docker rm -f $(docker ps -a -q)
It forcefully stops and removes all containers present locally.
它强制停止并移除本地的所有容器。
#16
6
So, personally I recommend doing this as part of your deploy script for both images and containers keeping only the most recent n containers and images. I tag my docker images with the same versioning schema I use with git tag
as well as always tagging the latest docker image with "latest." This means that without cleaning up anything, my docker images wind up looking like:
因此,我个人建议您将此作为部署脚本的一部分,用于图像和容器,只保留最近的n个容器和图像。我用与git标记相同的版本控制模式标记我的docker映像,并始终用“最新”标记最新的docker映像。这意味着在不清理任何东西的情况下,我的docker图像最终会变成:
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
some_repo/some_image 0.0.5 8f1a7c7ba93c 23 hours ago 925.4 MB
some_repo/some_image latest 8f1a7c7ba93c 23 hours ago 925.4 MB
some_repo/some_image 0.0.4 0beabfa514ea 45 hours ago 925.4 MB
some_repo/some_image 0.0.3 54302cd10bf2 6 days ago 978.5 MB
some_repo/some_image 0.0.2 0078b30f3d9a 7 days ago 978.5 MB
some_repo/some_image 0.0.1 sdfgdf0f3d9a 8 days ago 938.5 MB
Now, of course I don't want to keep all my images (or containers) going back to perpetuity on all my production boxes. I just want the last 3 or 4 for rollbacks and to get rid of everything else. Unix's tail
is your best friend here. Since docker images
and docker ps
both order by date, we can just use tail
to select all but the top 3 and remove them:
现在,当然我不想把所有的图像(或容器)保留在我所有的生产箱上。我只需要最后3或4个回滚,然后去掉其他的。Unix的尾巴是这里最好的朋友。由于docker图像和docker都是按日期顺序排列的,所以我们可以只使用tail来选择除前3之外的所有内容,然后删除它们:
docker rmi $(docker images -q | tail -n +4)
Run that along with your deploy scripts (or locally) to always keep just enough images to comfortably roll back without taking up too much room or cluttering stuff up with old images.
在你的部署脚本(或本地)中运行它,以便总是保持足够的图像以舒适地回滚,而不会占用太多的空间,或者用旧的图像处理杂乱的东西。
Personally, I only keep one container on my production box at any time, but you can do the same sort of thing with containers if you want more:
就我个人而言,我每次只在我的生产箱上放一个容器,但是如果你想要更多的话,你可以用容器做同样的事情:
docker rm $(docker ps -aq | tail -n +4)
Finally, in my simplified example we're only dealing with one repo at a time, but if you had more, you can just get a bit more sophisticated with the same idea. Say I just want to keep the last 3 images from some_repo/some_image. I can just mix in grep
and awk
and be on my way:
最后,在我的简化的例子中,我们只处理一次一个repo,但是如果你有更多,你可以用同样的想法变得更复杂一些。比方说,我只想把最后3个图像保存在some_repo/some_image中。我可以混入grep和awk,然后上路:
docker rmi $(docker images -a | grep 'some_repo/some_image' | awk '{print $3}' | tail -n +4)
Again, same idea applies to containers, but you get it by this point so I'll stop giving examples.
同样的道理也适用于容器,但你可以从这一点得到它所以我不再举例子了。
#17
5
Remove all stopped containers.
删除所有停止容器。
sudo docker rm $(sudo docker ps -a -q)
sudo docker rm $(sudo docker ps -a -q)
This will remove all stopped containers by getting a list of all containers with docker ps -a -q and passing their ids to docker rm. This should not remove any running containers, and it will tell you it can’t remove a running image.
这将删除所有已停止的容器,通过获取docker -a -q的所有容器的列表,并将它们的id传递给docker rm。这不能删除任何运行的容器,它会告诉您它不能删除运行的映像。
Remove all untagged images
删除所有未加标签的图片
Now you want to clean up old images to save some space.
现在你需要清理旧图片以节省空间。
sudo docker rmi $(sudo docker images -q --filter "dangling=true")
sudo docker rmi $(sudo docker图像-q -过滤器“悬挂=true”)
#18
4
Remove 5 oldest containers:
删除5古老的容器:
docker rm `docker ps -aq | tail -n 5`
See how many containers there are left:
看看还有多少个集装箱:
docker ps -aq | wc -l
#19
4
New way: spotify/docker-gc play the trick.
新方法:spotify/docker-gc玩这个把戏。
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v /etc:/etc spotify/docker-gc
- Containers that exited more than an hour ago are removed.
- 一个多小时前离开的集装箱被移走了。
- Images that don't belong to any remaining container after that are removed
- 删除后不属于任何剩余容器的图像。
It has supported environmental settings
它支持环境设置。
Forcing deletion of images that have multiple tags
强制删除具有多个标记的图像。
FORCE_IMAGE_REMOVAL=1
Forcing deletion of containers
强制删除容器
FORCE_CONTAINER_REMOVAL=1
Excluding Recently Exited Containers and Images From Garbage Collection
不包括最近从垃圾收集中提取的容器和图像。
GRACE_PERIOD_SECONDS=86400
This setting also prevents the removal of images that have been created less than GRACE_PERIOD_SECONDS seconds ago.
这个设置还可以防止图像的删除,而这些图像是在几秒之前创建的。
Dry run
排练
DRY_RUN=1
Cleaning up orphaned container volumes CLEAN_UP_VOLUMES=1
清理孤立的容器卷,clean_up_volume =1。
Reference: docker-gc
参考:docker-gc
Old way to do:
老方法:
delete old, non-running containers
删除旧的、跑步的容器中
docker ps -a -q -f status=exited | xargs --no-run-if-empty docker rm
OR
docker rm $(docker ps -a -q)
delete all images associated with non-running docker containers
删除与不运行的docker容器相关的所有图像。
docker images -q | xargs --no-run-if-empty docker rmi
cleanup orphaned docker volumes for docker version 1.10.x and above
为docker版本1.10清除孤立的docker卷。x和上面
docker volume ls -qf dangling=true | xargs -r docker volume rm
Based on time period
基于时间
docker ps -a | grep "weeks ago" | awk "{print $1}" | xargs --no-run-if-empty docker rm
docker ps -a | grep "days ago" | awk "{print $1}" | xargs --no-run-if-empty docker rm
docker ps -a | grep "hours ago" | awk "{print $1}" | xargs --no-run-if-empty docker rm
#20
4
You can use the following command to remove the exited containers:
您可以使用以下命令删除已退出的容器:
docker rm $(sudo docker ps -a | grep Exit | cut -d ' ' -f 1)
Here is the full gist to also remove the old images on docker: Gist to remove old Docker containers and images.
这里有完整的要点,也可以删除docker上的旧图像:删除旧的docker容器和图像的要点。
#21
3
#!/bin/bash
# docker-gc --- Remove stopped docker containers
RUNNING=$(docker ps -q)
ALL=$(docker ps -a -q)
for container in $ALL ; do
[[ "$RUNNING" =~ "$container" ]] && continue
echo Removing container: $(docker rm $container)
done
#22
3
I always use docker rmi $(docker ps -a -q)
to remove all images.
我总是使用docker rmi $(docker ps -a -q)来删除所有图像。
You can remove directory /var/lib/docker/graph
when docker rmi
failed.
当docker rmi失败时,您可以删除目录/var/lib/docker/graph。
#23
3
-
Remove all docker processes:
删除所有码头工人过程:
docker rm $(docker ps -a -q)
-
Remove specific container:
删除特定的容器:
$ docker ps -a (lists all old containers) $ docker rm container-Id
#24
3
You can remove only stopped containers. Stop all of them in the beginning
您可以只删除已停止的容器。一开始就把它们都停下来。
docker stop $(docker ps -a -q)
docker停止$(docker ps -a -q)
Then you can remove
然后您可以删除
docker rm $(docker ps -a -q)
docker rm $(docker psa -q)
#25
2
To remove ALL containers without crying:
移除所有的容器而不哭泣:
sudo docker ps -a | grep -v CONTAINER | awk '{print $1}' | xargs --no-run-if-empty sudo docker rm -f
#26
2
For anyone interested, I took the example from qkrijger and turned it into a clear all (stop and remove all)
对于任何感兴趣的人,我从qkrijger拿了一个例子,把它变成了一个清晰的(停止和删除所有)
docker stop `docker ps --no-trunc -aq` ; docker rm `docker ps --no-trunc -aq`
#27
2
docker rm --force `docker ps -qa`
#28
2
Remove all containers created from a certain image:
docker rm $(docker ps -a | awk '/myimage:mytag/{print $1}')
#29
2
On Ubuntu 14.04 (Trusty Tahr):
在Ubuntu 14.04 (Trusty Tahr):
$ for CON in `docker ps -qa`; do docker rm $CON ; done
This is just a normal Bash command so it should work with EVERY Bash-compliant terminal.
这只是一个普通的Bash命令,因此它应该与每个符合Bash的终端一起工作。
#30
2
if you want to automatically/periodically clean up exited containers and remove images and volumes that aren't in use by a running container you can download this image meltwater/docker-cleanup.
如果您想要自动/定期清理退出的容器,并删除运行容器中不使用的图像和卷,您可以下载这张图像文件/docker-cleanup。
I use this on production since we deploy several times a day on multiple servers, and i don't want to go to every server to clean up ( that would be a pain).
我在生产中使用这个,因为我们每天在多台服务器上部署好几次,我不希望每个服务器都清理(这将是一种痛苦)。
Just run:
运行:
docker run -d -v /var/run/docker.sock:/var/run/docker.sock:rw -v /var/lib/docker:/var/lib/docker:rw --restart=unless-stopped meltwater/docker-cleanup:latest
It will run every 30mins by default (or however long you set it using DELAY_TIME=1800 option) and clean up exited containers and images.
默认情况下,它将每30分钟运行一次(或者使用DELAY_TIME=1800选项设置它),并清理退出的容器和图像。
More details: https://github.com/meltwater/docker-cleanup/blob/master/README.md
更多信息:https://github.com/meltwater/docker-cleanup/blob/master/README.md。
#1
1033
There is a new feature in Docker 1.13.x called Docker container prune: docker container prune
This will do what you want and should work on all platforms the same way.
Docker 1.13中有一个新特性。x被称为Docker容器prune: Docker容器prune这将做你想做的事情,并且应该在所有平台上使用相同的方法。
There is also a docker system prune: docker system prune
, which will clean up containers, images, volumes, and networks all in one command.
还有一个docker系统prune: docker系统prune,它可以在一个命令中清理容器、图像、卷和网络。
Original Answer:
最初的回答:
There has been some talk about a Docker cleanup command. You can find the information on this ticket: https://github.com/dotcloud/docker/issues/928
有一些关于Docker清理命令的讨论。您可以在这张彩票上找到相关信息:https://github.com/dotcloud/docker/es/928。
Until that command is available, you can string Docker commands together with other Unix commands to get what you need. Here is an example on how to clean up old containers that are weeks old:
在该命令可用之前,您可以与其他Unix命令一起使用字符串Docker命令来获得所需的内容。下面是一个如何清理几周大的旧容器的例子:
$ docker ps --filter "status=exited" | grep 'weeks ago' | awk '{print $1}' | xargs --no-run-if-empty docker rm
To give credit, where it is due, this example is from https://twitter.com/jpetazzo/status/347431091415703552.
为了给它提供信用,这个例子来自https://twitter.com/jpetazzo/status/347431091415703552。
#2
570
Another method, which I got from Guillaume J. Charmes (credit where it is due):
另一种方法,是我从Guillaume J. Charmes那里得到的:
docker rm `docker ps --no-trunc -aq`
will remove all containers in an elegant way.
将以优雅的方式移除所有容器。
And by Bartosz Bilicki, for Windows:
以及Bartosz Bilicki,为Windows:
FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i
For PowerShell:
PowerShell:
docker rm @(docker ps -aq)
An update with Docker 1.13 (Q4 2016), credit to VonC (later in this thread):
与Docker 1.13(2016年Q4)的更新,VonC(稍后在此线程中):
docker system prune
will delete ALL unused data (i.e., in order: containers stopped, volumes without containers and images with no containers).
docker系统prune将删除所有未使用的数据(即:,顺序是:容器停止,体积没有容器,图像没有容器。
See PR 26108 and commit 86de7c0, which are introducing 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.
请参阅PR 26108和提交86de7c0,它将引入一些新的命令,以帮助可视化Docker守护进程数据在磁盘上占用了多少空间,并允许轻松清除“不必要的”过量。
docker system prune
WARNING! This will remove:
- all stopped containers
- all volumes not used by at least one container
- all images without at least one container associated to them
Are you sure you want to continue? [y/N] y
#3
374
Updated Answer Use docker system prune
or docker container prune
now. See VonC's updated answer: https://*.com/a/39860665/411229
更新的答案使用docker系统prune或docker容器现在。参见VonC更新的答案:https://*.com/a/39860665/411229。
Previous Answer Composing several different hints above, the most elegant way to remove all non-running containers seems to be:
前面的答案组合了几个不同的提示,最优雅的方法是删除所有非运行的容器:
docker rm $(docker ps -q -f status=exited)
docker rm $(docker ps -q -f状态=退出)
-
-q
prints just the container ids (without column headers) - -q只打印容器id(没有列标题)
-
-f
allows you to filter your list of printed containers (in this case we are filtering to only show exited containers) - -f允许您过滤您的打印容器列表(在本例中,我们过滤的只是显示已退出的容器)
#4
238
The official way is:
官方的方式是:
docker rm `docker ps -aq`
The Docker maintainers have indicated there will be no command for this - and you compose the commands like that:
Docker维护者表示将不会对此进行命令,您可以编写如下命令:
We have discussed this before and prefer users to use the above line without having to add additional code to Docker.
我们之前已经讨论过这个问题,并且希望用户可以使用上面的代码,而不需要向Docker添加额外的代码。
#5
98
It is now possible to use filtering with docker ps
:
现在可以使用docker ps进行过滤:
docker rm $(docker ps -q -f status=exited)
And for images:
和图片:
docker rmi $(docker images -q -f "dangling=true")
However, any of those will cause docker rm
or docker rmi
to throw an error when there are no matching containers. The older docker rm $(docker ps -aq)
trick was even worse as it tried to remove any running container, failing at each one.
但是,如果没有匹配的容器,其中任何一个都会导致docker rm或docker rmi抛出错误。更老的docker rm $(docker ps -aq)技巧更糟糕,因为它试图删除任何运行的容器,每一个都失败了。
Here's a cleaner script to add in your ~/.bashrc
or ~/.profile
:
这里有一个更清晰的脚本可以添加到你的~/中。bashrc或(~ /。简介:
# Use `docker-cleanup --dry-run` to see what would be deleted.
function docker-cleanup {
EXITED=$(docker ps -q -f status=exited)
DANGLING=$(docker images -q -f "dangling=true")
if [ "$1" == "--dry-run" ]; then
echo "==> Would stop containers:"
echo $EXITED
echo "==> And images:"
echo $DANGLING
else
if [ -n "$EXITED" ]; then
docker rm $EXITED
else
echo "No containers to remove."
fi
if [ -n "$DANGLING" ]; then
docker rmi $DANGLING
else
echo "No images to remove."
fi
fi
}
Edit: As noted below, original answer was for removing images, not containers. Updated to answer both, including new links to documentation. Thanks to Adrian (and Ryan's answer) for mentioning the new ps
filtering.
编辑:如下所述,原始答案是删除图片,而不是容器。更新的答案,包括新的文档链接。感谢Adrian(和Ryan的回答)提到了新的ps过滤。
#6
97
With Docker 1.13 (Q4 2016), you now have:
与Docker 1.13 (Q4 2016),您现在有:
docker system prune
will delete ALL unused data (i.e., in order: containers stopped, volumes without containers and images with no containers).
docker系统prune将删除所有未使用的数据(即:,顺序是:容器停止,体积没有容器,图像没有容器。
See PR 26108 and commit 86de7c0, which are introducing 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.
请参阅PR 26108和提交86de7c0,它将引入一些新的命令,以帮助可视化Docker守护进程数据在磁盘上占用了多少空间,并允许轻松清除“不必要的”过量。
docker system prune
WARNING! This will remove:
- all stopped containers
- all volumes not used by at least one container
- all images without at least one container associated to them
Are you sure you want to continue? [y/N] y
As wjv comments,
wjv评论,
There is also
docker {container,image,volume,network} prune
, which may be used to remove unused instances of just one type of object.还有docker {container,image,volume,network} prune,它可以用来删除一种对象的未使用实例。
Introduced in commit 913e5cb, only for Docker 1.13+.
引入了提交913e5cb,只对Docker 1.13+。
docker container prune
#7
79
UPDATED 2017 (NEWEST)
更新2017(最新)
docker container prune
This - 2017 (OLD) way
这是2017年(旧)的方式。
To remove ALL STOPPED CONTAINERS
移除所有已停止的容器。
docker rm $(docker ps -a -q)
To remove ALL CONTAINERS (STOPPED AND NON STOPPED)
移除所有容器(停止和不停止)
docker rm -f $(docker ps -a -q)
#8
46
Remove all stopped containers:
删除所有停止容器:
docker rm $(docker ps -a | grep Exited | awk '{print $1}')
From the comment by pauk960:
从pauk960的评论:
Since version 1.3.0 you can use filters with
docker ps
, instead ofgrep Exited
usedocker ps -a -f status=exited
. And if you use-q
with it you can get container IDs only instead of full output, no need to use awk for that.从1.3.0版本开始,你可以使用docker ps的过滤器,而不是grep退出使用docker ps - - -f状态=退出。如果你使用-q,你只能得到容器id而不是全部输出,没有必要使用awk。
#9
30
If you do not like to remove all containers, you can select all containers created before or after a specific container with docker ps --before <container-ID>
or with docker ps --since <container-ID>
. This feature is at least in docker version 0.6.5.
如果您不喜欢删除所有容器,您可以选择在特定容器之前或之后使用docker ps创建的所有容器——在 <容器id> 或使用docker ps之前——因为 <容器id> 。该特性至少在docker版本0.6.5中。
Let's say You have developed your system, now it is working but there are a number of containers left. You want to remove containers created before that working version. Determine the ID of the working container with docker ps
.
假设你已经开发了你的系统,现在它在工作,但是还有很多的容器。您希望删除在该工作版本之前创建的容器。使用docker ps确定工作容器的ID。
Remove containers created before an other container
删除在另一个容器之前创建的容器。
docker rm $(docker ps --before 9c49c11c8d21 -q)
Another example. You have your database already running on a docker container. You have developed your application to run on an other container and now You have a number of unneeded containers.
另一个例子。您的数据库已经在docker容器上运行了。您已经开发了在另一个容器上运行的应用程序,现在您有了一些不需要的容器。
Remove containers created after a certain container
删除在某个容器后创建的容器。
docker rm $(docker ps --since a6ca4661ec7f -q)
Docker stores containers in /var/lib/docker/containers
in Ubuntu. I think extra containers do no other harm, but take up disk space.
Docker在Ubuntu中存储/var/lib/docker/container中的容器。我认为额外的容器没有其他的危害,但是占用磁盘空间。
#10
29
Update: As of Docker version 1.13 (released January 2017), you can issue the following command to clean up stopped containers, unused volumes, dangling images and unused networks:
更新:在Docker版本1.13(2017年1月发布)中,您可以发出以下命令来清理停止的容器、未使用的卷、悬空图像和未使用的网络:
docker system prune
If you want to insure that you're only deleting containers which have an exited
status, use this:
如果您想确保只删除具有退出状态的容器,请使用以下内容:
docker ps -aq -f status=exited | xargs docker rm
Similarly, if you're cleaning up docker stuff, you can get rid of untagged, unnamed images in this way:
类似地,如果你正在清理docker的东西,你可以用这种方法清除未标记的、未命名的图像:
docker images -q --no-trunc -f dangling=true | xargs docker rmi
#11
19
Here is my docker-cleanup script, which removes untagged containers and images. Please check the source for any updates.
这里是我的docker-cleanup脚本,它删除未标记的容器和图像。请检查源代码是否有任何更新。
#!/bin/sh
# Cleanup docker files: untagged containers and images.
#
# Use `docker-cleanup -n` for a dry run to see what would be deleted.
untagged_containers() {
# Print containers using untagged images: $1 is used with awk's print: 0=line, 1=column 1.
docker ps -a | awk '$2 ~ "[0-9a-f]{12}" {print $'$1'}'
}
untagged_images() {
# Print untagged images: $1 is used with awk's print: 0=line, 3=column 3.
# NOTE: intermediate images (via -a) seem to only cause
# "Error: Conflict, foobarid wasn't deleted" messages.
# Might be useful sometimes when Docker messed things up?!
# docker images -a | awk '$1 == "<none>" {print $'$1'}'
docker images | tail -n +2 | awk '$1 == "<none>" {print $'$1'}'
}
# Dry-run.
if [ "$1" = "-n" ]; then
echo "=== Containers with uncommitted images: ==="
untagged_containers 0
echo
echo "=== Uncommitted images: ==="
untagged_images 0
exit
fi
# Remove containers with untagged images.
echo "Removing containers:" >&2
untagged_containers 1 | xargs --no-run-if-empty docker rm --volumes=true
# Remove untagged images
echo "Removing images:" >&2
untagged_images 3 | xargs --no-run-if-empty docker rmi
Source: https://github.com/blueyed/dotfiles/blob/master/usr/bin/docker-cleanup
来源:https://github.com/blueyed/dotfiles/blob/master/usr/bin/docker-cleanup
#12
10
First, stop running containers before attempting to remove them
首先,在尝试删除容器之前停止运行容器。
Remove Running containers
docker rm $(docker stop -t=1 $(docker ps -q))
You could use kill
instead of stop
. In my case I prefer stop
since I tend to rerun them vs creating a new one every time so I try to shut them down nicely.
你可以用kill代替stop。在我的例子中,我喜欢停下来,因为我倾向于重新运行它们,每次都创建一个新的,所以我试着很好地关闭它们。
Note: Trying to stop a container will give you an error:
注意:试图阻止一个容器会给你一个错误:
Error: Impossible to remove a running container, please stop it first
错误:无法删除正在运行的容器,请先停止。
Remove All containers
docker rm $(docker ps -a -q)
#13
10
Removing all containers from Windows shell:
从Windows shell中删除所有容器:
FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i
#14
7
https://github.com/HardySimpson/docker-cleanup
https://github.com/HardySimpson/docker-cleanup
Docker cleanup
A tiny all-in-one shell, which removes:
一种小型的全合一的外壳,可以移除:
- Containers that not running more than one day ago
- 不超过一天前运行的容器。
- Images that don't belong to any remaining container
- 不属于任何剩余容器的图像。
Intend to run as a crontab job
打算以crontab的方式运行。
Feature
- It will remove all
<none>:<none>
images - 它将删除所有 <没有> : <没有> 图像。
- If the image has multiple repo:tag references to it, it will remove all repo:tag except with running a container. Actually it is a nature of "docker rmi".
- 如果图像有多个repo:标记引用到它,它将删除所有的repo:标签,除了运行一个容器。实际上,这是“docker rmi”的本质。
- Many error message will be show on screen, and you can decide to
2>/dev/null
or not - 许多错误消息将在屏幕上显示,您可以决定2个>/dev/null。
- Learn something from docker-gc, and fix its problem (it can not remove image that has mutliple repo:tag)
- 从docker-gc学习一些东西,并修复它的问题(它不能删除有mutliple repo:标记的图像)
#15
7
Use:
使用:
docker rm -f $(docker ps -a -q)
It forcefully stops and removes all containers present locally.
它强制停止并移除本地的所有容器。
#16
6
So, personally I recommend doing this as part of your deploy script for both images and containers keeping only the most recent n containers and images. I tag my docker images with the same versioning schema I use with git tag
as well as always tagging the latest docker image with "latest." This means that without cleaning up anything, my docker images wind up looking like:
因此,我个人建议您将此作为部署脚本的一部分,用于图像和容器,只保留最近的n个容器和图像。我用与git标记相同的版本控制模式标记我的docker映像,并始终用“最新”标记最新的docker映像。这意味着在不清理任何东西的情况下,我的docker图像最终会变成:
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
some_repo/some_image 0.0.5 8f1a7c7ba93c 23 hours ago 925.4 MB
some_repo/some_image latest 8f1a7c7ba93c 23 hours ago 925.4 MB
some_repo/some_image 0.0.4 0beabfa514ea 45 hours ago 925.4 MB
some_repo/some_image 0.0.3 54302cd10bf2 6 days ago 978.5 MB
some_repo/some_image 0.0.2 0078b30f3d9a 7 days ago 978.5 MB
some_repo/some_image 0.0.1 sdfgdf0f3d9a 8 days ago 938.5 MB
Now, of course I don't want to keep all my images (or containers) going back to perpetuity on all my production boxes. I just want the last 3 or 4 for rollbacks and to get rid of everything else. Unix's tail
is your best friend here. Since docker images
and docker ps
both order by date, we can just use tail
to select all but the top 3 and remove them:
现在,当然我不想把所有的图像(或容器)保留在我所有的生产箱上。我只需要最后3或4个回滚,然后去掉其他的。Unix的尾巴是这里最好的朋友。由于docker图像和docker都是按日期顺序排列的,所以我们可以只使用tail来选择除前3之外的所有内容,然后删除它们:
docker rmi $(docker images -q | tail -n +4)
Run that along with your deploy scripts (or locally) to always keep just enough images to comfortably roll back without taking up too much room or cluttering stuff up with old images.
在你的部署脚本(或本地)中运行它,以便总是保持足够的图像以舒适地回滚,而不会占用太多的空间,或者用旧的图像处理杂乱的东西。
Personally, I only keep one container on my production box at any time, but you can do the same sort of thing with containers if you want more:
就我个人而言,我每次只在我的生产箱上放一个容器,但是如果你想要更多的话,你可以用容器做同样的事情:
docker rm $(docker ps -aq | tail -n +4)
Finally, in my simplified example we're only dealing with one repo at a time, but if you had more, you can just get a bit more sophisticated with the same idea. Say I just want to keep the last 3 images from some_repo/some_image. I can just mix in grep
and awk
and be on my way:
最后,在我的简化的例子中,我们只处理一次一个repo,但是如果你有更多,你可以用同样的想法变得更复杂一些。比方说,我只想把最后3个图像保存在some_repo/some_image中。我可以混入grep和awk,然后上路:
docker rmi $(docker images -a | grep 'some_repo/some_image' | awk '{print $3}' | tail -n +4)
Again, same idea applies to containers, but you get it by this point so I'll stop giving examples.
同样的道理也适用于容器,但你可以从这一点得到它所以我不再举例子了。
#17
5
Remove all stopped containers.
删除所有停止容器。
sudo docker rm $(sudo docker ps -a -q)
sudo docker rm $(sudo docker ps -a -q)
This will remove all stopped containers by getting a list of all containers with docker ps -a -q and passing their ids to docker rm. This should not remove any running containers, and it will tell you it can’t remove a running image.
这将删除所有已停止的容器,通过获取docker -a -q的所有容器的列表,并将它们的id传递给docker rm。这不能删除任何运行的容器,它会告诉您它不能删除运行的映像。
Remove all untagged images
删除所有未加标签的图片
Now you want to clean up old images to save some space.
现在你需要清理旧图片以节省空间。
sudo docker rmi $(sudo docker images -q --filter "dangling=true")
sudo docker rmi $(sudo docker图像-q -过滤器“悬挂=true”)
#18
4
Remove 5 oldest containers:
删除5古老的容器:
docker rm `docker ps -aq | tail -n 5`
See how many containers there are left:
看看还有多少个集装箱:
docker ps -aq | wc -l
#19
4
New way: spotify/docker-gc play the trick.
新方法:spotify/docker-gc玩这个把戏。
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v /etc:/etc spotify/docker-gc
- Containers that exited more than an hour ago are removed.
- 一个多小时前离开的集装箱被移走了。
- Images that don't belong to any remaining container after that are removed
- 删除后不属于任何剩余容器的图像。
It has supported environmental settings
它支持环境设置。
Forcing deletion of images that have multiple tags
强制删除具有多个标记的图像。
FORCE_IMAGE_REMOVAL=1
Forcing deletion of containers
强制删除容器
FORCE_CONTAINER_REMOVAL=1
Excluding Recently Exited Containers and Images From Garbage Collection
不包括最近从垃圾收集中提取的容器和图像。
GRACE_PERIOD_SECONDS=86400
This setting also prevents the removal of images that have been created less than GRACE_PERIOD_SECONDS seconds ago.
这个设置还可以防止图像的删除,而这些图像是在几秒之前创建的。
Dry run
排练
DRY_RUN=1
Cleaning up orphaned container volumes CLEAN_UP_VOLUMES=1
清理孤立的容器卷,clean_up_volume =1。
Reference: docker-gc
参考:docker-gc
Old way to do:
老方法:
delete old, non-running containers
删除旧的、跑步的容器中
docker ps -a -q -f status=exited | xargs --no-run-if-empty docker rm
OR
docker rm $(docker ps -a -q)
delete all images associated with non-running docker containers
删除与不运行的docker容器相关的所有图像。
docker images -q | xargs --no-run-if-empty docker rmi
cleanup orphaned docker volumes for docker version 1.10.x and above
为docker版本1.10清除孤立的docker卷。x和上面
docker volume ls -qf dangling=true | xargs -r docker volume rm
Based on time period
基于时间
docker ps -a | grep "weeks ago" | awk "{print $1}" | xargs --no-run-if-empty docker rm
docker ps -a | grep "days ago" | awk "{print $1}" | xargs --no-run-if-empty docker rm
docker ps -a | grep "hours ago" | awk "{print $1}" | xargs --no-run-if-empty docker rm
#20
4
You can use the following command to remove the exited containers:
您可以使用以下命令删除已退出的容器:
docker rm $(sudo docker ps -a | grep Exit | cut -d ' ' -f 1)
Here is the full gist to also remove the old images on docker: Gist to remove old Docker containers and images.
这里有完整的要点,也可以删除docker上的旧图像:删除旧的docker容器和图像的要点。
#21
3
#!/bin/bash
# docker-gc --- Remove stopped docker containers
RUNNING=$(docker ps -q)
ALL=$(docker ps -a -q)
for container in $ALL ; do
[[ "$RUNNING" =~ "$container" ]] && continue
echo Removing container: $(docker rm $container)
done
#22
3
I always use docker rmi $(docker ps -a -q)
to remove all images.
我总是使用docker rmi $(docker ps -a -q)来删除所有图像。
You can remove directory /var/lib/docker/graph
when docker rmi
failed.
当docker rmi失败时,您可以删除目录/var/lib/docker/graph。
#23
3
-
Remove all docker processes:
删除所有码头工人过程:
docker rm $(docker ps -a -q)
-
Remove specific container:
删除特定的容器:
$ docker ps -a (lists all old containers) $ docker rm container-Id
#24
3
You can remove only stopped containers. Stop all of them in the beginning
您可以只删除已停止的容器。一开始就把它们都停下来。
docker stop $(docker ps -a -q)
docker停止$(docker ps -a -q)
Then you can remove
然后您可以删除
docker rm $(docker ps -a -q)
docker rm $(docker psa -q)
#25
2
To remove ALL containers without crying:
移除所有的容器而不哭泣:
sudo docker ps -a | grep -v CONTAINER | awk '{print $1}' | xargs --no-run-if-empty sudo docker rm -f
#26
2
For anyone interested, I took the example from qkrijger and turned it into a clear all (stop and remove all)
对于任何感兴趣的人,我从qkrijger拿了一个例子,把它变成了一个清晰的(停止和删除所有)
docker stop `docker ps --no-trunc -aq` ; docker rm `docker ps --no-trunc -aq`
#27
2
docker rm --force `docker ps -qa`
#28
2
Remove all containers created from a certain image:
docker rm $(docker ps -a | awk '/myimage:mytag/{print $1}')
#29
2
On Ubuntu 14.04 (Trusty Tahr):
在Ubuntu 14.04 (Trusty Tahr):
$ for CON in `docker ps -qa`; do docker rm $CON ; done
This is just a normal Bash command so it should work with EVERY Bash-compliant terminal.
这只是一个普通的Bash命令,因此它应该与每个符合Bash的终端一起工作。
#30
2
if you want to automatically/periodically clean up exited containers and remove images and volumes that aren't in use by a running container you can download this image meltwater/docker-cleanup.
如果您想要自动/定期清理退出的容器,并删除运行容器中不使用的图像和卷,您可以下载这张图像文件/docker-cleanup。
I use this on production since we deploy several times a day on multiple servers, and i don't want to go to every server to clean up ( that would be a pain).
我在生产中使用这个,因为我们每天在多台服务器上部署好几次,我不希望每个服务器都清理(这将是一种痛苦)。
Just run:
运行:
docker run -d -v /var/run/docker.sock:/var/run/docker.sock:rw -v /var/lib/docker:/var/lib/docker:rw --restart=unless-stopped meltwater/docker-cleanup:latest
It will run every 30mins by default (or however long you set it using DELAY_TIME=1800 option) and clean up exited containers and images.
默认情况下,它将每30分钟运行一次(或者使用DELAY_TIME=1800选项设置它),并清理退出的容器和图像。
More details: https://github.com/meltwater/docker-cleanup/blob/master/README.md
更多信息:https://github.com/meltwater/docker-cleanup/blob/master/README.md。