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:

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:

You can verify the current Docker root directory by running:

This command outputs the current path where Docker stores its data:

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:

Step 2, Update Docker Configuration

Docker’s storage directory can be updated by modifying its daemon configuration file:

  1. Open the Docker daemon configuration file: sudo nano /etc/docker/daemon.json
  2. 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:

This command ensures that all existing data is safely copied to the new location. Once completed, rename or backup the old directory:

Step 4, Restart Docker

Restart the Docker service to apply the changes:

Verify that Docker is now using the new storage directory:

The output should now display the updated path, e.g.:

Step 5, Test the Setup

Pull an image and verify it is stored in the new directory:

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:

  1. Stop Docker: sudo systemctl stop docker
  2. Restore the original directory: sudo mv /var/lib/docker.bak /var/lib/docker
  3. Remove the data-root entry from /etc/docker/daemon.json or set it back to /var/lib/docker.
  4. 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.

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.