User Namespaces & UID/GID Remapping
Remap container UIDs so root-in-container is an unprivileged host user.
In plain words
Add host-identity isolation for containers that may otherwise run as root, while preserving known access patterns for bind mounts and devices.
Think of it like…
The name badge says “0” inside the tenant office, but the building directory translates it to employee 231072 with only the corresponding room key.
Where you’ve seen it
In practice, dockerd --userns-remap=default; cat /etc/subuid; docker run --rm --userns=host -v $(pwd)/test:/data alpine:3.21 id is the concrete move for this job: remap container UIDs so root-in-container is an unprivileged host user. The release is not accepted until inspect /proc/1/uid_map and stat a file to compare container UID 0 with its mapped host UID confirms the result.
Words you’ll hear
- userns
- The implementation control in
dockerd --userns-remap=default; cat /etc/subuid; docker run --rm --userns=host -v $(pwd)/test:/data alpine:3.21 idfor remap container UIDs so root-in-container is an unprivileged host user. - UID Remap
- The result to observe with
inspect /proc/1/uid_map and stat a file to compare container UID 0 with its mapped host UID, rather than infer from a successful command alone. - Isolation
- The production boundary exposed when chowning every bind path to container UID 0 after mapping, or disabling userns per container just because a host mount reports permission denied.
Visual diagram
Theory & deep dive
userns control path
The concrete mechanism is dockerd --userns-remap=default; cat /etc/subuid; docker run --rm --userns=host -v $(pwd)/test:/data alpine:3.21 id. The observable result is inspect /proc/1/uid_map and stat a file to compare container UID 0 with its mapped host UID; if those disagree, inspect the runtime evidence before changing unrelated settings.
Remap container UIDs so root-in-container is an unprivileged host user. Keep userns, UID Remap and Isolation in the same deployment record so the intent and the observed state cannot drift apart.
dockerd --userns-remap=default; cat /etc/subuid; docker run --rm --userns=host -v $(pwd)/test:/data alpine:3.21 id
inspect /proc/1/uid_map and stat a file to compare container UID 0 with its mapped host UID, not with command success alone.Isolation failure boundary
A root container with a user namespace still sees UID 0 internally, so this is not the same as USER non-root. Remapped numeric owners can make a bind volume inaccessible, and some privileged/devices/network features are deliberately incompatible. The narrow failure to design for is chowning every bind path to container UID 0 after mapping, or disabling userns per container just because a host mount reports permission denied.
Accept this compromise explicitly: UID remap gives defense in depth for host ownership; rootless mode moves the whole daemon boundary and may cover more daemon risks. The controlled check-inspect /proc/1/uid_map and stat a file to compare container UID 0 with its mapped host UID-also identifies the point where a different design is safer.
Exercise this failure safely: chowning every bind path to container UID 0 after mapping, or disabling userns per container just because a host mount reports permission denied.
How it works, step by step
Step 1 of 41. State the boundary
Write down why the workload needs this: A root container with a user namespace still sees UID 0 internally, so this is not the same as USER non-root. Remapped numeric owners can make a bind volume inaccessible, and some privileged/devices/network features are deliberately incompatible.
- Intent
- userns
When to use it & trade-offs
What it is
User namespace remapping maps container UID 0 to a high unprivileged host UID range, so files and many kernel objects owned by “root in container” are not host root. Docker’s default userns-remap can use dockremap or a chosen user with /etc/subuid and /etc/subgid ranges.
Why it matters: A root container with a user namespace still sees UID 0 internally, so this is not the same as USER non-root. Remapped numeric owners can make a bind volume inaccessible, and some privileged/devices/network features are deliberately incompatible.
When to reach for it
- The fit is strongest for this job: remap container UIDs so root-in-container is an unprivileged host user. Pair it with
inspect /proc/1/uid_map and stat a file to compare container UID 0 with its mapped host UIDin the runbook. - Production userns changes should make UID Remap observable through
dockerd --userns-remap=default; cat /etc/subuid; docker run --rm --userns=host -v $(pwd)/test:/data alpine:3.21 id, rather than trusting the runtime default. - Keep a rollback example for the case where chowning every bind path to container UID 0 after mapping, or disabling userns per container just because a host mount reports permission denied.
When to avoid or be careful
- Do not accept command success while the system can still fail by chowning every bind path to container UID 0 after mapping, or disabling userns per container just because a host mount reports permission denied.
- Avoid erasing Isolation protection merely to dodge this cost: “UID remap gives defense in depth for host ownership; rootless mode moves the whole daemon boundary and may cover more daemon risks”.
- Do not replace the evidence path-
inspect /proc/1/uid_map and stat a file to compare container UID 0 with its mapped host UID-with a screenshot or a mutable tag.
dockerd --userns-remap=default; cat /etc/subuid; docker run --rm --userns=host -v $(pwd)/test:/data alpine:3.21 id: practical choices
Strengths: User namespace remapping maps container UID 0 to a high unprivileged host UID range, so files and many kernel objects owned by “root in container” are not host root. Docker’s default userns-remap can use dockremap or a chosen user with /etc/subuid and /etc/subgid ranges.
Watch out: UID remap gives defense in depth for host ownership; rootless mode moves the whole daemon boundary and may cover more daemon risks.
Strengths: It can begin with dockerd --userns-remap=default; cat /etc/subuid; docker run --rm --userns=host -v $(pwd)/test:/data alpine:3.21 id and fewer surrounding controls.
Watch out: It silently accepts chowning every bind path to container UID 0 after mapping, or disabling userns per container just because a host mount reports permission denied, which makes the result hard to reproduce after promotion.
Strengths: A root container with a user namespace still sees UID 0 internally, so this is not the same as USER non-root. Remapped numeric owners can make a bind volume inaccessible, and some privileged/devices/network features are deliberately incompatible.
Watch out: The platform adds an ownership and debugging layer; retain inspect /proc/1/uid_map and stat a file to compare container UID 0 with its mapped host UID as the direct check.
Interview signals & numbers to know
When this topic comes up
- dockerd --userns-remap=default; cat /etc/subuid; docker run --rm --userns=host -v $(pwd)/test:/data alpine:3.21 id
- inspect /proc/1/uid_map and stat a file to compare container UID 0 with its mapped host UID
- Failure signature: chowning every bind path to container UID 0 after mapping, or disabling userns per container just because a host mount reports permission denied
- Trade-off: UID remap gives defense in depth for host ownership; rootless mode moves the whole daemon boundary and may cover more daemon risks.
Benchmarks & rules of thumb
- 165536 UIDs
- common 64 KiB subordinate ID range starting near a configured value
- 2 files
- /etc/subuid and /etc/subgid define maps
- 0 host root
- intended mapping for ordinary container UID 0
Common mistakes & follow-ups
Common interview mistakes
- Treating a successful
dockerd --userns-remap=default; cat /etc/subuid; docker run --rm --userns=host -v $(pwd)/test:/data alpine:3.21 idas proof and skippinginspect /proc/1/uid_map and stat a file to compare container UID 0 with its mapped host UID. - Chowning every bind path to container UID 0 after mapping, or disabling userns per container just because a host mount reports permission denied
- Optimizing userns without accepting that uID remap gives defense in depth for host ownership; rootless mode moves the whole daemon boundary and may cover more daemon risks.
Likely interviewer follow-ups
- Why does
dockerd --userns-remap=default; cat /etc/subuid; docker run --rm --userns=host -v $(pwd)/test:/data alpine:3.21 idaddress userns here rather than through application code? - How would you distinguish healthy UID Remap from the failure “chowning every bind path to container UID 0 after mapping, or disabling userns per container just because a host mount reports permission denied” using
inspect /proc/1/uid_map and stat a file to compare container UID 0 with its mapped host UID? - When would this trade-off-“UID remap gives defense in depth for host ownership; rootless mode moves the whole daemon boundary and may cover more daemon risks”-justify a different Isolation design?
dockerd --userns-remap=default; cat /etc/subuid; docker run --rm --userns=host -v $(pwd)/test:/data alpine:3.21 id-and one proof-inspect /proc/1/uid_map and stat a file to compare container UID 0 with its mapped host UID. Then name the boundary: chowning every bind path to container UID 0 after mapping, or disabling userns per container just because a host mount reports permission denied.Knowledge check
Put it into practice
Practice related interview Q&A
Apply User Namespaces & UID/GID Remapping with generative AI interview Q&A and hands-on practice.