How to change Docker Image default Installation Directory

How to change Docker Image default Installation Directory

By default, Docker places the containers under /mnt/doc/docker/containers which you can get from docker info command as “Docker Root Directory”. Normally this may cost, make your server running out of space while you are creating more and more containers. To avoid such a case, we can change the default installation path to preferable one or separate directory. Will see how to change the docker default installation directory.

root@ubuntu:~# docker info

docker info

Sometimes, if you are using much containers or your server space is limited, you may run out od space. You can keep check the server space using df command.

root@ubuntu:~# df -h
 Filesystem                   Size  Used Avail Use% Mounted on
udev                         2.4G  4.0K  2.4G   1% /dev
tmpfs                        496M   52M  444M  11% /run
/dev/mapper/ubuntu--vg-root   30G   28G    2G  98% /
tmpfs                        2.5G  188K  2.5G   1% /dev/shm
tmpfs                        5.0M     0  5.0M   0% /run/lock
tmpfs                        2.5G     0  2.5G   0% /sys/fs/cgroup
/dev/sda1                    472M  462M     0 100% /boot
/dev/sda1                    200G    8G  192G   4% /mnt/doc
tmpfs                        496M     0  496M   0% /run/user/111
tmpfs                        496M     0  496M   0% /run/user/1000

The / directory is almost full. We can fix by adding a new virtual disk (in case you are running it under VM), partioning it and mounting it under /mnt/doc. You can see the status of current disk space, and where I have a new disk /dev/sdb1 added and mounted under /mnt/doc/. Now I can use /mnt/doc/docker folder to store all my containers.

Step 1: Stop the docker service

root@ubuntu:~# /etc/init.d/docker stop

Step 2: Change the Directory

  • Ubuntu/Debian: edit your /etc/default/docker file with the -g option: DOCKER_OPTS=”-dns -dns -g /mnt/doc”
  • Fedora/Centos: edit /etc/sysconfig/docker, and add the -g option in the other_args variable: ex. other_args=”-g /mnt/doc/”. If there’s more than one option, make sure you enclose them in ” “. After a restart, (service docker restart) Docker should use the new directory.

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

Caution – These steps depend on your current /mnt/doc/docker being an actual directory (not a symlink to another location).

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 /mnt/doc/docker/
2a) Make a backup – tar -zcC /var/lib docker > /mnt/doc/var_lib_docker-backup-$(date +%s).tar.gz
3) Move the /mnt/doc/docker directory to your new partition: mv /mnt/doc/docker /mnt/doc/docker
4) Make a symlink: ln -s /mnt/doc/docker /mnt/doc/docker
5) Take a peek at the directory structure to make sure it looks like it did before the mv: ls /mnt/doc/docker/ (note the trailing slash to resolve the symlink)
6) Start docker back up service docker start
7) restart your containers

Step 3: Restart the docker service:

root@ubuntu:~# service docker start
root@ubuntu:~# docker info
 Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 0
Server Version: 1.12.2
Storage Driver: aufs
 Root Dir: /mnt/doc/docker/aufs
 Backing Filesystem: extfs
 Dirs: 363
 Dirperm1 Supported: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
 Volume: local
 Network: null host bridge overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Security Options: apparmor seccomp
Kernel Version: 4.4.0-47-generic
Operating System: Ubuntu 16.04.1 LTS
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 4.835 GiB
Name: ubuntu                                                     
Docker Root Dir: /mnt/doc/docker
Debug Mode (client): false
Debug Mode (server): false
WARNING: No swap limit support
Insecure Registries:

Now the Data file shows /mnt/doc/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

root@ubuntu:~# ls -lrt
 total 34
 drwx——. 2 root root 4096 Apr 25 07:24 tmp
 drwx——. 4 root root 4096 Apr 25 07:24 devicemapper
 drwx——. 2 root root 4096 Apr 25 07:24 containers
 drwx——. 2 root root 4096 Apr 25 07:24 graph
 drwx——. 2 root root 4096 Apr 25 07:24 volumes
 drwx——. 2 root root 4096 Apr 25 07:24 trust
 -rw——-. 1 root root 19 Apr 25 07:24 repositories-devicemapper
 -rw-r–r–. 1 root root 5120 Apr 25 07:24 linkgraph.db
 drwx——. 2 root root 4096 Apr 25 07:24 init
 drwx——. 3 root root 4096 Apr 25 07:24 execdriver