Change docker root directory by creating a systemd drop-in service file

William L'Archevêque
2 min readOct 18, 2019

--

TLDR; It is possible to change docker root directory by creating a systemd drop-in service file. This is useful when you want images and containers or docker generated files to be located in another partition or drive.

Changing docker root directory and container / images location

We build and use different images on our current project, and they can take up a lot of space on my drive. It happens that I have two different partitions on my hard drive, one for the OS “/” (25 GB) and another one for my home directory “/home” (362 GB). While building my latest project, it seems that the first partition got filled up rapidly until there was no space left.

Creating a systemd drop-in service file

A unit configuration file whose name ends in .service encodes information about a process controlled and supervised by systemd. Docker has one of those.

You should avoid modifying a .service file directly as it could be overwritten during an update. This is why it is preferred to create a systemd drop-in service file. Drop-in service file only changes the specific settings one is interested in. Note that multiple drop-in files are read if present.

Packages typically ship unit files in /lib/systemd/system/. These should not be edited. Instead, systemd allows you to override these files by creating appropriate files in /etc/systemd/system/.

Create drop-in configuration file

  1. Create the systemd directory:
    sudo mkdir /etc/systemd/system/docker.service.d/
  2. Create the docker.service drop-in file:
    sudo nano /etc/systemd/system/docker.service.d/docker.root.conf
  3. Add the following configuration :
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -g /your/custom/docker/root -H fd://

Reload the daemon and restart docker

  1. sudo systemctl daemon-reload
  2. sudo systemctl restart docker
  3. You can confirm the root directory has been modified :
    docker info
    Docker Root Dir: /your/custom/docker/root

Existing Containers and Images

You may already have containers or images located in /var/lib/docker. If you wish you can stop and back these up before moving them to the new root directory. You can either sync folders withrsync -a /var/lib/docker/* /your/custom/docker/root or use the mv or cp commands (if permissions do not matter).

--

--

William L'Archevêque

Full-Stack Web Developer. Enthusiast Helper. Freelancer for 7 years, now having fun working at Centiva. Specialized in Laravel PHP, AWS Cloud and cartwheeling.