Difficulty: Beginner
Estimated Time: 10 minutes

spotify/docker-gc is a simple Docker container and image garbage collection script, run as a container.

The script will

  • Containers that exited more than an hour ago are removed.
  • Images that don't belong to any remaining container after that are removed.
  • Base images used by container are not removed unlike docker rmi $(docker images -q)

In this scenario we explored how to use the spotify/docker-gc to perform simple container garbage collection to ensure that unused containers and images are cleaned up.

To run this in production, combine the Docker commands explained here with a scheduler, such as Cron or Mesos, to run your container task at certain points during the day.

Garbage collecting Docker images and containers

Step 1 of 3

Step 1 - Old Containers

The environment has two unused Docker images. You can view them using docker images

In the next step we'll launch the GC. Because no other containers are depending on these images, they will be cleaned up.