By default, Docker stores most of its data inside the /var/lib/docker
directory on Linux systems. There may come a time when you want to move this storage space to a new location. For example, the most obvious reason might be that you’re running out of disk space.
In this tutorial, we’ll show you how to change the storage directory for Docker to some other location on your Linux system. Most of the configuration that will need done is with systemd, and then moving the directory to a new location. Follow the step by step instructions below to get started.
- How to edit systemd to accommodate new Docker location
- How to move your Docker installation to a new location
Category | Requirements, Conventions or Software Version Used
System | Any Linux distro with systemd |
---|---|
Software | Docker |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions | # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command |
$ – requires given linux commands to be executed as a regular non-privileged user |
Be sure to follow these steps in their exact order, otherwise you may encounter strange errors that will be a little headache to recover from. These instructions have been performed on an Ubuntu system, but should work for any other Linux distro as long as it uses systemd.
STEP 1 - The first thing we want to do is stop Docker from running. Making these changes while Docker is still running is certain to cause some errors. Use the following systemd command to stop Docker.
$ sudo systemctl stop docker.service
$ sudo systemctl stop docker.socket
Step 2 - Next, we need to edit the /lib/systemd/system/docker.service
file. This is the systemd file that relates to Docker, and we need to enter the new location inside this file. Use nano or your preferred text editor to open it.
$ sudo nano /lib/systemd/system/docker.service
Step 3 - The line we need to edit looks like this:
ExecStart=/usr/bin/dockerd -H fd://
Edit the line by putting a -g
and the new desired location of your Docker directory. When you’re done making this change, you can save and exit the file.
ExecStart=/usr/bin/dockerd -g /new/path/docker -H fd://
Step 4 - If you haven’t already, create the new directory where you plan to move your Docker files to.
$ sudo mkdir -p /new/path/docker
Step 5 - Afterwards, you can copy the content from /var/lib/docker
to the new directory. A good way to do that would be with the following rsync command.
$ sudo rsync -aqxP /var/lib/docker/ /new/path/docker
Step 6 - Next, reload the systemd configuration for Docker, since we made changes earlier. Then, we can start Docker.
$ sudo systemctl daemon-reload
$ sudo systemctl start docker
Step 7 - Just to make sure that it worked, run the ps command to make sure that the Docker service is utilizing the new directory location.
$ ps aux | grep -i docker | grep -v grep
All done.
In this tutorial, we saw how to move the Docker storage directory to a new location on Linux. This is actually a straightforward process, but one that most users don’t know how to do right away, since it involves editing the systemd configuration file for Docker. You can now delete the default /var/lib/docker
directory. If something goes wrong, you can move the directory back while troubleshooting.
EDIT
This approach is not a good one since everything can get overriden when an upgrade of the Docker is performed. A better way to do it is to follow the documentation.
Instead of modifying the file /lib/systemd/system/docker.service
you can create a file in the following directory:
$ touch /etc/docker/daemon.json
And add the override option to change the location of the data when Docker runs:
{
"data-root": "/web/var/lib/docker"
}