Skip to content
LF Cert Trainer

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

1/10Security in Docker
0:00 / 3:54

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.

NamespaceWhat the container gets of its own
pidA process list that starts again at PID 1
netNetwork interfaces, IP addresses, routes and ports
mntA root file system built from the image
utsA host name
ipcShared memory and message queues
userA 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.

One process, seen from two sideslives insyscallsseesruns on the same kernelInside the containersleep: PID 1, rootNamespacesown PIDs, network, filesHost kernelone, shared by allAdmin on the hostps -efThe same processsleep: PID 4312, root
One process, seen from two sides. A container is a normal process on the host. Namespaces change what the process can see, not what it is: the host lists it with a different process ID and, by default, the same root user.

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.

CapabilityAllows
CHOWNChanging the owner of any file
KILLSending signals to processes of other users
NET_BIND_SERVICEListening on ports below 1024
NET_ADMINConfiguring interfaces, routes and firewall rules
SYS_TIMESetting the system clock
SYS_ADMINMounting file systems and many other admin calls
SYS_BOOTRebooting the machine
SYS_MODULELoading 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
Three levels of power for a containerasksasksaskslittlesomeall--user 1000no capabilitiesDefault: root14 capabilities--privilegedevery capabilityKernel checkhas the capability?Its own files onlyno root powerschown, kill, setuidno mount, reboot, clockThe whole hostdisks, devices, kernel
Three levels of power for a container. For every privileged action the kernel checks the capabilities of the process, not its user name. Docker decides which capabilities the process starts with.

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:

FlagEffect
--cap-add NAMEAdds one capability to the default set
--cap-drop NAMERemoves one capability from the default set
--cap-drop ALLRemoves all of them. Combine it with --cap-add to build the set from zero
--privilegedGrants 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 /sys and /proc/sys writable.
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=unconfined turns it off, and so does --privileged.
  • AppArmor and SELinux are mandatory access control systems of the host. Docker loads a default profile (docker-default on AppArmor hosts) that denies writes to sensitive paths under /proc and /sys.
  • --security-opt no-new-privileges=true stops a process from gaining privileges it did not start with, for example through a setuid program such as sudo.
  • User namespace remapping (userns-remap in 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.

From docker run flags to a Podbecomesbecomesbecomessetssetssets--user 1000docker run--cap-addNET_ADMIN--privilegeddocker runrunAsUser: 1000securityContextcapabilities.add["NET_ADMIN"]privileged: truesecurityContextProcess user IDwho the kernel seesCapability setwhat root may doAll caps, all devicesfilters switched off
From docker run flags to a Pod. Kubernetes hands the same three decisions to the container runtime. Only the spelling changes: a flag in Docker is a securityContext field in a Pod.

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
DockerPod securityContextLevel
--user 1000runAsUser: 1000Pod or container
--user 1000:3000runAsUser: 1000 and runAsGroup: 3000Pod or container
--cap-add NET_ADMINcapabilities: {add: ["NET_ADMIN"]}Container only
--cap-drop ALLcapabilities: {drop: ["ALL"]}Container only
--privilegedprivileged: trueContainer only
--security-opt no-new-privileges=trueallowPrivilegeEscalation: falseContainer 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 --privileged to fix one error. It removes every barrier at once. Add the single capability instead.
  • Believing --privileged only 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.sock can 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 --user or USER in 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-add and --cap-drop adjust the set. --cap-drop ALL plus a short add list is the safest pattern.
  • --privileged grants all capabilities and all devices and turns off the filters. Avoid it.
  • In a Pod the same switches are runAsUser, capabilities and privileged under securityContext.

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.