basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    Isolating root in Docker: userns-remap and Rootless Docker

    By default, a uid 0 process inside a container runs as the real root on the host — if an attacker escapes through a kernel bug, they own the server. userns-remap shifts container UIDs into an unprivileged range, while Rootless Docker also moves the dockerd daemon itself into a regular user’s namespace.

    How they compare: userns-remap vs Rootless Docker

    Standard Docker runs containers without a separate user namespace: /proc/<pid>/uid_map reads 0 0 4294967295. A process running as uid 0 inside a container shows up as root in ps on the host. Settings from Hardening containers in Compose (cap_drop, read_only, no-new-privileges) strip what that process can do, but don’t change its host UID.

    A user namespace shifts the ID map: container uid 0 becomes, say, 100000 on the host, uid 1 becomes 100001, and so on across 65,536 IDs. Inside the container the program still sees itself as root; to the host kernel it’s an unprivileged user with no access to system files.

    Docker offers two ways to enable this:

    Standard Docker userns-remap Rootless Docker
    Who runs dockerd root root regular user
    Container uid 0 on host 0 (root) 100000 (from /etc/subuid) user’s UID plus their /etc/subuid
    Ports 80 and 443 out of the box out of the box needs ip_unprivileged_port_start = 80
    Access to docker.sock root on host (docker.sock) root on host privileges of that user only
    Per-container opt-out — userns_mode: host none
    Real client IP in container visible visible sees 127.0.0.1 without extra setup (slirp4netns)

    Which one to pick:

    • userns-remap — for a VPS running Traefik and CrowdSec: ports 80 and 443 work immediately, real client IPs come through intact, and a host-level container like a monitoring agent can opt out with userns_mode: host.
    • Rootless Docker — when dockerd itself must not run as root (a shared dev box, or a server where an unprivileged user runs containers without sudo).

    Always start container hardening with Hardening containers in Compose: those options don’t touch existing volumes or images. Enable userns-remap or Rootless Docker on a fresh server before deploying stacks.

    Option 1: enable userns-remap

    1. Add userns-remap to /etc/docker/daemon.json

      If /etc/docker/daemon.json already configures log limits (Docker on Ubuntu), add the line to the same JSON object:

      {
        "userns-remap": "default"
      }
    2. Restart Docker and check the assigned UID range

      sudo systemctl restart docker
      grep dockremap /etc/subuid /etc/subgid
      # /etc/subuid:dockremap:100000:65536
      # /etc/subgid:dockremap:100000:65536

      Docker creates the dockremap system user automatically and allocates a 65,536-ID range (the start offset is usually 100000 or 165536 — check your output). Container uid 0 now maps to host 100000, and uid 33 (www-data) maps to 100033.

    3. Update ownership on bind-mounted host directories

      Docker creates named volumes with the right owner already. Host bind mounts (volumes: - ./data:/data) need the shifted UID:

      sudo chown -R 100000:100000 /opt/site/data

      If the service inside runs as non-root (say uid 33 or 999), add the /etc/subuid base offset to the process UID: 100000 + 33 = 100033.

    4. Keep an exception for a host-networked container

      A container with network_mode: host won’t start under userns-remap. If one service (such as a monitoring agent) needs host networking, disable remapping for that container only:

      services:
        agent:
          image: ghcr.io/example-user/agent:1.0
          network_mode: host
          userns_mode: host

    Option 2: Rootless Docker

    Rootless Docker requires Docker from the official repository (docker-ce and docker-ce-rootless-extras, as in Docker on Ubuntu): Ubuntu’s docker.io package doesn’t ship the rootless scripts.

    1. Install dependencies and stop the system daemon

      sudo apt install -y uidmap dbus-user-session slirp4netns docker-ce-rootless-extras
      sudo systemctl disable --now docker.service docker.socket
      sudo rm -f /var/run/docker.sock

      If /var/run/docker.sock stays accessible, the setup script aborts with Aborting because rootful Docker (/var/run/docker.sock) is running and accessible.

      Your user needs entries in /etc/subuid and /etc/subgid (Ubuntu creates them for new accounts; check with grep "^$USER:" /etc/subuid /etc/subgid).

      Ubuntu 24.04 enables kernel.apparmor_restrict_unprivileged_userns = 1, and its built-in /etc/apparmor.d/rootlesskit profile already allows user namespaces for /usr/bin/rootlesskit. Installing docker-ce-rootless-extras via apt requires no AppArmor edits; you only need a custom profile if a script installed rootlesskit into your home directory (~/bin/rootlesskit).

    2. Install the user daemon and enable linger

      Run as the regular user (without sudo) in a direct SSH login — not via su, or the user systemd session won’t start:

      dockerd-rootless-setuptool.sh install
      sudo loginctl enable-linger "$USER"

      The script creates ~/.config/systemd/user/docker.service, starts the daemon, and switches the CLI to the rootless context. loginctl enable-linger starts the daemon on reboot and keeps it running after you close SSH.

      For tools that don’t read Docker contexts, append the user socket path to ~/.bashrc:

      export DOCKER_HOST=unix:///run/user/$(id -u)/docker.sock
    3. Open ports 80 and 443 if containers need them

      By default net.ipv4.ip_unprivileged_port_start is 1024, so ports: - "80:80" and "443:443" fail with permission denied. If this server runs a containerized reverse proxy, lower the threshold:

      echo 'net.ipv4.ip_unprivileged_port_start = 80' | sudo tee /etc/sysctl.d/99-rootless-ports.conf
      sudo sysctl --system

    If it doesn’t work

    After enabling userns-remap, all images and containers disappeared — docker images and docker ps -a are empty

    Cause
    Docker switched its data directory to /var/lib/docker/<uid>.<gid>/ (for example, /var/lib/docker/100000.100000/). The old images and containers still sit in the root of /var/lib/docker/; nothing was deleted.
    Fix
    Pull images and bring stacks up in the new namespace (adjusting ownership on host data directories for the UID offset). To revert, remove userns-remap from /etc/docker/daemon.json and restart docker.

    Container can’t write to a bind-mounted host directory: Permission denied

    Cause
    Container uid 0 maps to an unprivileged host UID (such as 100000), while the directory is owned by root:root with mode 755. For a service running as uid 33, the host owner is 100033.
    Fix
    Set ownership using the offset from /etc/subuid: sudo chown -R 100000:100000 /opt/site/data (or 100000 + container_uid).

    docker: Error response from daemon: cannot share the host's network namespace when user namespaces are enabled

    Cause
    Containers using --network host (or network_mode: host in Compose) aren’t compatible with UID remapping.
    Fix
    Opt that single container out of remapping: --userns=host in docker run or userns_mode: host in compose.yaml.

    Rootless Docker can’t publish ports 80 and 443: cannot expose privileged port 80 ... bind: permission denied

    Cause
    The daemon runs as a regular user, and Linux blocks unprivileged processes from binding ports below 1024 (net.ipv4.ip_unprivileged_port_start).
    Fix
    Lower the threshold in /etc/sysctl.d/99-rootless-ports.conf (net.ipv4.ip_unprivileged_port_start = 80) and run sudo sysctl --system.

    On Ubuntu 24.04, Rootless Docker fails with Operation not permitted when creating a user namespace

    Cause
    Ubuntu 24.04 sets kernel.apparmor_restrict_unprivileged_userns = 1. The built-in /etc/apparmor.d/rootlesskit profile permits only /usr/bin/rootlesskit; if the binary sits elsewhere (such as ~/bin/rootlesskit), AppArmor blocks it.
    Fix
    Install docker-ce-rootless-extras from apt (which places the binary at /usr/bin/rootlesskit), or add an /etc/apparmor.d/ profile with flags=(unconfined) and userns, for your path and run sudo systemctl reload apparmor.

    How to check

    For userns-remap, start a test container and compare its process inside and on the host:

    docker run -d --rm --name test-userns busybox:1.37 sleep 30
    docker exec test-userns id
    # uid=0(root) gid=0(root)
    
    PID=$(docker inspect -f '{{.State.Pid}}' test-userns)
    ps -o pid,uid,gid,user,comm -p "$PID"
    #   PID   UID   GID USER     COMMAND
    #  6670 100000 100000 100000 sleep
    
    cat /proc/"$PID"/uid_map
    #          0     100000      65536
    docker rm -f test-userns

    Inside the container it’s uid=0, on the host it’s UID 100000, and uid_map maps 0 to 100000 across 65,536 IDs.

    For Rootless Docker, verify that SecurityOptions lists rootless and both dockerd and container processes run as the regular user (container uid 0 maps to that user’s UID, while uid 1 and up map into /etc/subuid):

    docker info --format '{{.SecurityOptions}}'
    # [name=seccomp,profile=builtin name=rootless name=cgroupns]
    ps -eo user,uid,comm | grep -E 'dockerd|rootlesskit'
    # example-user 1000 rootlesskit
    # example-user 1000 dockerd