Skip to content

Instantly share code, notes, and snippets.

@nileshsimaria
Last active April 4, 2024 09:13
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save nileshsimaria/ec2ea6847d494d2a1935c95d7c4b7155 to your computer and use it in GitHub Desktop.
Save nileshsimaria/ec2ea6847d494d2a1935c95d7c4b7155 to your computer and use it in GitHub Desktop.
Change docker's default /var/lib/docker to different directory on Ubuntu
1. Take a backup of docker.service file.
$ cp /lib/systemd/system/docker.service /lib/systemd/system/docker.service.orig
2. Modify /lib/systemd/system/docker.service to tell docker to use our own directory
instead of default /var/lib/docker. In this example, I am using /p/var/lib/docker
Apply below patch.
$ diff -uP -N /lib/systemd/system/docker.service.orig /lib/systemd/system/docker.service
--- /lib/systemd/system/docker.service.orig 2018-12-05 21:24:20.544852391 -0800
+++ /lib/systemd/system/docker.service 2018-12-05 21:25:57.909455275 -0800
@@ -10,7 +10,7 @@
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
-ExecStart=/usr/bin/dockerd -H unix://
+ExecStart=/usr/bin/dockerd -g /p/var/lib/docker -H unix://
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
3. Stop docker service
$ systemctl stop docker
4. Do daemon-reload as we changed docker.service file
$ systemctl daemon-reload
5. rsync existing docker data to our new location
$ rsync -aqxP /var/lib/docker/ /p/var/lib/docker/
6. Start docker service
$ sysctl docker start
@slavikme
Copy link

You should be consistent and start the docker service using the following command:

systemctl start docker

@d0zingcat
Copy link

nice shot

@JKatzwinkel
Copy link

this didn't work for me so I went with /etc/docker/daemon.json solution.

@iamvasanth07
Copy link

Worked for me.

@rpetrenko
Copy link

thanks, works on ubuntu20.04, docker19.03

@ochapman
Copy link

use —data-root in Docker version 19.03.1

@dlandigi
Copy link

dlandigi commented Aug 1, 2021

This works

@netopaez97
Copy link

Worked.

Moreover, for systemctl spot docker:

Sometimes docker has socket running to be active. You have to also stop it with

systemctl stop docker.socket

Then, check if docker is not running with

systemctl status docker
AND
ps aux | grep -i docker | grep -v grep -----> if you don't have outputs here, it means docker is stopped, otherwise it needs to be stopped.

@Dantali0n
Copy link

this is al fun and games until your package manager updates the docker.service file and suddenly everything breaks.

@joshuataylor
Copy link

You are going to get a very nasty surprise when docker updates :)

Try this?

  1. sudo systemctl edit docker
  2. Add JUST this (using your example):
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -g /p/var/lib/docker -H unix://

I found a great explanation here, which explains better than I can: https://askubuntu.com/questions/659267/how-do-i-override-or-configure-systemd-services

@pumuugel82
Copy link

pumuugel82 commented Jan 4, 2023

[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -g /p/var/lib/docker -H unix://

Your option -g /p/var/lib/docker doesn't work for me. But a:

sudo /var/lib/dockerd --help

revealed that there is a option --data-root. After copying all the contents from /var/lib/docker to /data/var/lib/docker (/data is my new directory pointing to some new bigger virtual disk as I am running fedora 37 in libvirt) and after modify the systemd's override via:

sudo systemctl edit docker

and pasted the following content:

[Service]
ExecStart=
ExecStart=/usr/bin/dockerd --data-root /data/var/lib/docker/ -H fd:// --containerd=/run/containerd/containerd.soc

it works perfect.

Pay attention: The value for --data-root has to end with a slash "/". The first shot didn't work as I missed it. After appending the "/" it worked.

Regards

EDIT
Of course the sequence of editing is as follows:

  1. sudo systemctl stop docker
  2. sudo systemctl status docker #check that docker is relay inactive
  3. sudo cp -r /var/lib/docker /directory-for--data-root
  4. sudo systemctl edit docker
  5. change and save the override with values like above "ExeStart= ...." (yes ExecStart is needed two times, 1. to reset, 2. with new value)
  6. sudo systemctl deamon-reload
  7. sudo systemctl start docker
  8. be happy

@lang8984
Copy link

lang8984 commented Sep 9, 2023

@pumuugel82 it's worked for me, thank you

@vijaykumar3060
Copy link

@pumuugel82 #6. sudo systemctl deamon-reload <---- daemon spelling correction :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment