Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove old Docker containers

Tags:

docker

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?

like image 485
qkrijger Avatar asked Jun 21 '13 13:06

qkrijger


People also ask

How do I destroy all docker containers?

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!

How do I delete an existing container?

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.


1 Answers

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.

like image 104
Ken Cochrane Avatar answered Sep 24 '22 00:09

Ken Cochrane