More Space for Docker Images

Have you experienced running out of disk space because of Docker images taking too much space? In this post I will show you how to configure Docker host to store images in a dedicated location.

By default Docker daemon stores its images together with data volumes and other containers-related stuff in /var/lib/docker on the host system. This directory most likely sits in the system’s root partition. Many servers use cheaper storage option for the root partition, like smaller rotating hard-drives or even USB sticks. These are usually sufficient for booting up Linux and accessing the filesystem here and there, but they're typically not big and fast enough to working with many archives of full-blown Linux distributions, which Docker images often are.

There are two ways how to get Docker use an alternative location for storing its data. One way is to use --graph="/mnt/docker-data" parameter with the Docker Daemon, like in the following snippet of systemd configuration:

ExecStart=/usr/bin/docker daemon -H fd:// --graph /mnt/docker-data --storage-driver btrfs  

Another way is to use fstab. In that case you only need to pass the following line to /etc/fstab file and mount the new location using mount -a:

/mnt/docker-data /var/lib/docker none bind 0 0

Before we can use Docker with this new setup, the existing directory has to be migrated. In order to do so, we stop the Docker daemon, create the mount directory in /mnt/docker-data, move the data and eventually start the daemon. Like this:

sudo service docker stop  
sudo mkdir /mnt/docker-data  
sudo rsync -aXS /var/lib/docker/. /mnt/docker-data/  
sudo service docker start  

In order to verify our new setup works, start pulling an image and watch the image layers being downloaded into the right location:

sudo watch ls -al /mnt/docker-data/graph/  

Note: My hope is that over time people will eventually start using base images with a smaller footprint (Alpine Linux) instead of the ubiquitous Ubuntu. A full-blown operating system is rarely useful inside a container.