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.
In this tutorial you will learn:
- 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|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Change Docker Location
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.
- 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
- Next, we need to edit the
/lib/systemd/system/docker.servicefile. 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
- The line we need to edit looks like this:
ExecStart=/usr/bin/dockerd -H fd://
Edit the line by putting a
-gand 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://
- If you haven’t already, create the new directory where you plan to move your Docker files to.
$ sudo mkdir -p /new/path/docker
- Afterwards, you can copy the content from
/var/lib/dockerto 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
- 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
- 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
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.