crafting (and) JavaScript

Free Docker Disk Space

If you are running out of disk space on your docker host, you can free up some space by removing unused docker images, containers, and volumes. I am running docker-compose and always watch the logs, since the service is not high scale yet, so the log files fill up. This means from time to time I need to free some disk space.

TL;DR Here are some commands to free up disk space:

# remove all stopped containers
docker container prune

# remove all unused images
docker image prune

# remove all unused volumes
docker volume prune

# remove all unused networks
docker network prune

The Log Files?

The log files are those that eat up space on my disk, I think. So I also remove them from time to time:

# remove all logs
sudo rm /var/lib/docker/containers/*/*-json.log

To show a sum of all the disk space docker uses:

> docker system df
Images          14        7         10.25GB   7.906GB (77%)
Containers      7         6         2.005MB   0B (0%)
Local Volumes   63        3         247.5MB   0B (0%)
Build Cache     167       0         632.9kB   632.9kB

They are not the ones eating up the space. So Moving on, to find it out.

It's Something Else

Now I need some linux skills, I guess it's a dummy move, but I am going down to the root cd / and run du -h -d1 . which shows me a readable list of all dirs under / and the disk space they occupy.

I see that /var is the one that eats up the most space, so I go there and run du -h -d1 . again.

In the end it's 11G that are in the overlay2 dir, see here:

> du -h -d1 .
24M	  ./image
4.0K  ./containers
4.0K  ./swarm
84K	  ./network
225M  ./volumes
16K	  ./plugins
16M	  ./buildkit
11G	  ./overlay2
4.0K  ./runtimes
4.0K  ./tmp
11G	.

After reading around a bit, it seems docker builder prune will clean up the build cache, let me try this. The worst that can happen is that the next docker build will take forever, but I can live with that.

> du -h -d1 .
9.5M    ./image
4.0K	./containers
4.0K	./swarm
84K	    ./network
225M	./volumes
16K	    ./plugins
16M	    ./buildkit
3.9G	./overlay2
4.0K	./runtimes
4.0K	./tmp
4.1G	.

Oh wow, I won 7G of disk space back. That's nice.