A while ago, I bought a data cloud disk for my cloud server to be mounted on, as the main storage of those data that occupy large disks in the future, so I thought of migrating some software data directories that were previously installed in the root directory / under the system to the new data disk mount directory, the first migration is Docker, my server system release version is Ubuntu-16.04.

I googled some blog posts and found that some of the methods were mentioned many times but didn’t work after I tried them, so I went straight to the official documentation and combined it with some other third-party blogs, and I finally got it done. Record it and make a backup.

Two ways to migrate Docker’s default installation (storage) directory

If Docker is installed via Ubuntu’s apt-get, the default installation directory should be: /var/lib/docker.

To be completely sure, you can use the following command to find the real installation path.

1
sudo docker info | grep "Docker Root Dir"

Preliminary work

The path /store/software/docker is used below as the new Docker installation (storage) directory to be migrated.

Before starting the migration, first copy the original Docker installation (storage) directory to the new path.

1
cp -a /var/lib/docker /store/software/

Then backup the original directory data.

1
mv -u /var/lib/docker /var/lib/docker.bak

Method 1: Soft Linking

The easiest and quickest way is to create a soft link under the original location by moving the original installation (storage) directory of Docker to a custom other path.

First, shut down the Docker service.

1
2
3
sudo systemctl stop docker
# or
sudo service docker stop

Next, create a new softlink to /var/lib/docker.

1
sudo ln -fs /store/software/docker /var/lib/docker

Finally, restart the Docker service.

1
2
3
sudo systemctl start docker
# or
sudo service docker start

After starting Docker, when you pull image and run container later, the path Docker writes to will still be /var/lib/docker, but because of the softlink setting, it will actually write to the new directory. This completes the migration of the Docker installation (storage) directory.

Method 2: Modify Docker configuration file directly

Docker version < v17.05.0

Since dockerd can specify the image and container storage path with the parameter graph when running the Docker service, for example: -graph=/var/lib/docker, we just need to modify the configuration file to specify the startup parameters.

Docker’s configuration file can set most of the background process parameters, and the location is different in each operating system, in Ubuntu: /etc/default/docker, in CentOS: /etc/sysconfig/docker.

1
2
3
4
5
6
7
# 如果是 CentOS 则添加下面这行:
OPTIONS=--graph="/store/software/docker" --selinux-enabled -H fd://

# 如果是 Ubuntu 则添加下面这行(因为 Ubuntu 默认没开启 selinux):
OPTIONS=--graph="/store/software/docker" -H fd://
# 或者
DOCKER_OPTS="-g /store/software/docker"

Docker version >= v17.05.0

Because Docker officially deprecated the graph feature in this release, if you have Docker installed on your machine with version >= v17.05.0, you cannot change the default installation (storage) directory of Docker by specifying the graph parameter in the /etc/default/docker configuration file. default installation (storage) directory, see the official documentation: Docker Docs.

The good news is that there are other ways to modify the installation (storage) directory in newer versions of Docker: by modifying (creating) /etc/docker/daemon.json and specifying the value of the data-root parameter.

Do this as follows.

1
vim /etc/docker/daemon.json

Add

1
2
3
4
{
    "data-root": "/store/software/docker",
    "storage-driver": "overlay2" # 这个是 Docker 是默认设置,这里也可以不用加
}

Restart Docker & clean up the original installation (storage) directory

Finally, restart the Docker service.

1
2
3
sudo systemctl restart docker
# or
sudo service docker restart

After completing the migration by any of the above methods, delete the backed up data from the original directory after confirming that Docker is working properly.

1
rm -rf /var/lib/docker.bak