how to clean up docker overlay directory?

We just started having this problem, and btafarelo's answer got me part of the way, or at least made me feel better about removing the sha256 entries.

System info: ec2 instances running CoreOS 1.12 behind an ELB

  • Drain the docker instance from the ELB
  • Shutdown docker

    systemctl stop docker
    rm -rf /var/lib/docker/overlay/*
    
  • Execute the results of the commands

    for d in $(find /var/lib/docker/image/overlay -type d -name '*sha256*'); do echo rm -rf $d/* ; done
    
  • reboot (easiest way to bring everything back up)

This recovered about 25% of the disk after the services restarted with no ill side affects.


I have added this to bashrc in my dev environment, and gotten used to running it every day or so.

function cleanup_docker() {
  docker ps -f status=exited -q | xargs -r docker rm
  docker images -f dangling=true -q | xargs -r docker rmi
}

In some cases, the following script can free up more space, as it will try to remove all images, and just fail silently:

function cleanup_docker_aggressive() {
  for i in $(docker images --no-trunc -q | sort -u)
  do
    docker rmi $i 2> /dev/null
  done
}

Sadly, they're not significantly cleaner than your solution.

EDIT: Starting with Docker 1.13, you can use docker system:

docker system df    # to check what is using space
docker system prune # cleans up also networks, build cache, etc

EDIT: Starting with Docker 2017.09, you can also use container and image

docker container prune
docker image prune -a

the latter you can use with fancy filters like --filter "until=24h"


Here's the hacky way I'm doing this right now. I'm not going to accept it as an answer because I'm hoping there's a better way.

# delete old docker processes
docker rm `docker ps -a | grep Exited | awk '{print $1 }'`
  ignore_errors: true

# delete old images. will complain about still-in-use images.
docker rmi `docker images -aq`

From our side we used:

sudo docker system prune -a -f

Which saved me 3Go!

We also used the famous commands:

sudo docker rm -v $(sudo docker ps -a -q -f status=exited)
sudo docker rmi -f  $(sudo docker images -f "dangling=true" -q)
docker volume ls -qf dangling=true | xargs -r docker volume rm

We put that on cron to manage a little bit more efficently our disk space.

Reference: https://forums.docker.com/t/some-way-to-clean-up-identify-contents-of-var-lib-docker-overlay/30604/4