Relocate Docker Data Root Directory

Lately I had a need to move the Docker root directory to a different filesystem. After researching the item and saw that the documentation out there was limited and not very clear, I decided to write the necessary steps here.

By default, on most systems, the docker root directory is located in the directory /var/lib/docker, this directory is used to store thigs like images, volumes, and other different docker runtime items. In order to move this directory to a different location, let’s say /opt/docker-root, there are few steps required, more specifically:

  1. Stop the docker daemon
  2. Create the filesystem mount point
  3. Move the content of the directory /var/lib/docker to /opt/docker-root
  4. Rename the directory /var/lib/docker to /var/lib/docker.old
  5. Update the docker startup command
  6. Refresh the systemd configuration
  7. Start the docker daemon
  8. If the directory /var/lib/docker is not recreated and the command docker images displays the installed images, you can safely delete the directory /var/lib/docker.old

Now, let’s discuss in details how to accomplish each of the items listed above, which they can variate depending on the operating system used, this guide assumes that you are using Ubuntu or Ubuntu derivative. If you are using something else, you might encounter differences in the commands, if you do, refer to the OS documentation for the alternative commands.

Stop the docker daemon

In order to stop the docker daemon, you would type the following command:

systemctl stop docker

If you installed docker using an alternative method, you should be familiar on how to stop the background process.

Create the filesystem and mountpoint

This step is heavily dependent on what type of filesystem you are running, the operating system you are using, etc. Create a filesystem goes beyond this guide. Plenty of guides on the internet on how to do that.

Move the file content to the new location

In order to move the content to the new location, you first copy the existing content with the following command:

cp -a /var/lib/docker/* /opt/docker-root/

This command might take a while depending on how big the docker installation is, so be patent!

The rename and delete in later steps will complete the move.

Update docker startup command

In this step we will instruct the dockerd command to use an alternative application root directory. If you using a recent Ubuntu distribution, you are using systemd, and the file will be located under /etc. More precisely:

/etc/systemd/system/multi-user.target.wants/docker.service

If the file is located elsewhere, refer to the installation of docker used to find the configuration file.

Now, use your prefer editor to open and modify the following line in the file.

From:

ExecStart=/usr/bin/dockerd -H fd:// –containerd=/run/containerd/containerd.sock

To:

ExecStart=/usr/bin/dockerd –data-root /opt/app/docker-root -H fd:// –containerd=/run/containerd/containerd.sock

If you notice, the command line argument –data-root /opt/app/docker-root has been added to the ExecStart variable. /opt/app/docker-root is the default this guide is using, but you can use whatever location you prefer.

Now on to the next step

Refresh systemd configuration

In order for the change to be picked up by systemd, use the following comand:

systemd daemon-reload

Now you can start the docker daemon with the new config applied.

Start the docker daemon

Much like the stop command, the start command is very similar:

systemctl start docker

Examine the directory /var/lib for the presence of the docker directory

In order to verify that the root directory has been correctly moved. Do the following:

  1. Verify that the directory /var/lib/docker does not exist after the daemon has loaded
  2. Use the command docker images to verify that images are loaded from the new root directory
  3. If the above two steps are ok, you can safely delete the directory /var/lib/docker.old

At this point, the move is completed.

Thank you for following this guide and type away!!!

Leave a Reply