Process isolation and Linux namespaces
Containers share the host kernel rather than running a separate guest kernel. Linux namespaces provide the isolation: each container gets its own set of namespaces (PID, mount, network, IPC, UTS, user, etc.), so processes inside a container see only that container’s view of system resources. Example workflow to observe PID namespace isolation:- Start an Ubuntu container that sleeps for an hour (detached so we can inspect it from the host):
- List processes from inside the container:
- On the Docker host, the same container process appears as a regular host process (with a different PID because of the separate PID namespace). For example:
Users and UIDs inside containers
A container image contains its own view of users (files like/etc/passwd and /etc/group), but by default Docker runs container processes as UID 0 (root) inside the container unless you specify otherwise.
Ways to run container processes as a non-root UID:
- Provide a UID at runtime with
--user:
/etc/passwd, tools inside the container will display the numeric UID instead of a username.
- Bake the user into the image with a
Dockerfileusing theUSERinstruction:
If your application depends on specific usernames, ensure the image includes the appropriate
/etc/passwd entries. Otherwise, the system will display numeric UIDs instead of names.Is container root the same as host root?
A process running as UID 0 inside a container is root from the container’s perspective, but Docker—and the kernel—limit what that root can do on the host. Docker uses several kernel-level controls to reduce the blast radius of a compromised container:- Linux capabilities: split the all-powerful root privileges into fine-grained permissions (for example,
CAP_NET_BIND_SERVICE,CAP_SYS_TIME,CAP_SYS_ADMIN). - Seccomp: syscall filtering to block dangerous system calls by default.
- LSMs (Linux Security Modules): AppArmor or SELinux profiles can further restrict container behavior where available.
- Add a capability:
- Drop a capability:
- Grant nearly unrestricted host access (strongly discouraged) with:
For a full reference on Linux capabilities, see man 7 capabilities.
Avoid running containers with
--privileged unless absolutely necessary. Instead, grant only the specific capabilities your workload requires using --cap-add and --cap-drop.Summary
- Containers share the host kernel; isolation relies on Linux namespaces (PID, network, mount, etc.).
- By default, container processes run as UID 0 inside the container. Use
--userorUSERin aDockerfileto run as a non-root UID. - Docker reduces container privileges using Linux capabilities, seccomp filters, and LSMs (AppArmor/SELinux). Prefer granting minimal capabilities over using
--privileged.
- Docker documentation — https://docs.docker.com/
- Kubernetes documentation — https://kubernetes.io/docs/
- Linux capabilities — https://man7.org/linux/man-pages/man7/capabilities.7.html
- Seccomp overview — https://www.kernel.org/doc/html/latest/userspace-api/seccomp_filter.html
