Join our Discord Server
Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 570+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 8900+ members and discord server close to 2200+ members. You can follow him on Twitter(@ajeetsraina).

How to change Docker Image Installation Directory?

2 min read

Are you running out of space while creating more and more containers? Here is a guide to change the default docker installation directory so that you can place the docker containers to other directory of your choice.

By default, Docker places the containers under /var/lib/docker/containers which you can easily know through the following command:

[root@localhost docker]# docker info
Containers: 5
Images: 71
Storage Driver: devicemapper
Pool Name: docker-253:1-54522732-pool
Pool Blocksize: 65.54 kB
Data file: /var/lib/docker/devicemapper/devicemapper/data
Metadata file: /var/lib/docker/devicemapper/devicemapper/metadata
Data Space Used: 2.701 GB
Data Space Total: 107.4 GB
Metadata Space Used: 4.305 MB
Metadata Space Total: 2.147 GB
Library Version: 1.02.84-RHEL7 (2014-03-26)
Execution Driver: native-0.2
Kernel Version: 3.10.0-123.el7.x86_64
Operating System: CentOS Linux 7 (Core)
Username: ajeetraina
Registry: [https://index.docker.io/v1/]

In case you have dozens of containers running leaving your host machine out of free space, as shown below:

[root@localhost ajeetraina]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 11G 10G 522M 96% /
devtmpfs 389M 0 389M 0% /dev
tmpfs 398M 92K 398M 1% /dev/shm
tmpfs 398M 5.8M 392M 2% /run
tmpfs 398M 0 398M 0% /sys/fs/cgroup
/dev/sda1 497M 129M 369M 26% /boot
/dev/sdb1 9.8G 323M 8.9G 4% /mnt/disk

The / directory is almost full. One solution could be adding a new virtual disk(in case you are running it under VM), partioning it and mounting it under /mnt/disk. The above df -h shows the exact scenerio where I have a new disk /dev/sdb1 added and mounted under /mnt/disk/. Now I can use /mnt/disk/docker folder to store all my containers.

Here are the instructions to make it possible.

1. Stop the docker service

#systemctl stop docker.service

2. Edit /etc/sysconfig/docker as shown:

cat /etc/sysconfig/docker
# /etc/sysconfig/docker

# Modify these options if you want to change the way the docker daemon runs
OPTIONS=–selinux-enabled -H fd:// -g /mnt/disk/docker -p /var/run/docker.pid
# Location used for temporary files, such as those created by
# docker load and build operations. Default is /var/lib/docker/tmp
# Can be overriden by setting the following environment variable.
# DOCKER_TMPDIR=/var/tmp

All I did is added a line -g /mnt/disk/docker -p /var/run/docker.pid to the OPTIONS.

3. Restart the docker service:

#systemctl start docker

[root@localhost docker]# docker info
Containers: 0
Images: 0
Storage Driver: devicemapper
Pool Name: docker-8:17-475139-pool
Pool Blocksize: 65.54 kB
Data file: /mnt/disk/docker/devicemapper/devicemapper/data
Metadata file: /mnt/disk/docker/devicemapper/devicemapper/metadata
Data Space Used: 307.2 MB
Data Space Total: 107.4 GB
Metadata Space Used: 729.1 kB
Metadata Space Total: 2.147 GB
Library Version: 1.02.84-RHEL7 (2014-03-26)
Execution Driver: native-0.2
Kernel Version: 3.10.0-123.el7.x86_64
Operating System: CentOS Linux 7 (Core)
Username: ajeetraina
Registry: [https://index.docker.io/v1/]

As shown above, now the Data file shows /mnt/disk/docker as the destination container storage location.

Also, you will notice that there will be a repo created as soon as you restart the docker service as shown:

[root@localhost docker]# ls -lrt
total 44
drwx——. 2 root root 4096 May 9 08:12 tmp
drwx——. 4 root root 4096 May 9 08:12 devicemapper
drwx——. 2 root root 4096 May 9 08:12 containers
drwx——. 2 root root 4096 May 9 08:12 graph
drwx——. 2 root root 4096 May 9 08:12 volumes
drwx——. 2 root root 4096 May 9 08:12 trust
-rw——-. 1 root root 19 May 9 08:12 repositories-devicemapper
-rw-r–r–. 1 root root 5120 May 9 08:12 linkgraph.db
drwx——. 2 root root 4096 May 9 08:12 init
drwx——. 3 root root 4096 May 9 08:12 execdriver
[root@localhost docker]# pwd
/mnt/disk/docker
[root@localhost docker]#

In case you already have 100s of containers and you need to take care of them too., then follow the below steps to backup them:

1) Stop docker: service docker stop. Verify no docker process is running ps faux
2) Double check docker really isn’t running. Take a look at the current docker directory: ls /var/lib/docker/
2b) Make a backup – tar -zcC /var/lib docker > /mnt/pd0/var_lib_docker-backup-$(date +%s).tar.gz
3) Move the /var/lib/docker directory to your new partition: mv /var/lib/docker /mnt/pd0/docker
4) Make a symlink: ln -s /mnt/pd0/docker /var/lib/docker
5) Take a peek at the directory structure to make sure it looks like it did before the mv: ls /var/lib/docker/ (note the trailing slash to resolve the symlink)
6) Start docker back up service docker start
7) restart your containers

Have Queries? Join https://launchpass.com/collabnix

Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 570+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 8900+ members and discord server close to 2200+ members. You can follow him on Twitter(@ajeetsraina).
Join our Discord Server