Gyasi Sutton, MD, MPH Physician in Training, Coder at heart. Python, R, Node, and Rust.

The Art of Docker Cleanup: How to Feng Shui Your Environment and Keep Your Containers Running Smoothly

  Reading Time:

If you're constantly creating and destroying Docker environments, like me,  you may find that your host machine's disk space fills up quickly. This can lead to unexpected errors, lost files, and reduced performance. Managing Docker environments can be a challenging task, especially when it comes to keeping your system clean and organized. This tutorial will cover some tips and best practices for reclaiming disk space in your Docker environment, including removing unused containers, images, networks, and volumes. By following these techniques, you can keep your system running smoothly and avoid running out of disk space.

Docker provides a number of tools for managing disk space, including removing unused objects, running the garbage collector, and using filters to limit the objects that will be removed. But lets get the basics out of the way first:

Docker Objects

There are three types of objects in Docker: images, containers, and volumes.

  • Images: An image is a read-only template that contains the application code, libraries, and dependencies required to run the application.
  • Containers: A container is a running instance of an image. When a container is created, it is based on an image and contains all the necessary files and libraries to run the application.
  • Volumes: A volume is a persistent data storage mechanism that can be shared between containers. Volumes are used to store data that needs to persist even when the container is removed.

Removing Unused Objects

To remove unused objects in Docker, you can use the docker system prune command. This command removes all unused containers, images, networks, and volumes from your system.

docker system prune

This command will ask for confirmation before removing the objects. If you want to remove all unused objects without confirmation, you can use the --force or -f option:

$ docker system prune --force
Docker Prune Commands:
Function Description
docker system prune Remove unused containers, images, networks, and volumes
docker system prune -a Remove all unused objects, including containers, images, networks, and volumes
docker system prune -f Force removal of all unused objects, without confirmation
docker system prune --volumes Remove unused volumes
docker system prune --filter "label= Remove objects matching a specific label
docker system prune --filter "until=" Remove objects that have not been used in a certain duration
docker system prune --filter "dangling=true" Remove objects that are not associated with any container
docker system prune --filter "networks=" Remove unused networks
docker container prune Remove all stopped containers
docker container prune --filter "until=" Remove containers that have not been used in a certain duration
docker image prune Remove all unused images
docker image prune --filter "dangling=true" Remove all dangling images
docker image prune --filter "label= Remove images with a specific label
docker image prune --filter "dangling=false" --filter "label!=" Remove images that are not associated with a specific repository
docker volume prune Remove all unused volumes
docker volume prune --filter "dangling=true" Remove all dangling volumes
docker volume prune --filter "label= Remove volumes with a specific label
docker volume prune --filter "dangling=false" --filter "label!=" Remove volumes that are not associated with a specific container

What if I delete an image?!

Images are necessary for creating and running containers in Docker. When a container is created, it is based on an image that contains the application code, libraries, and dependencies required to run the application.

If you delete an image that is being used by a running container, the container will continue to function normally. This is because the container has already loaded the necessary files and libraries into memory, and does not rely on the image file once it is running. However, if you delete an image that is required to create a container, you will not be able to create new containers from that image since it's no longer on your system but, you can pull it again from a registry or rebuild it from a Dockerfile.

To pull an image from a registry, you can use the docker pull command followed by the name of the image and its tag (if applicable). For example:

$ docker pull nginx:latest

This will download the latest version of the Nginx image from Docker Hub.

To rebuild an image from a Dockerfile, you can use the docker build command followed by the path to the Dockerfile. For example:

$ docker build -t myapp:latest .

This will build a new image called myapp from the Dockerfile in the current directory.

Once you have the image, you can use it to create new containers as needed. For example:

$ docker run -d myapp:latest

This will create a new container from the myapp image and run it in the background.

It's a good practice to avoid deleting images that are required to create containers. Instead, you can use the docker image prune command to remove unused images that are not associated with any containers or tags. This will help you to free up disk space without affecting your running containers.

Using Filters

To limit the objects that will be removed, you can use filters with the docker system prune command. Here are some examples of filters you can use:

  • Dangling: Filter images that are not associated with any container or tag.
$ docker image prune --filter "dangling=true"

The dangling filter option is used to filter images that are not referenced by any container or tag. When an image is created, it is given a unique ID that is used to identify it. If the image is tagged with a name and pushed to a registry or used to create a container, it is no longer considered dangling. However, if the image is not tagged or used to create a container, it is considered dangling and can be removed with the docker image prune command.

  • Until: Filter objects that have not been used in a certain duration.
$ docker system prune --filter "until=24h"
  • Label: Filter objects that have a specific label.
$ docker volume prune --filter "label=myapp"

Warning

It's important to note that removing unused objects and running the garbage collector can have unintended consequences. Be sure to review the objects that will be removed before running any of the above commands. Also, removing an image or container will permanently delete it, so be sure to back up any data that you want to keep.

Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications. It provides a convenient way to manage containers, networks, and volumes, and automates the creation and management of these objects.

You can use the docker-compose down command to remove all containers, networks, and volumes created by docker-compose up. This command will stop and remove all containers that were created with docker-compose up, as well as any associated networks and volumes.

Here's an example docker-compose.yml file that defines a web application with a PostgreSQL database:

version: '3'
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
  db:
    image: postgres:latest
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: mypassword
      POSTGRES_DB: mydb
    volumes:
      - db-data:/var/lib/postgresql/data
volumes:
  db-data:

This file defines two services, web and db, and creates a named volume called db-data for storing the data of the db service.

To create and start the containers defined in this file, you can run the following command:

$ docker-compose up -d

This command will start the containers defined in the docker-compose.yml file in detached mode (-d), which means that they will run in the background. To stop and remove the containers created by docker-compose up, you can use the docker-compose down command:

$ docker-compose down

This command will stop and remove all containers created by docker-compose up, as well as any associated networks and volumes. By default, the docker-compose down command will remove only the containers and networks created by docker-compose up. To also remove any volumes that were created by docker-compose up, you can use the --volumes or -v option:

$ docker-compose down --volumes

This command will stop and remove all containers created by docker-compose up, as well as any associated networks and volumes, including named volumes.

Note that the docker-compose down command will permanently delete all data stored in the volumes associated with the containers that are being removed. Make sure to back up any data that you want to keep before running this command.

Using Docker Compose with persistent volumes is a good way to ensure that your data is safe and secure even when the containers are removed. By defining named volumes in your docker-compose.yml file, you can store data in a volume that will persist even when the container is removed. This is a good practice for applications that require persistent data storage.

In conclusion, managing disk space in Docker is an important aspect of Docker administration. By using the tools provided by Docker, such as the docker system prune command and filters, you can remove unused objects and reclaim disk space. Additionally, using Docker Compose with persistent volumes is a good way to ensure that your data is safe and secure even when the containers are removed.

Summary Commands
Command Description
docker system prune Remove all unused containers, images, networks, and volumes
docker image prune Remove all unused images
docker container prune Remove all stopped containers
docker volume prune Remove all unused volumes
docker system prune --filter Use filters to limit the objects that will be removed
docker image rm Remove a specific image
docker-compose up Start the containers defined in a Docker Compose file
docker-compose down Stop and remove the containers, networks, and volumes created by Docker Compose

Demystifying User Permissions and Access in Linux for Developers

In Linux, permissions and access control are fundamental concepts that govern the security of a system. The users of a Linux system can have different levels...

Stash Your Way to a Better Git Workflow

Git stash is a command in Git that allows you to temporarily save changes that you've made to your working directory without committing them. Stashing is...