How to remove old Docker containers
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?
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: https://github.com/dotcloud/docker/issues/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.
Another method, which I got from Guillaume J. Charmes (credit where it is due):
docker rm `docker ps --no-trunc -aq`
will remove all containers in an elegant way.
And by Bartosz Bilicki, for Windows:
FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i
For PowerShell:
docker rm @(docker ps -aq)
An update with Docker 1.13 (Q4 2016), credit to VonC (later in this thread):
docker system prune
will delete ALL unused data (ie, 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.
docker system prune
WARNING! This will remove:
- all stopped containers
- all volumes not used by at least one container
- all images without at least one container associated to them
Are you sure you want to continue? [y/N] y
Updated Answer Use docker system prune
or docker container prune
now. See VonC's updated answer: https://stackoverflow.com/a/39860665/411229
Previous Answer Composing several different hints above, the most elegant way to remove all non-running containers seems to be:
docker rm $(docker ps -q -f status=exited)
-q
prints just the container ids (without column headers) -f
allows you to filter your list of printed containers (in this case we are filtering to only show exited containers) 上一篇: 如何在Docker中移除图像?
下一篇: 如何删除旧的Docker容器