Change directory to store containers and images in Docker

As discussed in one of the previous posts, docker is more focused on ease of use and not so much on the disk space efficiency out of the box. However, it is our responsibility to maintain adequate free space on hard disk and also do some kind of standardization. DevOps is not only about automation but it is also about standardization of process thereby removing any gotchas or discrepancy in the configuration. For example, we may like to store docker images in a separate directory that may be located on a mounted device storage or some other easy to navigate configuration.

By default, docker store containers and images in /var/lib/docker by default:

Default directory for containers and images in Docker
Default directory for containers and images in Docker

The location of image may vary on which version of storage driver associated with docker you are running on. The same info can be obtained by using docker info:

Output of docker info
Output of docker info

By default this will be aufs but can fall back to overlay, overlay2, btrfs, devicemapper or zfs depending on your kernel support. In most places this will be aufs but the RedHat uses devicemapper.

In the case of aufs:

/var/lib/docker/aufs/diff/<id> has the file contents of the images.
/var/lib/docker/repositories-aufs is a JSON file containing local image information. This can be viewed with the command docker images.

In the case of devicemapper:

/var/lib/docker/devicemapper/devicemapper/data stores the images
/var/lib/docker/devicemapper/devicemapper/metadata stores the metadata

Eventually, /var partition will start filling up as you use more and more images from repository. To change, we need to edit /etc/default/docker file and set DOCKER_OPTS with -g followed by new location. For example, in our case we created a new directory as /dockercontainers and then modify the file to look as below:

Modified docker configuration file
Modified docker configuration file

We then restart docker daemon or service by using appropriate command to our Operating system. For example, we would be using systemctl restart docker on Linux based distributions.

From now onwards, the newly created Docker containers and images would be stored in the new directory.


One thought on “Change directory to store containers and images in Docker

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s