Docker
OverlayFS
Cleanup
Docker Directory
System Maintenance

how to clean up docker overlay directory?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The Docker overlay network is a mechanism to allow containers to communicate, even if they are distributed across different hosts in a Docker Swarm or Kubernetes cluster. While Docker simplifies many aspects of container management, cleaning up the overlay storage used by containers often confounds users. This article provides a detailed guide on how to clean up the Docker overlay directory to regain disk space and maintain a streamlined system.

Understanding the Overlay Filesystem

Docker's overlay filesystem is a union filesystem that creates a unified view of multiple file directories. It's commonly used because it allows for efficient storage and changes to container filesystem layers. The primary components of the overlay filesystem include:

  • Lowerdir: Holds the lower layers from the image.
  • Upperdir: Holds the upper layers with writable changes.
  • Workdir: Used by the overlay filesystem when changes are committed.
  • Merged: The visible content presented to the user.

Understanding these components is crucial as you'll frequently encounter them while cleaning the overlay directory.

When to Clean Up

Over time, docker builds up many unused and dangling images or unmanaged directories in the overlay storage, which can occupy significant disk space. You should consider cleaning up the overlay directory if you notice:

  • Disk space is rapidly consumed.
  • Numerous dangling images exist.
  • Accumulation of orphaned volumes and stale containers.

Cleaning Up the Docker Overlay Directory

Step 1: Prune Unused Docker Objects

Docker provides different prune commands to remove unused objects, including containers, images, networks, and volumes.

bash
1# Remove stopped containers
2docker container prune
3
4# Remove unused images
5docker image prune
6
7# Remove unused networks
8docker network prune
9
10# Remove unused volumes
11docker volume prune
12
13# Remove all unused objects
14docker system prune

Step 2: Examine Overlay Storage

Before deleting anything manually, it's useful to inspect the current state of your overlay directory. Typically located at /var/lib/docker/overlay2, this directory contains the writable layers of your containers.

bash
# Check the size of the overlay directory
du -sh /var/lib/docker/overlay2

Step 3: Delete Unused and Stale Layers

If the above methods don't reclaim a satisfactory amount of space, you can delve into more detailed cleanup methods:

  1. Identify Largest Directories:
    To identify which directories are consuming the most disk space:
bash
   du -ah /var/lib/docker/overlay2 | sort -rh | head -n 10
  1. Deleting Specific Layers:
    Once identified, you should tread carefully. Manually deleting directories can break Docker if the corresponding images or containers are still in use.
bash
   rm -rf /var/lib/docker/overlay2/<directory_name>

Note: This should be done with extreme caution and ideally in systems with halted Docker services.

Step 4: Advanced Cleanup and Rebuild

In scenarios where the overlay storage has become significantly cluttered and resistant to easy cleanup, consider rebuilding your Docker environment:

  1. Backup Docker Images and Volumes:
    Ensure you have backups of essential data:
bash
   docker save -o my_image.tar my_image
   docker export -o my_container.tar my_container_id
  1. Restart Docker Services:
bash
   systemctl restart docker
  1. Rebuild Docker Data Directory:
    As a last resort, you can completely rebuild your Docker images from Dockerfiles:
bash
1   service docker stop
2   rm -rf /var/lib/docker/overlay2
3   service docker start
4   # Rebuild images
5   docker build -t my_image .

Summary

The following table summarizes the key aspects of cleaning up the Docker overlay directory:

ActionDescription
Prune Unused ObjectsUse Docker prune commands to clean up unused objects.
Examine Overlay StorageInspect /var/lib/docker/overlay2 for bloat.
Identify Largest DirectoriesUse du to find the largest directory for analysis.
Delete Specific LayersCautiously remove directories after verifying usage.
Advanced Cleanup and RebuildBackup and rebuild Docker environment if required.

Conclusion

Effectively managing and cleaning up the Docker overlay directories is vital for maintaining system performance and freeing up disk space. While Docker provides convenient methods, understanding the underlying filesystem helps in making informed decisions when manual cleanup becomes necessary. Always proceed with caution, ensuring proper backups and system checks are in place to avoid unintended service interruptions.


Course illustration
Course illustration

All Rights Reserved.