Sub topic 10
The docker group and rootless mode
This chapter assumes you have installed Docker Engine on Linux and finished the post-install steps. You should be comfortable with sudo, Linux users and groups, and systemd services.
Why the docker group is powerful
The Docker CLI talks to the daemon through a Unix socket at /var/run/docker.sock. On a default install the daemon runs as root, and the socket belongs to the docker group. Any member of that group can send commands to a root process.
A container can mount any host path, so a group member can read or change any file on the machine. You can see this yourself:
docker run --rm -v /etc:/host-etc:ro alpine head -n 1 /host-etc/shadowThe command prints the first line of /etc/shadow, a file only root can normally read. The container process runs as root, and the bind mount exposes the host's files to it.
What rootless mode changes
Rootless mode runs both the daemon and the containers inside a user namespace owned by an ordinary user. Root inside a container maps to your unprivileged UID on the host. A container escape or a malicious mount then gives an attacker only what your user account can already do.
Setting up rootless mode on Ubuntu
You need the docker-ce-rootless-extras package, which the install chapter's package list includes, and the uidmap package. uidmap provides newuidmap and newgidmap, which map user IDs into the namespace. Your user also needs entries in /etc/subuid and /etc/subgid. Ubuntu creates these when it creates the account, and you can check with grep "$USER" /etc/subuid /etc/subgid.
Run the setup as the regular user, not as root:
sudo apt-get install -y uidmap docker-ce-rootless-extras
sudo systemctl disable --now docker.service docker.socket
dockerd-rootless-setuptool.sh install
sudo loginctl enable-linger "$USER"The second command stops the system-wide daemon so the two do not compete. The setup tool creates a systemd user service. The loginctl enable-linger command keeps that service running after you log out and starts it at boot.
The tool prints an export line. Add it to your shell profile so the CLI finds the rootless socket:
export DOCKER_HOST=unix:///run/user/$(id -u)/docker.sockCheck the result:
docker info --format '{{.SecurityOptions}}'
docker run --rm -v /etc:/host-etc:ro alpine head -n 1 /host-etc/shadowThe first command should list name=rootless. The second now fails with "Permission denied", because container root is no longer host root.
Trade-offs of rootless mode
Rootless mode has real limits:
- Privileged ports. Unprivileged users cannot bind ports below 1024. Publishing
-p 80:80fails unless you lower the limit withsudo sysctl net.ipv4.ip_unprivileged_port_start=80, which affects the whole host. - Separate storage. Images and containers live under
~/.local/share/docker. They do not appear in the root daemon's/var/lib/docker, so you must pull images again. - Networking. Traffic goes through a user-space network stack, which is slower than the default bridge. Containers also see the original client address less reliably.
- Feature gaps. Some storage drivers, cgroup limits, and
--privileged-style host access behave differently or are unavailable.
| Question | docker group | Rootless mode |
|---|---|---|
| Daemon runs as | root | your user |
| Container root maps to | host root | your UID |
| Ports below 1024 | work | need a sysctl change |
| Setup effort | one command | several steps plus `DOCKER_HOST` |
| Best for | single-user machines you trust | shared hosts, CI runners, untrusted images |
Choosing between them
On a personal workstation where you are the only user, the docker group is common. Accept that it equals root and keep the machine's other accounts out of the group. On a shared server, a build host that runs third-party code, or any place where privilege separation matters, prefer rootless mode. Using sudo docker does not remove the risk. It only makes the privilege escalation visible.
Exercise
A teammate says, "I don't want to give the CI user root, so I'll just add it to the docker group." What is wrong with this reasoning, and what would you suggest instead?
Show answer
Membership in the docker group lets the user control a root-owned daemon. It can start a container that mounts the host's root filesystem and read or modify any file, so the CI user effectively has root. Run Docker in rootless mode for that user instead, where container root maps to the CI user's own UID. Then confirm it with docker info --format '{{.SecurityOptions}}', which should list name=rootless.
Exercise
After installing rootless Docker, docker ps still shows containers from the old system-wide daemon. What most likely went wrong?
Show answer
The DOCKER_HOST variable is not set in your shell, so the CLI is still using /var/run/docker.sock, which belongs to the root daemon. Export DOCKER_HOST=unix:///run/user/$(id -u)/docker.sock, add it to your shell profile, and run docker info to confirm.