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?
If you do, killing multiple containers takes one command: docker-compose down. You could also run docker-compose without detached mode. If so, you'll just use ^C to kill all containers. And there you have it—all containers killed!
Remove all exited containers To review the list of exited containers, use the -f flag to filter based on status. When you've verified you want to remove those containers, use -q to pass the IDs to the docker rm command: List: docker ps -a -f status=exited.
Since Docker 1.13.x you can use Docker container prune:
docker container prune
This will remove all stopped containers and should work on all platforms the same way.
There is also a Docker system prune:
docker system prune
which will clean up all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes, in one command.
For older Docker versions, 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With