User namespaces and UID/GID remapping
A user namespace lets a process have one numeric identity inside the namespace and another outside it. Docker userns-remap can map container users to subordinate host IDs while leaving the daemon rootful. Rootless mode also uses user namespaces, but its mapping and daemon identity differ.
Worked example
This is an offline lesson. Scenarios are fictional and Docker commands are unexecuted. Any optional execution requires an approved disposable VM, a reviewed preloaded image with verified digest, blocked external access and appropriate resource limits. No host-policy change, real credentials or live scan is required. Keep the exercise offline. Before any separately authorized runtime check, verify the effective daemon, builder, runner or deployment identity as applicable; use synthetic data, reviewed inputs and bounded resources. Record missing evidence as NOT RUN.
Synthetic contiguous mapping: inside_start=0 outside_start=200000 length=65536inside UID 0 -> outside UID 200000inside UID 1000 -> outside UID 201000inside UID 65535 -> outside UID 265535inside UID 65536 -> outside this mappingFor a single contiguous mapping, outside ID = outside start + (inside ID - inside start), provided the inside ID is within the range. GID mappings must be checked separately. Real systems may have multiple ranges; the example is not a host configuration. A bind-mounted file retains filesystem ownership and permissions as interpreted by the runtime, so remapping can cause a legitimate write to fail. Check the actual mapping and intended volume ownership before making a scoped correction. Changing daemon-wide remapping can affect access to existing Docker objects; stage migration and recovery instead of changing it blindly. Refer back to the storage engineering chapter for ownership reasoning. The interval is half-open: [0, 65536). UID 42 maps to 200042; UID 65535 is the final mapped ID, and 65536 is outside this example. The local helper tests this arithmetic, not a real kernel mapping or permission change.
Common mistake
Do not assume every mapping uses the same offset or fix ownership by granting all users write access.
Manual practice
For the fictional mapping above, calculate the outside UID of 42 and say whether 70000 is represented. Explain why this arithmetic does not establish a real daemon mapping.
Worksheet: lab-fixtures/docker-container-security/04-user-namespace/scenario.txt.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
User namespaces and UID/GID remapping — Knowledge check
Apply defensive container security reasoning in a fictional scenario.
For the example, what is the outside UID for inside UID 42?
What is the daemon identity in userns-remap mode?
What is the first step for a new bind-mount permission failure after remapping?