Releasing space in Docker on Ubuntu or Linux

Tiempo de lectura: < 1 minuto

If we have problems with the space that Docker occupies, we can do the following to free up the space used:

Pexels - jarron

These logs grow unchecked and can free up dozens of GB without losing anything important.

Ejecuta:

sudo find /var/lib/docker/containers/ -type f -name "*-json.log" -delete 

This will delete all container logs (you should do it).

Then check the space:

df -h 

<

p You probably will recover several GB.

<

p Once you have some space, run:

docker system prune -a --volumes 

y when asked.
This deletes everything that is not in use (idle containers, old images, orphaned volumes).

If you need to keep some containers, you can clean by parts:

docker container prune docker image prune -a docker volume prune docker builder prune 

Check how much space you’ve reclaimed

Run:

sudo du -h --max-depth=1 /var/lib/docker | sort -hr | head -20 df -h 

You should see something like /var/lib/docker going from 59 GB down to 2–5 GB.

You can limit the size of logs in the future by editing /etc/docker/daemon.json:

sudo nano /etc/docker/daemon.json 

You can add this:

{ "log-driver": "json-file", "log-opts": { "max-size": "10m", "max-file": "3" } } 

Then restart Docker:

sudo systemctl restart docker 

Leave a Comment