DEV Community

Dongchao (Alan) Chen
Dongchao (Alan) Chen

Posted on

move /var/lib/docker to different volume for docker images

Problem 1 - need move /var/lib/docker to large volume

Our local linux disk volume is under 30GB, so it will be quite challenge for our docker images, which take a lot of space under /var/lib/docker folder and quickly run out of space.
small disk size

Solution for Problem 1

It is possible to follow this artile - Use a Different Volume For Your Docker Images in Ubuntu that mount a new volume folder e.g. /dev/sda2 for /var/lib/docker and add the following line

/dev/sda2    /var/lib/docker    ext4    defaults    0    1
Enter fullscreen mode Exit fullscreen mode

in /etc/fstab so the mount also happen during startup linux.

A quick tip to get volume type is using command df -T

use command df -T

What I did in fact follow this artile - How to move docker data directory to another loccation on Ubuntu

  • stop the docker daemon
$ sudo service docker stop
Enter fullscreen mode Exit fullscreen mode

and make sure that docker is not running

$ ps faux | grep -i docker
Enter fullscreen mode Exit fullscreen mode
  • make the folder and rsync the files
$ mkdir /mnt/docker
$ rsync -avxP /var/lib/docker/ /mnt/docker
Enter fullscreen mode Exit fullscreen mode
  • Update docker configuration
    • Add a configuration file and tell the docker daemon what is the location of the data directory by creating a file called daemon.json under directory /etc/docker. the file should have the content:
{
    "data-root":"/mnt/docker"
}
Enter fullscreen mode Exit fullscreen mode
  • Update the docker service by editing /lib/systemd/system/docker.service Find the following line
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Enter fullscreen mode Exit fullscreen mode

and update to

ExecStart=/usr/bin/dockerd -g /mnt/docker -H fd:// --containerd=/run/containerd/containerd.sock
Enter fullscreen mode Exit fullscreen mode
  • Re-start the docker daemon
sudo service docker start
# reload docker serivce configuration 
sudo systemctl daemon-reload
Enter fullscreen mode Exit fullscreen mode
  • test by the following command after you pull an image
docker inspect image_id | grep WorkDir
Enter fullscreen mode Exit fullscreen mode

Problem 2 - After linux reboot, docker images are gone

The docker images under /mnt/docker are gone, e.g. /mnt/docker/overlay2 has no docker images that we pulled fromm DockerHub.

See github discussion https://github.com/moby/moby/issues/36149
and PR https://github.com/moby/sys/pull/62

Solution for Problem 2

No solution yet, keep looking! So far, have to re-pull the docker images.

Top comments (1)

Collapse
 
nuhyurdev profile image
nuh yurduseven

That's it. Thanks for sharing.