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.
|
|
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.
|
|
Then backup the original directory data.
|
|
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.
Next, create a new softlink to /var/lib/docker
.
|
|
Finally, restart the Docker service.
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
.
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.
|
|
Add
Restart Docker & clean up the original installation (storage) directory
Finally, restart the Docker service.
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.
|
|