How to move default Docker /var/lib/docker to other location

Previously we talked about how to install docker in RHEL 7 (Red Hat Enterprise Linux 7). Docker will use /var/lib/docker/ as their data and image storage. In some case you have small space in your system and you run out your space, then you need to change its default location to other partition that have more space.

The following guide will let you through a process of changing the  docker’s default /var/lib/docker storage disk space to another  partition directory.. The following guide should work for both CentOS, RHEL, Fedora, Ubuntu and Debian  Linux or any other systemd system.

Make sure you have followed this guide in the  exact order of execution.

Let’s get started by modifying systemd’s docker start up script.

Locate where systemd daemon run docker startup script by running following script.

[[email protected] ~]$ systemctl status docker● docker.service - Docker Application Container Engine   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)  Drop-In: /etc/systemd/system/docker.service.d           └─http-proxy.conf   Active: active (running) since Mon 2019-05-27 14:34:42 WIB; 17h ago     Docs: https://docs.docker.com Main PID: 32783 (dockerd)    Tasks: 23   Memory: 152.2M   CGroup: /system.slice/docker.service           ├─32783 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.s...           └─33975 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 80 -container-ip 172.17.0.2 -...May 27 14:34:42 xptapiprtldb01 dockerd[32783]: time="2019-05-27T14:34:42.263288083+07:00" level=info msg=...rt."May 27 14:34:42 xptapiprtldb01 dockerd[32783]: time="2019-05-27T14:34:42.343168108+07:00" level=info msg=...ess"May 27 14:34:42 xptapiprtldb01 dockerd[32783]: time="2019-05-27T14:34:42.377570566+07:00" level=info msg=...ne."May 27 14:34:42 xptapiprtldb01 dockerd[32783]: time="2019-05-27T14:34:42.402248122+07:00" level=info msg=...09.6May 27 14:34:42 xptapiprtldb01 dockerd[32783]: time="2019-05-27T14:34:42.402358552+07:00" level=info msg=...ion"May 27 14:34:42 xptapiprtldb01 systemd[1]: Started Docker Application Container Engine.May 27 14:34:42 xptapiprtldb01 dockerd[32783]: time="2019-05-27T14:34:42.431513598+07:00" level=info msg=...ock"May 27 14:37:27 xptapiprtldb01 dockerd[32783]: time="2019-05-27T14:37:27.222355039+07:00" level=info msg=...ete"May 27 14:41:16 xptapiprtldb01 dockerd[32783]: time="2019-05-27T14:41:16.601716305+07:00" level=info msg=...ete"May 27 14:42:49 xptapiprtldb01 dockerd[32783]: time="2019-05-27T14:42:49.372735491+07:00" level=info msg=...ete"Hint: Some lines were ellipsized, use -l to show in full.

See also  How to Change Docker Timezone

In my environment, docker.service file located at /usr/lib/systemd/system/docker.service. Edit it with your favorite text editor and replace the following line where /new/path/docker is a location of your new chosen docker directory:

#FROMExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock#TOExecStart=/usr/bin/dockerd -g /new/path/docker -H fd:// --containerd=/run/containerd/containerd.sock

Reload systemd daemon and stop Docker service.

sudo systemctl daemon-reloadsudo systemctl stop docker

Check if Docker still running or not. It is important that docker doesn’t write data to existing location.

ps aux | grep -i docker | grep -v grep

If no output, then you proceed to move current /var/lib/docker data to your new /new/path/docker/ location. If you have freshly installed Docker, proceed to next step after below step.

# mkdir -p /new/path/docker# rsync -aqxP /var/lib/docker/* /new/path/docker

Now we can safely start docker daemon.

# sudo systemctl start docker

Confirm that docker run with new data location.

$ ps aux | grep -i docker | grep -v greproot      32783  0.0  0.0 602460 62924 ?        Ssl  May27   0:22 /usr/bin/dockerd -g /new/path/docker -H fd:// --containerd=/run/containerd/containerd.sock

Originally posted 2019-05-28 00:46:35.