How to clean up Docker

Docker

Docker Problem Overview


I've just noticed that I ran out of disk space on my laptop. Quite a lot is used by Docker as found by mate-disk-usage-analyzer:

Enter image description here

The docker/aufs/diff folder contains 152 folders ending in -removing.

I already ran the following commands to clean up

Kill all running containers:
# docker kill $(docker ps -q)

Delete all stopped containers
# docker rm $(docker ps -a -q)

Delete all images
# docker rmi $(docker images -q)

Remove unused data
# docker system prune

And some more
# docker system prune -af

But the screenshot was taken after I executed those commands.

What is docker/aufs/diff, why does it consume that much space and how do I clean it up?

I have Docker version 17.06.1-ce, build 874a737. It happened after a cleanup, so this is definitely still a problem.

Docker Solutions


Solution 1 - Docker

The following is a radical solution. IT DELETES ALL YOUR DOCKER STUFF. INCLUDING VOLUMES.

$ sudo su
# service docker stop
# cd /var/lib/docker
# rm -rf *
# service docker start

See https://github.com/moby/moby/issues/22207#issuecomment-295754078 for details

It might not be /var/lib/docker

The docker location might be different in your case. You can use a disk usage analyzer (such as mate-disk-usage-analyzer) to find the folders which need most space.

See Where are Docker images stored on the host machine?

Solution 2 - Docker

This dir is where container rootfs layers are stored when using the AUFS storage driver (default if the AUFS kernel modules are loaded).

If you have a bunch of *-removing dirs, this is caused by a failed removal attempt. This can happen for various reasons, the most common is that an unmount failed due to device or resource busy.

Before Docker 17.06, if you used docker rm -f to remove a container all container metadata would be removed even if there was some error somewhere in the cleanup of the container (e.g., failing to remove the rootfs layer). In 17.06 it will no longer remove the container metadata and instead flag the container with a Dead status so you can attempt to remove it again.

You can safely remove these directories, but I would stop docker first, then remove, then start docker back up.

Solution 3 - Docker

docker takes lot of gig into three main areas :

  1. Check for downloaded and compiled images. clean unused and dead images by running below command

    $docker image prune -a

  2. Docker creates lot of volume, some of the volumes are from dead container that are no more used clean the volume and reclaim the space using

    $docker system prune -af &&
    docker image prune -af &&
    docker system prune -af --volumes &&
    docker system df

  3. Docker container logs are also very notorious in generating GBs of log overlay2 storage for layers of container is also another source of GBs eaten up . One better way is to calculate the size of docker image and then restrict the docker container with below instructions for storage and logs upper cap. For these feature use docker V19 and above.

    $docker run -it --storage-opt size=2G --log-opt mode=non-blocking --log-opt max-buffer-size=4m fedora /bin/bash

Solution 4 - Docker

Note that this is actually a know, yet still pending, issue: https://github.com/moby/moby/issues/37724 If you have the same issue, I recommend to "Thumbs Up" the issue on GitHub so that it gets addressed soon.

Solution 5 - Docker

I had same issue. In my case solution was:

  1. view all images:
    docker images

  2. remove old unused images:
    docker rmi IMAGE_ID

  3. possibly you will need to prune stopped containers:
    docker container prune

p.s. docker --help is good solution :)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMartin ThomaView Question on Stackoverflow
Solution 1 - DockerMartin ThomaView Answer on Stackoverflow
Solution 2 - Dockercpuguy83View Answer on Stackoverflow
Solution 3 - Dockervinit sharmaView Answer on Stackoverflow
Solution 4 - DockerRobertView Answer on Stackoverflow
Solution 5 - DockerDmitryView Answer on Stackoverflow