How to reduce Docker disk usage

Docker directory ( /var/lib/docker on Linux) tends to grow very big with time. There are two main reasons for it:

  1. Container logs
  2. Old images

Clearing container logs

By default the container logs are written to /var/lib/docker/CONTAINER_ID/CONTAINER_ID-json.log. In order to trim that file (assuming the logs are not necessary) is to use truncate:

$ truncate -s 0 /var/lib/docker/CONTAINER_ID/CONTAINER_ID-json.log 

If the goal is to remove all the logs from all the containers at once:

$ truncate -s 0 /var/lib/docker/containers/*/*-json.log

Removing old images

Old images, that are not in use any more can be removed using the following command:

$ docker image prune -a

In order to avoid being asked about every image:

$ docker image prune -a -f

The result might look like this:

deleted: sha256:45ed867aca4315d546eedf8b8535967325e566754973ad587a4e4fa3956be7d7
deleted: sha256:9b5ce6da8a0e001313bc3c531737b29ee226bff454997c7caccad1203881885d
deleted: sha256:2d99fa07f45de53bb0840f5c473d35ddf715f025e1e56437cb409935714c3dab

Total reclaimed space: 6.783GB