Container Security Hardening: Non-Root Users, Read-Only Filesystems, and Minimal Images
Docker container security in layers: non-root users, read-only filesystems, dropped capabilities, seccomp, rootless Docker and minimal images.

A vulnerable image-processing library lets an attacker run commands inside a container. In the default configuration, those commands run as root, can write anywhere in the filesystem, can install tools with the package manager, can use dozens of Linux capabilities, and can start unlimited processes. If someone also mounted the Docker socket “for monitoring”, the attacker now controls the host. Every one of those doors was open by default or by convenience.
Container hardening is the practice of reducing the privileges, writable surface and tooling available inside a container so a compromise stays contained. It complements, rather than replaces, keeping images patched and scanned. The underlying isolation, namespaces, cgroups and the shared kernel, is covered in Docker vs. virtual machines; here the focus is tightening it.
1. Run as a non-root user
Containers run as root (UID 0) unless the image or the run command says otherwise. Root inside a container is not automatically root on the host, but it is the same UID, has every capability the container was granted, and is one kernel bug or misconfiguration away from the host.
FROM python:3.11-slim
RUN groupadd --system --gid 10001 app \
&& useradd --system --uid 10001 --gid app --no-create-home app
WORKDIR /app
COPY --chown=app:app . .
RUN pip install --no-cache-dir -r requirements.txt
USER 10001:10001
CMD ["gunicorn", "-b", "0.0.0.0:8000", "app:app"]
Use a numeric UID in USER. Orchestrators such as Kubernetes can only verify “runs as non-root” when the user is numeric, because a name like app could map to UID 0. Many official images already provide a user: node in Node images, nginx in the unprivileged nginx images, nonroot in distroless.
A non-root process can still listen on port 80 inside a container: since Docker 20.10, containers start with the net.ipv4.ip_unprivileged_port_start sysctl set to 0, so low ports do not require root.
2. Make the root filesystem read-only
docker run -d --name api \
--read-only \
--tmpfs /tmp:rw,noexec,nosuid,size=64m \
-v uploads:/app/uploads \
billing-api:1.6.0
--read-only mounts the container’s root filesystem read-only. The application can write only where you give it a tmpfs or a volume. An attacker cannot drop a binary into /usr/local/bin, modify the application code, or plant a cron job. It also surfaces where your application writes unexpectedly, which is useful information in itself. Common paths that need a tmpfs are /tmp, /var/run and framework cache directories.
3. Drop capabilities
Linux splits root’s power into about forty capabilities. Docker grants a default subset of around fourteen, including CHOWN, SETUID, NET_RAW and MKNOD. Most applications need none of them:
docker run --cap-drop ALL billing-api:1.6.0
# Only if the process must bind a port below 1024 without the sysctl above, for example
docker run --cap-drop ALL --cap-add NET_BIND_SERVICE my-proxy
Dropping NET_RAW alone blocks a class of network spoofing attacks from inside containers. Start with ALL dropped and add back only what fails, with a comment explaining why.
4. Block privilege escalation
docker run --security-opt no-new-privileges:true billing-api:1.6.0
This sets the kernel’s no_new_privs flag, so processes cannot gain privileges through setuid or setgid binaries, such as sudo or su, even if they exist in the image. It is a one-line defense against the most common escalation path inside a container.
5. Keep seccomp and AppArmor or SELinux on
Docker applies a default seccomp profile that blocks around 40 to 50 risky system calls, such as mount, kexec_load and keyctl. On Ubuntu and Debian hosts it also applies an AppArmor profile, and on Red Hat family hosts SELinux labels. They are invisible when they work. Do not disable them with --security-opt seccomp=unconfined to fix an error without understanding which call was blocked.
6. Limit resources
docker run --memory 512m --cpus 1.0 --pids-limit 200 billing-api:1.6.0
Limits are a security control as well as an operational one. A --pids-limit stops a fork bomb, and memory and CPU limits stop one compromised or buggy container from starving the host. Logs and resource limits covers sizing them.
7. Ship less
Every tool in the image is a tool for an attacker: curl and wget download payloads, a shell runs scripts, a package manager installs anything. Multi-stage builds keep compilers out of the runtime image, and a minimal base image such as slim or distroless removes most of the rest. Smaller images also produce shorter vulnerability reports, so real problems are not buried under noise.
Putting it together in Compose
services:
api:
image: ghcr.io/acme/billing-api:1.6.0
user: "10001:10001"
read_only: true
tmpfs:
- /tmp:size=64m
cap_drop: [ALL]
security_opt:
- no-new-privileges:true
pids_limit: 200
deploy:
resources:
limits:
cpus: "1.0"
memory: 512M
volumes:
- uploads:/app/uploads
volumes:
uploads:
The flags that undo everything
| Setting | What it gives away |
|---|---|
--privileged |
All capabilities, all devices, no seccomp or AppArmor: effectively root on the host |
-v /var/run/docker.sock:/var/run/docker.sock |
Full control of the Docker daemon, which runs as root |
-v /:/host or other sensitive host paths |
Read or write access to the host filesystem |
--pid=host, --network=host, --ipc=host |
Removes the corresponding namespace isolation |
--cap-add SYS_ADMIN |
A capability so broad it is nearly equivalent to root |
Each has rare legitimate uses, such as a monitoring agent or a CI runner. Treat them as exceptions that need a written justification, and audit for them.
Protecting the host itself
- User namespace remapping (
userns-remapindaemon.json) maps root inside containers to an unprivileged range on the host, so a container escape lands as a nobody user. - Rootless Docker runs the daemon itself as a normal user, removing the root daemon from the picture, with some networking and storage limitations.
- The CIS Docker Benchmark lists host and daemon settings to check, and the open-source
docker-bench-securityscript audits a host against it.
Frequently Asked Questions
Why should Docker containers not run as root?
Root inside a container uses the same UID as root on the host and holds every capability granted to the container. If the application is compromised, a root process has far more ways to modify the container, escalate privileges or exploit a kernel bug to escape. A non-root user limits the damage.
What does –read-only do in Docker?
It mounts the container’s root filesystem as read-only, so the process can only write to paths you explicitly provide with tmpfs mounts or volumes. Attackers cannot modify binaries, application code or configuration inside the container.
Which Linux capabilities should I drop?
Drop all of them with –cap-drop ALL and add back only what the application demonstrably needs, such as NET_BIND_SERVICE for a process that must bind a low port. Most web applications need no capabilities at all.
Is –privileged ever safe?
It removes most isolation between the container and the host, so treat it as host-level access. Use it only for tools that genuinely need it, such as some system agents, on hosts where that trade-off is accepted and documented.
Defense in layers, one flag at a time
No single setting makes a container secure, and none of them is expensive. Non-root, read-only, no capabilities, no new privileges, limits and a minimal image together leave an attacker with very little to work with. Make them the default in your base Dockerfiles and Compose templates, and require a reason to turn any of them off. The official documentation is worth keeping open: the Docker Engine security documentation and the OWASP Docker Security Cheat Sheet. Related reading on this site: Docker secrets and image scanning and supply chain security.
Originally posted on 10 August 2023 and post last modified on 10 August 2023