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?
Use:
It forcefully stops and removes all containers present locally.
Remove all containers created from a certain image:
I always use
docker rmi $(docker ps -a -q)
to remove all images.You can remove directory
/var/lib/docker/graph
whendocker rmi
failed.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:
To give credit, where it is due, this example is from https://twitter.com/jpetazzo/status/347431091415703552.
Remove all stopped containers:
From the comment by pauk960:
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 withdocker 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
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 stores containers in
/var/lib/docker/containers
in Ubuntu. I think extra containers do no other harm, but take up disk space.