Sub topic 8
Pointing the Docker CLI at the rootless daemon
This chapter assumes you have installed the rootless daemon with dockerd-rootless-setuptool.sh install and started it with systemctl --user start docker. It also assumes you know what a Unix socket is and how shell environment variables work.
Why the client needs redirecting
The Docker CLI is only a client. By default it talks to unix:///var/run/docker.sock, which belongs to the rootful daemon. The rootless daemon listens on a different socket inside your runtime directory, so the client must be told where to connect. If you skip this step, you get an error like this:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?The socket is at $XDG_RUNTIME_DIR/docker.sock. On most systemd hosts, XDG_RUNTIME_DIR is /run/user/<uid>. For UID 1000 the socket is /run/user/1000/docker.sock.
Option 1: the DOCKER_HOST variable
Set DOCKER_HOST to the socket path. Every docker command in that shell then uses the rootless daemon.
export DOCKER_HOST=unix://$XDG_RUNTIME_DIR/docker.sock
docker info --format '{{.SecurityOptions}}'The output should include name=rootless. If it does not, the client is still talking to a rootful daemon. Add the export line to ~/.bashrc or your shell's equivalent to make it permanent.
Option 2: a Docker context
A Docker context is a named set of connection settings stored by the CLI. The setup tool creates one called rootless. Switch to it with docker context use:
docker context ls
docker context use rootless
docker info --format '{{.SecurityOptions}}'The setting persists across shells, so you do not need to edit any profile file. To run one command against a different daemon without switching, pass the context by name:
docker --context rootless ps
docker --context default psWhich setting wins
The client checks several places and uses the first one that is set. DOCKER_HOST outranks the active context, so a stale export in your profile can silently override docker context use. The CLI warns about this in docker context ls.
Choosing between them
| Approach | Scope | Best for |
|---|---|---|
| `DOCKER_HOST` | Current shell, or every shell if set in a profile | Scripts and CI jobs that need an explicit endpoint |
| `docker context use` | Persistent for your user | Day-to-day work, switching between daemons |
| `--context` flag | One command | Occasional comparisons |
Verifying the connection
Run a container and check the user ID it reports:
docker run --rm alpine idIt prints uid=0(root). That is root inside the user namespace only. On the host, the process runs as your unprivileged user.
Common errors
XDG_RUNTIME_DIRis empty. This happens aftersuorsudo -u, which do not start a proper login session. The socket path then becomesunix:///docker.sock. Log in directly, or usesshormachinectl shell.- Using
sudo docker. This runs the client as root, with root's environment, so it reaches the rootful daemon or none at all. Rundockeras your own user. - Socket not found. The daemon is not running. Check with
systemctl --user status docker. - Wrong daemon, no error. A leftover
DOCKER_HOSTpoints at the rootful socket. Runecho "$DOCKER_HOST"anddocker context lsto see which setting is active.
Exercise
You ran docker context use rootless, but docker info still shows a rootful daemon. Your ~/.bashrc contains export DOCKER_HOST=unix:///var/run/docker.sock. Why does this happen, and how do you fix it?
Show answer
DOCKER_HOST takes precedence over the active context, so the context selection is ignored. Remove the line from ~/.bashrc, run unset DOCKER_HOST in the current shell, and check again with docker info --format '{{.SecurityOptions}}'. The output should now include name=rootless.