How to remove old Docker containers

2019-01-03 19:18发布

This question is related to Should I be concerned about excess, non-running, Docker containers?.

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).

Maybe there is a directory in which these containers are stored where I can delete them easily manually?

标签: docker
30条回答
萌系小妹纸
2楼-- · 2019-01-03 19:32

Use:

docker rm -f $(docker ps -a -q)

It forcefully stops and removes all containers present locally.

查看更多
Bombasti
3楼-- · 2019-01-03 19:32

Remove all containers created from a certain image:

docker rm  $(docker ps -a | awk '/myimage:mytag/{print $1}')
查看更多
我想做一个坏孩纸
4楼-- · 2019-01-03 19:33

I always use docker rmi $(docker ps -a -q) to remove all images.

You can remove directory /var/lib/docker/graph when docker rmi failed.

查看更多
Ridiculous、
5楼-- · 2019-01-03 19:34

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.

There is also a Docker system prune: docker system prune, which will clean up containers, images, volumes, and networks all in one command.

Original Answer:

There has been some talk about a Docker cleanup command. You can find the information on this ticket: Implement a 'clean' command (#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:

$ 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.

查看更多
ゆ 、 Hurt°
6楼-- · 2019-01-03 19:35

Remove all stopped containers:

docker rm $(docker ps -a | grep Exited | awk '{print $1}')

From the comment by pauk960:

Since version 1.3.0 you can use filters with docker ps, instead of grep Exited use docker 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.

查看更多
Bombasti
7楼-- · 2019-01-03 19:35

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.

Let's say you have developed your system, and 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.

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 another container and now you have a number of unneeded containers.

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.

查看更多
登录 后发表回答