Derek Lawless

There is always sunshine. Far above the grey sky

21st February 2020

Docker spring cleaning

2 min read

When using Docker you may find over time that disk space is being consumed rapidly by ophan images and/or volumes.

(These commands were tested with Docker running on Ubuntu, your mileage with Docker on Windows or macOS may vary.)

To find out how much disk space Docker is consuming type the following command in your terminal:

docker system -df

This will give you an overview of what resources are being used.

There are a number of ways to clean up unwanted Docker artifacts and reclaim disk space using your terminal, a few of which are outlined below.

Deleting exited containers

docker rm -v $(docker ps -a -q -f status=exited)

Removing unwanted 'dangling' images

docker rmi $(docker images -f "dangling=true" -q)

Deleting unwanted volumes

docker volume rm $(docker volume ls -qf dangling=true)

Pruning

With Docker v1.25 or later you can accomplish the above with the prune command:

docker system prune

To remove all unused images (vs. just 'dangling' ones), include the -a switch:

docker system prune -a

Automating spring cleaning

docker-gc-cron is an excellent Docker container provided by Spotify that can be used to automatically clean up unused containers and images.

You may want to consider modifying the default docker-compose.yml file provided with configuration values more appropriate to your needs e.g.

# Set the grace period
- GRACE_PERIOD_SECONDS=43200 # 12 hours

# Commenting out will reset the log output to UTC
# - TZ=America/New_York