Docker stores all its data, such as images, containers, and volumes, in the default directory /var/lib/docker. This directory can become large over time, consuming space on the root filesystem. To avoid storage limitations or to use a separate disk for Docker data, it is common to change Docker’s data directory to a different location.

Changing the Docker data directory allows you to store Docker data in a more suitable location, such as a dedicated disk with more space. This can be useful when the default location is running out of space or when performance improvements are needed. The process requires modifying Docker’s configuration to point to the new directory and ensuring that Docker data is safely moved.

Before proceeding with any configuration changes, it is essential to stop the Docker service and move all existing data to the new directory. Once the configuration is updated, Docker will store all its data in the new location. This simple adjustment helps manage storage efficiently without affecting Docker’s performance.

Steps to change Docker data directory:

  1. Stop the Docker service.
    $ sudo systemctl stop docker

    Stopping the Docker service ensures that no containers are running and prevents data corruption while moving the directory.

  2. Move the current Docker data directory to the new location.
    $ sudo mv /var/lib/docker /new/path/docker

    Replace /new/path/docker with the actual location where you want to store Docker data. Ensure that the new directory has enough space and the correct permissions.

  3. Update Docker’s configuration to use the new directory.
    $ sudo vim /etc/docker/daemon.json
  4. Add the following line to the file.
    {
        "data-root": "/new/path/docker"
    }

    If the daemon.json file doesn’t exist, create it. This file configures Docker’s storage settings, including the data directory.

  5. Restart the Docker service.
    $ sudo systemctl start docker

    After restarting, Docker will apply the new settings and use the specified data directory.

  6. Verify that Docker is using the new directory.
    $ docker info | grep "Docker Root Dir"
    Docker Root Dir: /new/path/docker

    If the output shows the new path, Docker has successfully switched to the new data directory. Verify that everything is functioning correctly before removing any backups.

Discuss the article:

Comment anonymously. Login not required.