Docker
Docker images
container storage
host machine
DevOps

Where are Docker images stored on the host machine?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Docker is a popular platform used for developing, shipping, and running applications in containers. It allows you to package your application along with its dependencies into a standardized unit known as a Docker image. These images are crucial because they form the blueprint of your applications and can be utilized to create containers.

Understanding where Docker stores these images on the host machine is quite beneficial for users, especially when managing disk space or optimizing performance. This article delves into the technical aspects of Docker image storage and provides insights into how you can interact with these storage layers.

Docker Image Storage Location

Docker images on Linux-based systems are typically stored under the Docker directory. On most Linux systems, the default storage location is /var/lib/docker. This directory houses the various subdirectories and files related to image storage, container data, and volume data.

By default, Docker uses a storage driver to manage these images and layers. Common storage drivers include OverlayFS, AUFS, and btrfs. The specific subdirectory for images varies based on the storage driver Docker has employed.

Main Subdirectories in /var/lib/docker

  1. /var/lib/docker/image/: Houses image metadata such as manifests and repositories.
  2. /var/lib/docker/overlay2/: Stores the read-only layers and the read-write layer for images when using OverlayFS.
  3. /var/lib/docker/aufs/: Used instead of overlay2 on older releases of Docker or specific configurations.
  4. /var/lib/docker/btrfs/: Applicable if the Btrfs storage driver is in use.

Storage Driver Details

OverlayFS

OverlayFS is a union filesystem that provides a way to superimpose a read/write layer atop several read-only layers. This is essential to Docker in maintaining an efficient layering of images where newer image layers are written over top of older ones, ensuring that identical layers are shared among containers to optimize storage.

  • Directory: /var/lib/docker/overlay2/
  • Supports Fast Layer Operations: Each image consists of multiple layers that share a common base, which helps save space and speeds up operations.

AUFS

AUFS (Another Union File System) was historically the default storage driver before OverlayFS. While it is still supported, OverlayFS is preferred due to its integration within the Linux kernel.

  • Directory: /var/lib/docker/aufs/
  • Features: Like OverlayFS, AUFS layers files over one another to provide a single unified view.

Btrfs

Btrfs is a copy-on-write filesystem ideal for Docker image storage. It allows snapshots, providing capabilities such as image rollback and restoration.

  • Directory: /var/lib/docker/btrfs/
  • Advanced Features: Supports subvolumes and snapshotting, which can aid in better management and backup of images.

Changing Image Storage Location

Docker allows customization of the default storage location if required. This can be done by modifying the --data-root option in the Docker daemon's configuration file or by setting it as a command-line option.

To change the storage location:

  1. Configure the Docker Daemon:
    • Modify or create the configuration file usually located at /etc/docker/daemon.json.
    • Add the entry:
json
     {
         "data-root": "/new/path/to/docker"
     }
  • Restart the Docker service:
bash
     sudo systemctl restart docker

Practical Examples

To inspect your current storage driver and the default storage location, use the command:

bash
docker info | grep "Storage Driver"

This command outputs which storage driver is currently in use. To see all images and their unique IDs:

bash
docker images -q

Finally, to visualize image usage:

bash
docker system df

This provides statistics about image storage and allows for manual cleanup decisions.

Summary Table

Here is a summary of the key points regarding Docker image storage:

AspectDetails
Default Linux Path/var/lib/docker
Main DriversOverlayFS, AUFS, Btrfs
Key Directoriesoverlay2, aufs, btrfs
CustomizationChange via daemon settings or --data-root
Command ExamplesInspect driver: docker info List IDs: docker images -q

By understanding the structure and configuration options regarding Docker image storage, you gain better control over resource management, enabling the delivery of reliable and efficient containerized applications.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.