Chapter 27Lesson 03~135 minutes

User Namespace Remapping, UID/GID Mapping, Non-Root Images, Filesystem Ownership, and Least Privilege: Configuration, Design Choices, and Tradeoffs

Choose numeric versus named identities, image-time versus runtime ownership, userns-remap versus rootless, and group-based sharing from explicit portability and security requirements.

Design choicesCOPY --chownGroupsuserns-remapRootless

Learning objectives

  • Choose fixed numeric IDs versus named users based on portability, image metadata, and host integration requirements.
  • Decide when ownership belongs in the image (COPY --chown) versus runtime initialization or storage provisioning.
  • Compare rootful + non-root, userns-remap, and rootless as different least-privilege layers.
  • Design shared write access around explicit groups and storage ownership rather than world-writable permissions.
Design principle. Put deterministic ownership in the image, persistent-data ownership in explicit provisioning/migration, and daemon-level namespace choices in infrastructure design.

1. Fixed numeric UID versus named user

Choice Benefits Risks / prerequisites Evidence
Named user in image Readable intent; integrates with app account files. Name resolves only inside image; numeric ID may vary across images. Image /etc/passwd + .Config.User + runtime id.
Fixed numeric UID/GID Stable numeric contract for storage and policy. Must avoid host/storage collisions; still needs documented group strategy. Dockerfile, image config, inode owner, runtime UID/GID.
Runtime --user/Compose user Easy environment-specific override. Can break image-owned paths; can bypass carefully prepared image user assumptions. Container config + writable-path test.

2. Image-time chown versus runtime ownership initialization

COPY --chown is deterministic for files that belong in the image. It should be preferred for application code/config that is immutable at runtime. Persistent volumes are different: the data outlives containers, can be restored from backups, and may have its own lifecycle. A controlled initialization step can prepare a new volume, but that step needs explicit authority and must not silently re-own arbitrary existing data.

Do not solve a persistent-data ownership problem by recursively chowning unknown content on every startup. Record a data version, expected UID/GID, and migration step.

3. userns-remap versus rootless: same technology family, different daemon boundary

Mode Daemon privilege Container UID 0 host mapping Operational consequence
No user namespace Rootful Host UID 0 Maximum feature compatibility; container root has stronger host correspondence.
userns-remap Rootful First subordinate UID of remap user Reduces container UID authority; daemon/socket remain root-equivalent.
Rootless Non-root user Host UID of rootless user Reduces daemon/runtime authority too; different storage/network/cgroup constraints.

4. Volume initialization pattern: separate provisioning from steady-state runtime

A clean pattern is: provision the volume once with the minimum authority required to create the intended directory/ownership, then run the application permanently as the non-root UID. Treat provisioning as a deployment/migration action with its own logs and rollback—not as an invisible root shell in the main container.

5. Group-based shared writes

When two processes need shared write access, a shared GID plus group-write/setgid directory can be more precise than forcing a shared UID. Docker also supports supplementary groups at runtime. Record the numeric GID and verify both container processes actually carry it. The group model still must line up with userns mappings and host/volume ownership.

# Read-only evidence pattern
id
docker inspect CONTAINER --format 'User={{json .Config.User}} GroupAdd={{json .HostConfig.GroupAdd}}' 2>/dev/null || true

6. Compose user and portable application models

Compose user: overrides the user configured by the image. Use it when the environment owns the identity contract; avoid adding it reflexively when the image already has a correct non-root user and prepared filesystem. Otherwise local Compose may work while another runtime that relies on image metadata fails differently.

7. Decision table

Scenario Recommended direction Why
Immutable app code owned by app user COPY --chown + non-root USER Ownership travels with image digest.
Persistent database volume Explicit volume provisioning/migration Data lifecycle is independent of image/container replacement.
Developer bind mount Match/translate host numeric ownership or group; document Desktop differences Host inode ownership is authoritative.
Need container root but reduce host UID authority Consider userns-remap on a planned/fresh daemon Root inside maps to unprivileged high host IDs.
Need daemon itself non-root Rootless Engine if feature constraints fit Changes the control-plane privilege boundary.
Shared write across services Dedicated shared GID with narrow directory permissions More precise than world-writable access.

8. Timestamped assumptions and portability checklist

  • Record Engine/Compose versions and daemon security options.
  • Record numeric UID/GID; do not store only usernames.
  • Record subordinate ranges if user namespaces matter.
  • Record mount type and host/volume ownership.
  • Record whether ownership preparation occurs at image build, deployment initialization, or runtime.
  • Do not assume Docker Desktop host-file semantics equal native Linux.

Knowledge check

When is numeric COPY --chown preferable to a named value?

Why separate volume provisioning from normal application startup?

Does userns-remap make the Docker daemon non-root?

What does Compose user: do?

What should drive a choice between rootless and VM isolation?

Next lesson

Next: User Namespace Remapping, UID/GID Mapping, Non-Root Images, Filesystem Ownership, and Least Privilege: Diagnostics, Failure Modes, Security, and Performance

Continue with the next lesson in the course sequence and carry forward the evidence-first Docker operating model.

Official references and version notes

  • User namespace remapping — current prerequisites, mapping behavior, daemon configuration, limitations, and migration cautions.
  • Rootless UID/GID mapping — precise difference between userns-remap and rootless host-ID translation.
  • Rootless mode — daemon-level non-root execution and subordinate-ID prerequisites.
  • Dockerfile reference — USER and COPY --chown semantics, including numeric versus name lookup.
  • Compose services reference — service user override and user namespace configuration surface.
  • Bind mounts — host-path ownership, read/write authority, and daemon-host path semantics.
  • Volumes — Docker-managed persistent storage and container mount behavior.
  • Docker Engine 29 release notes — current Engine baseline and user-namespace-related fixes/changes.
Version/platform baseline, verified 2026-09-22.

Docker Engine 29.8.1 is the current Engine release. ' Docker documents two different mappings: in userns-remap, container UID/GID 0 maps to the first subordinate ID and ID n maps to subordinate-start + n; in rootless mode, container UID/GID 0 maps to the host user and IDs ≥1 map into the subordinate range with an offset. ' Docker recommends enabling userns-remap on a new daemon because existing Docker objects become masked by the remapped storage layout, and it warns that host-mounted filesystem ownership must be arranged for the mapped IDs. ' COPY --chown accepts names or numeric IDs; names require matching /etc/passwd//etc/group inside the build rootfs, while numeric IDs do not. ' Compose user overrides the image-configured user. Every lab therefore records the actual Engine/context/security mode and numeric UID/GID evidence instead of assuming usernames imply equivalent authority.

Keep the academy open

Support free, practical DevOps education.

Every lesson is designed to remain readable in a browser, downloadable from GitHub, and usable without a paid learning platform. Contributions help expand and maintain the curriculum.

Ethereum / ERC-20
0x716c4Ab160C4B66F31a28AE2448BfF68fc3a2ef0Send only Ethereum/ERC-20 compatible assets to this address.