How to set the default Docker data directory
If you’ve been using Docker for a while you’ve already seen its appetite for
storage. All the images, layers, containers, etc. are stored on its default
data directory /var/lib/docker
:
/var/lib/docker/
├── buildkit
├── containers
├── image
├── network
├── nuke-graph-directory.sh
├── overlay2
├── plugins
├── runtimes
├── swarm
├── tmp
├── trust
└── volumes
As you keep building your images, running your containers and mounting your
application’s volumes all those folders grow and grow and, since they are on
the root partition /
, it is quite easy to run out of space an ruin your system.
This post shows how to move and configure the Docker data directory. Docker recommends to use fast drives, as SSD, to store this data, but it can be actually installed on other types of hard drives.
Since it is likely that you want to keep your current volumes, images, etc. first
we need to copy the default data directory /var/lib/docker
to the new location;
to do so we need to stop de Docker Daemon:
sudo service docker stop # Assuming we are on a Linux machine
After that, we need to configure the Docker Daemon to use the new location by
editing its configuration file located in /etc/docker/daemon.json
and by adding
the data-root
property. Here [1] you can se a full configuration file with
all its possible parameters:
{
"data-root": "/path/to/your/data/docker"
}
Now let’s copy the current Docker data folder:
sudo rsync -aP /var/lib/docker/ /path/to/your/data/docker
Finally, let’s restart the Docker Daemon:
sudo service docker start
To check if Docker is using the new configuration, just execute:
docker info
And look for the “Docker Root Dir: «path»” entry.