Reading Time: < 1 minute

Docker uses (/var/lib/docker) as default root directory to provide storage. We will see how to change this root directory to some other directory, let’s say ‘/home/techrunnr/docker’.
Lets login to our docker server and see the running containers through below command,

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

Now we will see the running containers respective file-system as below

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

 

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

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

Stop all running docker containers and then docker daemon. Move “/var/lib/docker” directory to the place where you want to have this data.

[root@techrunnr-1 ~]# systemctl stop docker
 [root@techrunnr-1 ~]#
[root@techrunnr-1 ~]# mv /var/lib/docker/* /home/techrunnr/docker/
 [root@techrunnr-1 ~]#

and then create symlink for this docker directory in /var/lib path:


 [root@techrunnr-1 ~]# ls -lrt /var/lib/docker
 total 0
 [root@techrunnr-1 ~]# ls -lrt /home/techrunnr/docker/
 total 4
 drwx--x--x. 3 root root 20 Nov 28 16:35 containerd
 drwx------. 4 root root 32 Nov 28 16:35 plugins
 drwx------. 3 root root 22 Nov 28 16:35 image
 drwx------. 2 root root 6 Nov 28 16:35 trust
 drwxr-x---. 3 root root 19 Nov 28 16:35 network
 drwx------. 2 root root 6 Nov 28 16:35 swarm
 drwx------. 2 root root 24 Nov 28 16:35 builder
 drwx------. 4 root root 169 Nov 26 06:23 volumes
 drwx------. 5 root root 222 Mar 3 11:17 containers
 drwx------. 2 root root 6 Dec 1 05:15 tmp
 drwx------. 2 root root 6 Dec 1 05:15 runtimes
 drwx------. 40 root root 4096 Dec 1 05:15 overlay2
 [root@techrunnr-1 ~]#

Since SELinux is enabled for the production environment, Docker will need permission to write into the new directory. We need to change the SELinux file contexts when using a custom Docker Data root, by default in RHEL/Centos provides SELinux file context default docker data root directory /var/lib/docker, and other locations.

In order to make the changes in the SELinux file context types in custom Docker data root, we should execute the below commands:

semanage fcontext -a -e /var/lib/docker /home/techrunnr/docker/
restorecon -R -v /home/techrunnr/docker/

Start docker daemon and containers.

systemctl daemon-reload
systemctl restart docker

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

[root@techrunnr-1 ~]# docker start b05158168037
 b05158168037
 [root@techrunnr-1 ~]#
 [root@techrunnr-1 ~]# docker ps
 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
 b05158168037 nginx "nginx -g 'daemon of…" 1 months ago Up 2 seconds 80/tcp ngin-testimage
 [root@techrunnr-1 ~]# df -Th | grep -i docker
 overlay overlay 5.2G 5.3G 1.9G 70% /home/techrunnr/docker/overlay2/7860342534e51..4a326/merged
 shm tmpfs 64M 0 64M 0% /home/techrunnr/docker/containers/b031568206..476s06b/shm
 [root@techrunnr-1 ~]#

Now we have changed the docker root directory from ‘/var/lib/docker’ to ‘/home/techrunnr/docker/ and set SELinux file contexts for new Docker data root.

 

© 2019, Techrunnr. All rights reserved.

#1
#2
#3
Questions Answered
Articles Written
Overall Points

0 Comments

Leave a Reply