Sub topic 6
Installing and setting up the rootless daemon
This chapter walks through installing the rootless daemon and confirming that it works. It assumes you know basic Docker usage, the Linux command line, Linux users and permissions, and simple systemd services. It also assumes the host checks from the previous chapter passed: your user has a range of at least 65536 subordinate UIDs and GIDs, and newuidmap and newgidmap are installed.
What the setup involves
Rootless Docker is a normal Docker daemon that runs inside a user namespace under your own account. The install has three parts:
- Install the rootless extras package, which contains the setup tool.
- Run the setup tool as your unprivileged user.
- Start the daemon and point the client at it.
The setup tool does the configuration work. It creates a systemd user unit for the daemon and a Docker CLI context named rootless.
Verify the prerequisites
Run these checks first. The first command should print one line per file for your user. The second should print two paths.
# Check that the current user has subordinate ID ranges
grep "^$(whoami):" /etc/subuid /etc/subgid
# Check that the helper binaries are installed
command -v newuidmap newgidmapIf either command prints nothing, fix that before continuing. On most distributions the uidmap package provides the helpers.
Run the setup tool
Install the Docker packages for your distribution, including the rootless extras package. Then run the setup tool as your own user.
# Run as the unprivileged user, not with sudo
dockerd-rootless-setuptool.sh installDo not use sudo. Running it as root creates the unit and the data in the wrong place and defeats the purpose. The tool prints the environment variables it recommends, so keep that output.
Start the daemon
The daemon runs as a systemd user service. Start it and enable it so it comes back after a reboot.
systemctl --user start docker
systemctl --user enable docker
# Keep the daemon running after you log out
sudo loginctl enable-linger "$(whoami)"Lingering tells systemd to keep your user manager running without an active login session. Without it, the daemon stops when your last session ends, and it does not start at boot.
Connect the client and verify
The client reaches the daemon through a socket in your runtime directory. Set DOCKER_HOST, or use the context the setup tool created.
export DOCKER_HOST=unix://$XDG_RUNTIME_DIR/docker.sock
# Alternatively, select the context created by the setup tool
docker context use rootless
docker info --format '{{.SecurityOptions}}'The output of the last command should include rootless. If it does not, your client is probably still talking to a rootful daemon at /var/run/docker.sock.
Run a test container
docker run --rm alpine id
docker run -d --name web -p 8080:80 nginx
curl -I http://localhost:8080The first command prints uid=0(root). That is root inside the user namespace, which maps to your unprivileged user on the host. The published port 8080 works because it is above 1023. Ports below 1024 need extra configuration, which a later chapter covers.
Common mistakes
- Running the setup tool with
sudo, which installs the wrong thing for the wrong user. - Skipping
enable-linger, so the daemon disappears after logout. - Leaving
DOCKER_HOSTunset, sodockertalks to the rootful daemon instead. - Setting
DOCKER_HOSTto a socket path that does not match$XDG_RUNTIME_DIR, which fails with a connection error.
Exercise
You ran the setup tool and started the daemon, but docker info --format '{{.SecurityOptions}}' does not mention rootless, and the daemon stops each time you log out. What two fixes do you apply?
Show answer
Point the client at the rootless daemon by exporting DOCKERHOST=unix://$XDGRUNTIME_DIR/docker.sock or running docker context use rootless. Then run sudo loginctl enable-linger "$(whoami)" so the systemd user manager, and the daemon with it, keeps running without a login session.