How to move docker's default /var/lib/docker to another directory on Ubuntu/Debian Linux - LinuxConfig.org

Bump
Didn’t work for me either with the -g option. The Docker daemon didn’t start anymore when I changed the /usr/bin/dockerd line in /lib/systemd/system/docker.service`.

No problems with --data-root instead. :+1: Very neat.

Thx @OP for Tutorial and @Lungaro for pointing this out. Should have read your comment first instead of aimlessyl searching and around and finally checking the CLI outputs of /usr/bin/dockerd --help. Would have saved me quite some time.^^

$ uname --srvmo
Linux 5.15.0-88-generic #98~20.04.1-Ubuntu SMP Mon Oct 9 16:43:45 UTC 2023 x86_64 GNU/Linux
$ sudo docker --version
Docker version 24.0.7, build afdd53b
$ /usr/bin/dockerd --help | grep "data"
      --data-root string                        Root directory of persistent Docker state (default "/var/lib/docker")
1 Like

Update:
TLDR: Changing /lib/systemd/system/docker.service by adding the option --data-root <new_dir_path> at the /usr/bin/dockerd […] command didn’t work for me either. Not sure why.
Also I killed my system accidentally by mistyping the path in sudo rm -r <path>.

Long:
First tried the instructions as in the tutorial. (sudo systemctl […] to stop docker.service, docker, docker.socket and docker.engine) than adding -g <new_dir_path> to the /usr/bin/dockerd […] command in /lib/systemd/system/docker.service. Than trying to restart docker with sudo systemctl again. Didn’t work.

Second tried to change it back to original. worked again but nearly no memory left.

Third tried to add the --data-root <new_dir_path> line as Lungaro and others mentioned before. Restarted docker and it worked. Seemingly. Forgot to rsync the files. Oops. Rsynced the files to the new directory.

Note: old directory was ext4, new directory was ntfs and on another ssd (internal). Not sure if that matters.

Started docker again. seemed to work, could pull an image and start it until I realized that still the default path is used. 0 bytes left on the partition with the default path. :confused: Ouch.

Fourth I did a stupid thing and killed my OS. :man_facepalming:

As i tried to delete /var/lib/docker and subfolders to remove the old images and containers which where already copied to new path with rsync. I accidentally mistyped: sudo rm -r /var/lib/docker with “/var/lib/&docker” as the path. That didn’t just delete the docker folder plus subfolders but instead deleted /var/lib and everything therein. HUGE MISTAKE.

Back-upped important data as long as I was still logged in. Now I have to completely reinstall my system which was luckily planned anyway in the near future. :person_shrugging: So guess that’s a good opportunity to make a fresh and clean install after my pretty messed system. :smiley:

Take care!
And be careful with removing files from /var/lib. Don’t learn it the hard way.^^

1 Like

Initially couldn’t get this to work. Dockerd didn’t want to startup. I added --debug to the ExecStart line in the docker.service and then it became clear that the -g option was no longer valid. Running dockerd --help showed that I should use --data-root , as previous comments have described as well.

Once updated with --data-root all works just fine.

Also the rsync command is perfect, all directories under /var/lib/docker/* can now be found under /localdisk/docker/var (the new directory I created for the docker files).

Once I had all my docker containers recreated and was confident it all worked with the storage in the new location I freed up the storage under /var/lib/docker:

cd /var/lib
pwd
(to double check that I’m indeed in /var/lib)
rm -rf docker
(always triple check before you hit on rm -rf commands! )