Docker
Installation
Image Directory
Configuration
How-To

How to change the docker image installation directory?

Master System Design with Codemia

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

Changing the installation directory of Docker images is a task that may arise from various needs, such as enhancing performance, improving data organization, or managing storage limitations on default file systems. Docker, by default, stores data—such as images, containers, and volumes—under the /var/lib/docker directory path. However, you can customize this setting to a different directory. This article explains how to change the Docker image installation directory.

Understanding Docker Storage

Before altering Docker's storage directory, it's important to understand how Docker stores its data. Below is a brief overview:

  • Images: Docker images are stored on disk in the graph driver, which can be found under /var/lib/docker/<driver>/.
  • Containers: Each container has its own dedicated directory under /var/lib/docker/containers/.
  • Volumes: When created, volumes reside under /var/lib/docker/volumes/.
  • Configuration: The server's state and configuration settings exist in /var/lib/docker.

Key Points Summary

ComponentDefault LocationStorage Description
Images/var/lib/docker/<driver>/Stores Docker images using the graph storage driver
Containers/var/lib/docker/containers/Contains running and stopped containers' data
Volumes/var/lib/docker/volumes/Persistent storage that's independent of containers
Configuration/var/lib/docker/Docker daemon configuration and state files

Preparation Steps

  1. Ensure Docker is not running:
    Execute command:
bash
   sudo systemctl stop docker
  1. Backup Docker Data:
    To prevent data loss, back up your Docker data. You can copy the entire /var/lib/docker directory to another location:
bash
   sudo cp -r /var/lib/docker /path/to/backup/
  1. Decide on a New Directory:
    Choose a location with adequate space available for storing Docker data (e.g., a separate disk or partition).

Changing Docker Directory

To change Docker’s default storage directory, follow these steps:

Step 1: Create a New Docker Directory

Make a new directory where you want Docker images to reside. Ensure Docker has permissions to write to this directory.

bash
sudo mkdir -p /new/docker/directory

Step 2: Modify the Docker Daemon Configuration

Depending on your system, configure the Docker daemon by modifying the settings used to launch Docker. On most Linux systems, Docker is managed by systemd.

  1. Edit Docker Daemon Configuration
    Open or create the /etc/docker/daemon.json file:
bash
   sudo nano /etc/docker/daemon.json

Ensure this file is set to change the data root option to your new directory:

json
   {
       "data-root": "/new/docker/directory"
   }
  1. Configure Systemd
    If the daemon.json file isn't available, or if additional configurations are needed, you can use a systemd override file:
bash
   sudo mkdir -p /etc/systemd/system/docker.service.d

Create an override.conf file:

bash
1   sudo tee /etc/systemd/system/docker.service.d/override.conf <<EOF
2   [Service]
3   ExecStart=
4   ExecStart=/usr/bin/dockerd -H fd:// --data-root /new/docker/directory
5   EOF

Step 3: Restart Docker Service

  1. Reload the systemd configurations and restart Docker:
bash
   sudo systemctl daemon-reload
   sudo systemctl start docker
  1. Verify the new configuration by checking where your Docker images are stored:
bash
   docker info

Look for the Docker Root Dir entry to confirm the change.

Step 4: Verify Docker Functionality

Ensure Docker images work as expected by pulling a test image:

bash
docker run hello-world

This command confirms whether Docker is operating correctly under the new configuration.

Final Considerations

Changing Docker's storage directory is beneficial for troubleshooting storage space limitations and optimizing environment variable configurations. However, exercise caution when modifying these settings. Always back up your Docker data before making changes and verify the adjustments with functional checks.

Possible Caveats

  • Verify the file permissions for the new directory to avoid permission-related issues.
  • Consider the filesystem type of your new directory as some filesystems may not be compatible with Docker's overlay storage drivers.
  • Make sure there is adequate disk space available to accommodate future growth of Docker images and containers.

With this guidance, you can efficiently change Docker's image installation directory to match your storage and organizational needs.


Course illustration
Course illustration

All Rights Reserved.