« Previous | Next »

Changing data root directory in rootless Docker

22 Feb 2023

I switched my Linux laptop to rootless Docker recently.

Running as root, by default Docker stores its data in /var/lib/docker. Switching over to rootless Docker, the equivalent location is now under my home directory.

My laptop is a two-disk setup (see my write up on dual booting Windows 10 and Xubuntu), with separate LVM volume groups (VGs) for home, var and tmp. The largest usage of var was due to Docker. With rootless Docker, I still want Docker to use var and not my home directory for its stuff.

I had set up rootless Docker with systemd, as recommended by Docker documentation, which also says:

  • The data dir is set to ~/.local/share/docker by default.
  • The daemon config dir is set to ~/.config/docker by default.

With systemd, rootless Docker's config file is in ~/.config/systemd/user/docker.service. The first three lines of the service stanza in that file looks like this:

[Service]
Environment=PATH=<blah blah blah>
ExecStart=/bin/dockerd-rootless.sh
ExecReload=/bin/kill -s HUP $MAINPID

Line 2, ExecStart tells us that rootless Docker is executed by /bin/dockerd-rootless.sh, which, by naming convention, is a shell script. And, helpfully, the comment block at the top of that file tells what it does:

#!/bin/sh
# dockerd-rootless.sh executes dockerd in rootless mode.
#
# Usage: dockerd-rootless.sh [DOCKERD_OPTIONS]

So this script, /bin/dockerd-rootless.sh, takes DOCKERD_OPTIONS. And what might those be? Docker's documentation says:

Usage: dockerd COMMAND

A self-sufficient runtime for containers.

Options:
      ...<options in alphabetical order>...
      --data-root string                      Root directory of persistent Docker state (default "/var/lib/docker")

Aha! Putting it together, the way to set data-root directory for rootless Docker is to modify the ExecStart key in ~/.config/systemd/user/docker.service, like this:

[Service]
Environment=PATH=<blah blah blah>
ExecStart=/bin/dockerd-rootless.sh --data-root /var/lib/docker-1000
ExecReload=/bin/kill -s HUP $MAINPID

As root, I created /var/lib/docker-1000 and then chown 1000:1000 it, to serve as my rootless Docker setup's data root directory. Restarted rootless Docker, and it now uses the new data root directory:

% systemctl --user stop docker
% systemctl --user daemon-reload
% systemctl --user start docker
% docker info | egrep "Root Dir"
 Docker Root Dir: /var/lib/docker-1000
Tags: Docker