Skip to content

Instantly share code, notes, and snippets.

@kinrokinro
Forked from nileshsimaria/docker-default-directory
Created September 19, 2022 10:50
Show Gist options
  • Save kinrokinro/d2facc89bc175a988db07bf1575754f0 to your computer and use it in GitHub Desktop.
Save kinrokinro/d2facc89bc175a988db07bf1575754f0 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment