Continuing from our previous article on setting up Docker and running Docker Compose on Debian 12 Bookworm, let’s dive deeper into understanding where Docker stores its image files and how to customize the storage directory for better management.
Default Docker Image Storage Location on Debian 12
By default, Docker stores all its data, including images, containers, volumes, and other artifacts, under the following path:
1 2 |
/var/lib/docker |
This directory contains subdirectories managed by the storage driver Docker uses. For instance, if the overlay2
storage driver is used (the default for most Linux distributions, including Debian 12), images and their layers will be stored in:
1 2 |
/var/lib/docker/overlay2 |
You can verify the current Docker root directory by running:
1 2 |
docker info | grep "Docker Root Dir" |
This command outputs the current path where Docker stores its data:
1 2 |
Docker Root Dir: /var/lib/docker |
While the default location works for most setups, there are scenarios where customizing the storage path is beneficial, such as:
- Low disk space on the default partition.
- Organizing Docker data on a dedicated storage drive or partition.
- Simplifying backups and maintenance.
Customizing Docker’s Storage Directory
To customize where Docker stores its images and other data, follow these steps:
Step 1, Create a New Directory for Docker Data
Choose a new location for Docker’s data, such as /mnt/docker-data
. Create the directory and ensure appropriate permissions:
1 2 3 4 |
sudo mkdir -p /mnt/docker-data sudo chown -R root:root /mnt/docker-data sudo chmod -R 700 /mnt/docker-data |
Step 2, Update Docker Configuration
Docker’s storage directory can be updated by modifying its daemon configuration file:
- Open the Docker daemon configuration file:
sudo nano /etc/docker/daemon.json
- Add or update the
data-root
field to point to the new directory:{ "data-root": "/mnt/docker-data" }
If the file doesn’t exist, create it and add the above content.
Step 3, Stop Docker and Move Existing Data
Before restarting Docker, stop the service and move existing data to the new directory:
1 2 3 |
sudo systemctl stop docker sudo rsync -a /var/lib/docker/ /mnt/docker-data/ |
This command ensures that all existing data is safely copied to the new location. Once completed, rename or backup the old directory:
1 2 |
sudo mv /var/lib/docker /var/lib/docker.bak |
Step 4, Restart Docker
Restart the Docker service to apply the changes:
1 2 |
sudo systemctl start docker |
Verify that Docker is now using the new storage directory:
1 2 |
docker info | grep "Docker Root Dir" |
The output should now display the updated path, e.g.:
1 2 |
Docker Root Dir: /mnt/docker-data |
Step 5, Test the Setup
Pull an image and verify it is stored in the new directory:
1 2 3 |
docker pull hello-world ls /mnt/docker-data |
You should see the newly pulled image data in the customized path.
Reverting to Default Settings
If you encounter issues and need to revert to the default storage location, simply:
- Stop Docker:
sudo systemctl stop docker
- Restore the original directory:
sudo mv /var/lib/docker.bak /var/lib/docker
- Remove the
data-root
entry from/etc/docker/daemon.json
or set it back to/var/lib/docker
. - Restart Docker:
sudo systemctl start docker
Final Words …
Customizing Docker’s storage directory on Debian 12 can help optimize disk usage and improve data management.
By following the steps above, you can easily configure Docker to use a different location for storing images and containers.
As always, test changes thoroughly in your environment and ensure backups are in place before making significant modifications.