Keywords: Docker image storage | storage drivers | filesystem structure
Abstract: This article provides an in-depth examination of Docker image storage mechanisms on host machines, detailing directory structures across different storage drivers. By comparing mainstream drivers like aufs and devicemapper, it analyzes storage locations for image contents and metadata, while addressing special storage approaches in Windows and macOS environments. The content includes complete path references, configuration methods for modifying storage locations, and best practices for image management to help developers better understand and operate Docker image storage.
Fundamental Architecture of Docker Image Storage
Docker employs a layered filesystem architecture to manage images, enabling efficient sharing and reuse of base layers. When users pull an image, Docker decomposes it into multiple read-only layers, each representing a filesystem change set. These layers are stored on the host in specific formats, with exact locations and structures dependent on the storage driver in use.
Critical Role of Storage Drivers
Docker supports multiple storage drivers including aufs, overlay, overlay2, btrfs, devicemapper, and zfs. By default, Docker automatically selects the optimal driver based on kernel support. Users can manually specify the storage driver using the -s or --storage-driver option. While different drivers vary in performance and features, they all adhere to the same fundamental storage principles.
General Storage Directory Structure
On Linux systems, Docker's default storage root directory is /var/lib/docker. This directory contains multiple subdirectories managing different components:
/var/lib/docker/{driver-name}: Stores driver-specific image contents/var/lib/docker/graph/<id>: Stores image metadata including JSON configurations and layer size information/var/lib/docker/containers: Stores container runtime data
Detailed Analysis of aufs Driver Storage
aufs (Advanced Multi-Layered Unification Filesystem) was Docker's default driver in early versions. Under aufs architecture:
- The
/var/lib/docker/aufs/diff/<id>directory contains actual file contents of images, with each subdirectory corresponding to an image layer - The
/var/lib/docker/repositories-aufsfile stores local image information in JSON format, viewable via thedocker imagescommand - aufs utilizes union mount technology to merge multiple read-only layers into a single view, achieving efficient layer reuse
Storage Mechanism of devicemapper Driver
devicemapper is widely used in RedHat-based systems, with the following storage structure:
/var/lib/docker/devicemapper/devicemapper/data: Stores sparse files containing image data/var/lib/docker/devicemapper/devicemapper/metadata: Stores image metadata- These files employ thin provisioning technology, occupying significantly less disk space than their logical size
Characteristics of Other Storage Drivers
overlay and overlay2 are recommended storage drivers for modern Linux kernels, offering better performance and stability. The btrfs driver leverages btrfs filesystem snapshots for efficient layer management. The zfs driver provides similar functionality on ZFS filesystems, suitable for scenarios requiring advanced storage features.
Special Storage in Non-Linux Systems
On macOS systems, Docker Desktop utilizes virtualization technology. Images are stored in virtual machine disk files:
- Earlier versions:
~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/Docker.qcow2 - Current versions:
~/Library/Containers/com.docker.docker/Data/vms/0/Docker.raw
On Windows systems, particularly when using WSL2 backend:
- Images are stored in WSL2 virtual machine disk files
- Accessible via
\\wsl.localhost\docker-desktop-data\data\docker\imagepath - Specific location is under
overlay2/imagedb/content/sha256directory
Storage Location Configuration and Management
Users can modify default storage locations by editing Docker daemon configuration. On Linux systems, edit the /etc/docker/daemon.json file:
{
"data-root": "/path/to/new/docker/root"
}
On Windows systems, modify the daemon.json configuration file:
{
"graph": "D:\\docker\\storage"
}
Relationship Between Image Operations and Storage
Understanding image storage locations is crucial for specific operations:
docker pull: Downloads remote image layers to local storage directoriesdocker build: Creates new image layers based on Dockerfile and stores themdocker save: Exports local images as tar archive filesdocker push: Uploads local images to registries
Best Practices and Important Considerations
In practical usage, it's recommended to:
- Regularly clean unused images and layers to free disk space
- Monitor storage directory sizes to prevent disk exhaustion
- Consider using dedicated storage drivers in production environments for optimal performance
- Avoid directly modifying files in storage directories; manage images through Docker commands
Troubleshooting and Debugging Techniques
When encountering storage-related issues:
- Use
docker infoto view current storage driver and root directory information - Check disk space usage
- Verify compatibility between storage driver and kernel version
- Backup important data before changing storage locations