how do I clean up my docker host machine

Richard picture Richard · Apr 21, 2014 · Viewed 17.2k times · Source

As I create/debug a docker image/container docker seems to be leaving all sorts of artifacts on my system. (at one point there was a 48 image limit) But the last time I looked there were 20-25 images; docker images.

So the overarching questions are:

  • how does one properly cleanup?
  • as I was manually deleting images more started to arrive. huh?
  • how much disk space should I really allocate to the host?
  • will running daemons really restart after the next reboot?

and the meta question... what questions have I not asked that need to be?

Answer

Mark O'Connor picture Mark O'Connor · Apr 21, 2014

Here's how I periodically purge my docker host:

Kill running containers:

docker kill $(docker ps -qa)

Delete all containers (and their associated volumes):

docker rm -v $(docker ps -qa)

Remove all images:

docker rmi $(docker images -q)

Update

Delete only the containers that are not running. Parse the "ps" output for the "Exited" string:

docker ps -a | awk '/Exited/ {print $1}' | xargs docker rm -v

Not perfect... Don't give your container the name "Exited" :-)

Update

Docker 1.9 has a new volume command that can be used to purge old volumes

docker volume rm $(docker volume ls -qf dangling=true)

Update

Latest community edition of docker has a new "system prune" command

docker system prune --volumes

This purged unused networks, kill stopped containers, dangling images and any unused volumes.