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?
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:
If you want to insure that you're only deleting containers which have an
exited
status, use this:Similarly, if you're cleaning up docker stuff, you can get rid of untagged, unnamed images in this way:
Remove all docker processes:
Remove specific container:
With Docker 1.13 (Q4 2016), you now have:
docker system prune -a
will delete ALL unused data (i.e., in order: containers stopped, volumes without containers and images with no containers).docker system prune
without -a will remove (for images) only dangling images, or images without a tag, as commented by smilebomb.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.
As wjv comments,
Introduced in commit 913e5cb, only for Docker 1.13+.
Remove 5 oldest containers:
See how many containers there are left:
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: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. Sincedocker images
anddocker ps
both order by date, we can just usetail
to select all but the top three and remove them: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:
Finally, in my simplified example we're only dealing with one repository 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 three images from some_repo/some_image. I can just mix in
grep
andawk
and be on my way:Again, the same idea applies to containers, but you get it by this point so I'll stop giving examples.
Another method, which I got from Guillaume J. Charmes (credit where it is due):
will remove all containers in an elegant way.
And by Bartosz Bilicki, for Windows:
For PowerShell:
An update with Docker 1.13 (Q4 2016), credit to VonC (later in this thread):
docker system prune
will delete ALL unused data (i.e., in order: containers stopped, volumes without containers and images with no containers).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.