Docker & Containers (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
Why is running containers as root a major security vulnerability, and how do you secure runtime privileges?
senior LevelCode ExampleSecurity & AuthDocker & ContainersSystem Design
In Simple Words: By default, UID 0 inside a container is UID 0 on the host Linux kernel unless User Namespaces are enabled. If an attacker achieves Remote Code Execution (RCE) inside a container running as root:
- Any breakout (via kernel vulnerability, container escape, or mounted Docker socket) grants root access to the entire host server.
Hardening Rules:
- Always create and switch to a non-privileged user in the Dockerfile:
dockerfile
RUN addgroup -S appgroup && adduser -S appuser -G appgroup USER appuser - Run container with read-only root filesystem:
bash
docker run --read-only --tmpfs /tmp --user 10001:10001 my-app - Drop all kernel capabilities:
bash
docker run --cap-drop=ALL --cap-add=NET_BIND_SERVICE my-app