ericvlog / note

6 stars 1 forks source link

Change Docker root directory /var/lib/docker to another location - Linux Tutorials #68

Open ericvlog opened 4 months ago

ericvlog commented 4 months ago

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:

Changing the default Docker directory to a new location

Software Requirements and Linux Command Line Conventions 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

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.

  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

  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

  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://

    Configuring systemd to accommodate the new Docker directory



  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

  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

  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

  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

    The output from ps shows that Docker is using the new directory we configured



All done.

Closing Thoughts

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.



Comments and Discussions