Lesson 85 · Section 7: Security
Lesson 85: Security in Docker: users, namespaces and capabilities
What actually separates a container from its host: one shared kernel, namespaces for isolation, the root user inside a container, and the Linux capabilities that decide how much that root may do. The ground work for Kubernetes security contexts. · 16 min read · 4 min video
Official competencies this lesson covers
Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.
What this lesson is about
Kubernetes does not secure containers by itself. When you write runAsUser, capabilities or privileged in a
Pod, Kubernetes only passes your wish to the container runtime, and the runtime asks the Linux kernel to enforce
it. To use those fields well, you first need a clear picture of what a container is from the host's point of view.
Docker is the easiest place to build that picture, because every setting is a single docker run flag you can try
in seconds. This lesson answers three questions:
- What separates a container from the host, and what does not?
- Which user runs the process inside a container?
- How much may that user do, and how do you change it?
How it works
One kernel, many namespaces
A virtual machine brings its own kernel. A container does not. Every container on a host makes its system calls to the same kernel as the host and as every other container. What gives a container its private world is a set of namespaces, a kernel feature that gives a process its own view of one kind of resource.
| Namespace | What the container gets of its own |
|---|---|
pid | A process list that starts again at PID 1 |
net | Network interfaces, IP addresses, routes and ports |
mnt | A root file system built from the image |
uts | A host name |
ipc | Shared memory and message queues |
user | A range of user IDs. Not used by default |
Namespaces work in one direction. The container cannot see the host's processes, but the host sees everything inside the container, because those processes are simply its own.
Parts:
- Inside the container (sleep: PID 1, root)
- Namespaces (own PIDs, network, files)
- Host kernel (one, shared by all)
- Admin on the host (ps -ef)
- The same process (sleep: PID 4312, root)
Connections:
- Inside the container to Namespaces: lives in
- Namespaces to Host kernel: syscalls
- Admin on the host to The same process: sees
- The same process to Host kernel: runs on the same kernel
You can watch this with one container:
docker run -d --name sleeper alpine:3.22 sleep 3600
docker exec sleeper ps # the view from inside
docker top sleeper # the view from the host
PID USER TIME COMMAND
1 root 0:00 sleep 3600
UID PID PPID C STIME TTY TIME CMD
root 4312 4290 0 10:02 ? 00:00:00 sleep 3600
Inside, sleep is PID 1. On the host the same process is PID 4312, and ps -ef on the host lists it next to
every other process. An administrator on the host can inspect it, trace it or kill it.
The user inside the container
Look at the USER column again: root in both views. Unless the image or the command says otherwise, the
process in a container runs as user ID 0.
Because the user namespace is not used by default, user IDs are not translated. UID 0 in the container is
UID 0 on the host. That matters as soon as the container can reach something of the host, for example through a
volume:
docker run --rm -v /etc/shadow:/shadow:ro alpine:3.22 head -n 1 /shadow
root:$y$j9T$Qx...:20050:0:99999:7:::
The container read the host's password hashes, a file that only root may read. Nothing was broken to do it: the process is root, and the file was handed to it.
There are two ways to choose another user.
For one container, with --user (short form -u). It accepts a name known inside the image or a number, and
an optional group:
docker run --rm --user 1000 alpine:3.22 id
docker run --rm --user 1000:3000 alpine:3.22 id
uid=1000 gid=0(root) groups=0(root)
uid=1000 gid=3000 groups=3000
For every container of an image, with the USER instruction in the Dockerfile:
FROM ubuntu:24.04
RUN useradd --uid 1000 --create-home app
USER 1000
CMD ["sleep", "3600"]
--user on the command line overrides USER in the image, in the same way that arguments on the command line
override CMD.
Capabilities: root in slices
If root in a container is the host's root, why can it not reboot the machine? Because Linux no longer treats root as a single switch. The power of root is split into about 40 capabilities, and for each privileged action the kernel checks one specific capability of the process, not its user name.
| Capability | Allows |
|---|---|
CHOWN | Changing the owner of any file |
KILL | Sending signals to processes of other users |
NET_BIND_SERVICE | Listening on ports below 1024 |
NET_ADMIN | Configuring interfaces, routes and firewall rules |
SYS_TIME | Setting the system clock |
SYS_ADMIN | Mounting file systems and many other admin calls |
SYS_BOOT | Rebooting the machine |
SYS_MODULE | Loading kernel modules |
Docker starts a container with an allowlist: it drops every capability except 14 that ordinary software commonly needs.
CHOWN DAC_OVERRIDE FSETID FOWNER MKNOD NET_RAW SETGID SETUID
SETFCAP SETPCAP NET_BIND_SERVICE SYS_CHROOT KILL AUDIT_WRITE
Everything else is gone, including NET_ADMIN, SYS_TIME, SYS_ADMIN, SYS_BOOT and SYS_MODULE. So root in a
default container can change file owners and kill its own processes, but cannot set the clock:
docker run --rm alpine:3.22 date -s "2030-01-01 00:00:00"
date: can't set date: Operation not permitted
Parts:
- --user 1000 (no capabilities)
- Default: root (14 capabilities)
- --privileged (every capability)
- Kernel check (has the capability?)
- Its own files only (no root powers)
- chown, kill, setuid (no mount, reboot, clock)
- The whole host (disks, devices, kernel)
Connections:
- --user 1000 to Kernel check: asks
- Default: root to Kernel check: asks
- --privileged to Kernel check: asks
- Kernel check to Its own files only: little
- Kernel check to chown, kill, setuid: some
- Kernel check to The whole host: all
A process that does not run as root starts with no effective capabilities at all. This is why --user is the
strongest single step: it removes all 14 at once.
Changing the capability set
Three flags of docker run adjust the set:
| Flag | Effect |
|---|---|
--cap-add NAME | Adds one capability to the default set |
--cap-drop NAME | Removes one capability from the default set |
--cap-drop ALL | Removes all of them. Combine it with --cap-add to build the set from zero |
--privileged | Grants everything. See below |
docker run --rm --cap-add NET_ADMIN alpine:3.22 ip link add dummy0 type dummy # works
docker run --rm --cap-drop CHOWN alpine:3.22 chown nobody /tmp # Operation not permitted
docker run -d --cap-drop ALL --cap-add NET_BIND_SERVICE --name web my-web:1.0 # only what it needs
Names can be written with or without the CAP_ prefix in Docker. Use the short form: it is the only form
Kubernetes accepts.
Privileged containers
--privileged is often described as "all capabilities". It is more than that:
- It grants every capability.
- It gives the container access to every device of the host, including the disks.
- It switches off the seccomp filter and the AppArmor or SELinux confinement.
- It makes kernel settings under
/sysand/proc/syswritable.
docker run --rm --privileged alpine:3.22 fdisk -l
Disk /dev/sda: 80 GB, 85899345920 bytes, 167772160 sectors
Device Boot StartSector EndSector Sectors Size Id Type
/dev/sda1 * 2048 167772126 167770079 80G 83 Linux
A privileged container can mount the host's disk and rewrite any file on it. Treat it as root on the host. It is meant for a small group of system programs that manage the node itself, such as network plugins, storage drivers or a container runtime running inside a container.
Under the hood · The other layers: seccomp, AppArmor, user namespaces and rootless mode
Capabilities are the layer you will configure most, but Docker stacks more behind them.
- seccomp filters system calls. Docker's default profile blocks about 44 of the more than 300 calls, among
them calls for loading kernel modules and rebooting. It applies even when a capability would allow the action.
--security-opt seccomp=unconfinedturns it off, and so does--privileged. - AppArmor and SELinux are mandatory access control systems of the host. Docker loads a default profile
(
docker-defaulton AppArmor hosts) that denies writes to sensitive paths under/procand/sys. --security-opt no-new-privileges=truestops a process from gaining privileges it did not start with, for example through a setuid program such assudo.- User namespace remapping (
userns-remapin the daemon configuration) translates UID 0 in the container to an unprivileged UID on the host, so root in the container is nobody outside it. It is off by default. - Rootless mode runs the Docker daemon itself as a normal user. In the default setup the daemon runs as root.
That last point has a consequence outside any container: whoever may talk to the Docker socket can start a
privileged container with the host's disk mounted. Membership of the docker group is therefore equal to root
on the machine.
From Docker to Kubernetes
On a Kubernetes node the kubelet asks containerd to start containers, and containerd uses the same kernel features. The defaults are the same too: a container in a Pod runs as root unless the image or the Pod says otherwise, and it gets the runtime's default list of capabilities.
Parts:
- --user 1000 (docker run)
- --cap-add (NET_ADMIN)
- --privileged (docker run)
- runAsUser: 1000 (securityContext)
- capabilities.add (["NET_ADMIN"])
- privileged: true (securityContext)
- Process user ID (who the kernel sees)
- Capability set (what root may do)
- All caps, all devices (filters switched off)
Connections:
- --user 1000 to runAsUser: 1000: becomes
- --cap-add to capabilities.add: becomes
- --privileged to privileged: true: becomes
- runAsUser: 1000 to Process user ID: sets
- capabilities.add to Capability set: sets
- privileged: true to All caps, all devices: sets
| Docker | Pod securityContext | Level |
|---|---|---|
--user 1000 | runAsUser: 1000 | Pod or container |
--user 1000:3000 | runAsUser: 1000 and runAsGroup: 3000 | Pod or container |
--cap-add NET_ADMIN | capabilities: {add: ["NET_ADMIN"]} | Container only |
--cap-drop ALL | capabilities: {drop: ["ALL"]} | Container only |
--privileged | privileged: true | Container only |
--security-opt no-new-privileges=true | allowPrivilegeEscalation: false | Container only |
apiVersion: v1
kind: Pod
metadata:
name: web
spec:
containers:
- name: web
image: nginx:1.29-alpine
securityContext:
runAsUser: 1000
capabilities:
drop: ["ALL"]
add: ["NET_BIND_SERVICE"]
The next lesson, Security Contexts, covers these fields in detail. What you take from this lesson is the meaning behind them.
Using it
Example · Compare the container's view with the host's view
docker run -d --name sleeper alpine:3.22 sleep 3600
docker exec sleeper ps # PID 1, root
docker top sleeper # host PID, root
docker inspect -f '{{.State.Pid}}' sleeper # the host PID alone
ps -o pid,user,args -p "$(docker inspect -f '{{.State.Pid}}' sleeper)"
PID USER COMMAND
4312 root sleep 3600
One process, two process IDs, one user. Clean up with docker rm -f sleeper.
Example · Run as another user and see the capabilities disappear
The kernel publishes the capability sets of every process in /proc/<pid>/status. CapEff is the effective
set, the one the kernel checks.
docker run --rm alpine:3.22 grep CapEff /proc/1/status
docker run --rm --user 1000 alpine:3.22 grep CapEff /proc/1/status
docker run --rm --privileged alpine:3.22 grep CapEff /proc/1/status
CapEff: 00000000a80425fb
CapEff: 0000000000000000
CapEff: 000001ffffffffff
The first value is Docker's default 14. The second is empty: a non-root process has no capabilities. The third
has every bit set. On a host with the libcap tools, capsh --decode=00000000a80425fb turns the number back
into names.
Example · Find the one capability a program needs
A container must add a dummy network interface. As default root it fails:
docker run --rm alpine:3.22 ip link add dummy0 type dummy
ip: RTNETLINK answers: Operation not permitted
Configuring network interfaces is NET_ADMIN. Add only that:
docker run --rm --cap-add NET_ADMIN alpine:3.22 sh -c 'ip link add dummy0 type dummy && ip -br link show dummy0'
dummy0 DOWN 7a:1c:55:0e:9b:42 <BROADCAST,NOARP>
The interface exists only in the container's own network namespace. The host's interfaces are untouched, because the capability applies inside the namespaces the process lives in.
Example · Lock a web server down
Build the set from zero for a server that listens on port 80 as root inside the container:
docker run -d --name web -p 8080:80 \
--cap-drop ALL \
--cap-add CHOWN --cap-add SETGID --cap-add SETUID --cap-add NET_BIND_SERVICE \
--security-opt no-new-privileges=true \
nginx:1.29-alpine
curl -sI http://localhost:8080 | head -n 1
HTTP/1.1 200 OK
nginx needs CHOWN, SETGID and SETUID to prepare its cache folders and switch its workers to the nginx
user, and NET_BIND_SERVICE for port 80. If you leave one out, the container exits and docker logs web names
the call that failed, which tells you the capability to add.
Tricks
Common mistakes
- Thinking a container is a security boundary like a virtual machine. Containers share the host's kernel. A kernel bug or a careless flag reaches the host directly.
- Assuming root in a container is a different, harmless root. Without user namespace remapping it is UID 0 on the host, limited only by capabilities and filters.
- Reaching for
--privilegedto fix one error. It removes every barrier at once. Add the single capability instead. - Believing
--privilegedonly adds capabilities. It also exposes all host devices and switches off seccomp and AppArmor. - Adding capabilities to a non-root user and expecting them to work. A process that starts as another user has an empty effective set. Capabilities you add are available to root in the container, not automatically to UID 1000.
- Mounting the Docker socket or host paths into a container without thought. A container with
/var/run/docker.sockcan start a privileged container, so it controls the host.
Recap
- A container is a process on the host. It shares the host's kernel; namespaces only limit what it can see.
- The host sees every container process, with a host PID and its real user ID.
- By default the process runs as root, and that is the host's root. Change it with
--userorUSERin the Dockerfile. - Root's power is split into capabilities. Docker keeps 14 and drops the rest, so default root cannot mount, reboot, set the clock or configure the network.
--cap-addand--cap-dropadjust the set.--cap-drop ALLplus a short add list is the safest pattern.--privilegedgrants all capabilities and all devices and turns off the filters. Avoid it.- In a Pod the same switches are
runAsUser,capabilitiesandprivilegedundersecurityContext.
Official docs
Quiz: check what you learned
5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.
Sign in to take the quiz and keep your score.
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.