Interview's MateDocker
Interview's Mate

Docker

Compose · Networking · Security · Ops

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Docker topics
Lesson 104/ 160
06. Docker Security & Hardening

User Namespaces & UID/GID Remapping

Remap container UIDs so root-in-container is an unprivileged host user.

Advanceduserns · UID Remap3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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 id for 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

Loading interactive 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.

Concrete userns setup

dockerd --userns-remap=default; cat /etc/subuid; docker run --rm --userns=host -v $(pwd)/test:/data alpine:3.21 id

Key takeaway: Prove UID Remap with 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.

Deliberate failure test

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.

Key takeaway: UID remap gives defense in depth for host ownership; rootless mode moves the whole daemon boundary and may cover more daemon risks.

How it works, step by step

Step 1 of 4
Design

1. 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.

Add host-identity isolation for containers that may otherwise run as root, while preserving known access patterns for bind mounts and devices.
Key numbers at this step
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 UID in 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

Explicit userns
Best for: Remap container UIDs so root-in-container is an unprivileged host user, with a reviewed runbook.

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.

Ambient default
Best for: Short-lived UID Remap experiments with low blast radius.

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.

Managed Isolation
Best for: Teams that centralize Isolation policy across many workloads.

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 id as proof and skipping inspect /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 id address 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?
Interview tip: Give one concrete operation-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

Loading quiz

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.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Rootless Docker Mode (Running Docker Daemon Without Root)
Next topic
Linux Capabilities (--cap-drop=ALL, --cap-add)