Changing Docker’s default root directory (/var/lib/docker)

Docker uses (/var/lib/docker) as default root directory to provide storage space for its operation.

We will see how to change this root directory to some other directory, lets say ‘/home/thiru/docker’.

Lets login to our docker host VM named ‘docker-host-1’ and see the running containers by executing below command,

[root@docker-host-1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a03199172063 nginx "nginx -g 'daemon of…" 2 months ago Up 2 minutes 80/tcp test-nginx
[root@docker-host-1 ~]#

Here container named ‘test-nginx’ is running and we can see the respective files-system as below,

[root@docker-host-1 ~]# df -Th | grep -i docker
overlay overlay 6.2G 4.3G 2.0G 70% /var/lib/docker/overlay2/6960599597e61...64a326/merged
shm tmpfs 64M 0 64M 0% /var/lib/docker/containers/a03199172063..d03a/shm
[root@docker-host-1 ~]#

So before changing docker’s default root directory, please make sure that new directory is existing,

[root@docker-host-1 ~]# cd /home/thiru/
[root@docker-host-1 thiru]# pwd
/home/thiru
[root@docker-host-1 thiru]# mkdir docker
[root@docker-host-1 thiru]# cd docker/
[root@docker-host-1 docker]# pwd
/home/thiru/docker
[root@docker-host-1 docker]# ls
[root@docker-host-1 docker]#

We can accomplish this change by editing two files, they are

1) /etc/sysconfig/docker
2) /etc/docker/daemon.json

1) Updating ‘/etc/sysconfig/docker’ file:

cat /etc/sysconfig/docker

OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false -D -g /home/thiru/docker'

g -> specify docker’s root directory

2) Updating /etc/docker/daemon.json

[root@docker-host-1 ~]# cat /etc/docker/daemon.json
{
"graph": "/home/thiru/docker"
}
[root@docker-host-1 ~]#

Before restarting the docker service, Please follow the below instruction for the running docker hosts,

A)  Stop the docker service,

[root@docker-host-1 ~]# systemctl stop docker
[root@docker-host-1 ~]#

B) Move the exiting docker’s root directory (/var/lib/docker/) content to new directory (/home/thiru/docker/)

[root@docker-host-1 ~]# mv /var/lib/docker/* /home/thiru/docker/
[root@docker-host-1 ~]#
[root@docker-host-1 ~]# ls -lrt /var/lib/docker
total 0
[root@docker-host-1 ~]# ls -lrt /home/thiru/docker/
total 4
drwx--x--x. 3 root root 20 Feb 18 14:21 containerd
drwx------. 4 root root 32 Feb 18 14:21 plugins
drwx------. 3 root root 22 Feb 18 14:21 image
drwx------. 2 root root 6 Feb 18 14:21 trust
drwxr-x---. 3 root root 19 Feb 18 14:21 network
drwx------. 2 root root 6 Feb 18 14:21 swarm
drwx------. 2 root root 24 Feb 18 14:21 builder
drwx------. 4 root root 169 Feb 21 08:43 volumes
drwx------. 5 root root 222 Mar 3 12:39 containers
drwx------. 2 root root 6 May 3 07:57 tmp
drwx------. 2 root root 6 May 3 07:57 runtimes
drwx------. 40 root root 4096 May 3 07:57 overlay2
[root@docker-host-1 ~]#

C) Restart the docker service,

[root@docker-host-1 ~]# systemctl restart docker
[root@docker-host-1 ~]#

Lets start any one of our containers and see the docker’s root directory,

[root@docker-host-1 ~]# docker start a03199172063
a03199172063
[root@docker-host-1 ~]#
[root@docker-host-1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a03199172063 nginx "nginx -g 'daemon of…" 2 months ago Up 2 seconds 80/tcp test-nginx
[root@docker-host-1 ~]# df -Th | grep -i docker
overlay overlay 6.2G 4.3G 1.9G 70% /home/thiru/docker/overlay2/6960599597e61..4a326/merged
shm tmpfs 64M 0 64M 0% /home/thiru/docker/containers/a0319917206..619d03a/shm
[root@docker-host-1 ~]#

So we have changed the docker root directory from ‘/var/lib/docker’ to ‘/home/thiru/docker’

 

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: