Home DockerDocker Security Basics: Run Containers Safely

Docker Security Basics: Run Containers Safely

by Knowledge Base Linux
Docker Security Basics — Docker Security Basics: Run Containers Safely

Containers are often described as lightweight virtual machines, but their security model is very different: all containers share the host kernel, and by default a container process runs as root with broad capabilities. Good docker security is therefore not one setting — it is a set of layers that limit what an attacker can do if an application is compromised. This guide covers the practical basics every sysadmin should apply: non-root users, dropped capabilities, read-only filesystems, resource limits and image hygiene. If Docker is not installed yet, start with our Ubuntu 24.04 installation guide.

1. Run Containers as a Non-Root User – Docker Security Basics

Root inside a container is much closer to root on the host than most people assume, especially combined with kernel vulnerabilities or misconfigurations. Make images run as an unprivileged user:

# In the Dockerfile
RUN useradd --create-home appuser
USER appuser

For images you do not control, enforce it at runtime:

docker run -d --user 1000:1000 myimage

Also enable user namespace remapping on the daemon if your workload allows it — it maps container root to an unprivileged host UID, so even a container escape lands as nobody. Check the official documentation for the storage-driver caveats before enabling it on existing hosts.

Docker security with locked containers and a firewall

2. Drop Capabilities and Block Privilege Escalation – Docker Security Basics

The Linux kernel splits root’s powers into capabilities. Most applications need almost none of them. Drop everything, then add back only what is required:

docker run -d \
  --cap-drop ALL \
  --cap-add NET_BIND_SERVICE \
  --security-opt no-new-privileges:true \
  myimage

no-new-privileges prevents the process from gaining extra rights through setuid binaries. And never run production containers with --privileged — it effectively disables container isolation by granting full device and kernel access. If a vendor’s instructions demand privileged mode, treat that as a security review trigger, not a default.

3. Use Read-Only Filesystems

If the application cannot write to its own filesystem, many attacks become dramatically harder — dropped web shells and modified binaries have nowhere to live:

docker run -d \
  --read-only \
  --tmpfs /tmp \
  --tmpfs /run \
  -v appdata:/var/lib/app/data \
  myimage

Give the app writable volumes only for the exact paths it needs to persist (data, uploads, cache), and mount configuration as read-only bind mounts. Most well-behaved services adapt with minimal changes; the ones that fight you usually reveal exactly where they secretly write state.

4. Limit Resources

A fork bomb or memory leak in one container should never take down the host or its neighbours. Set explicit limits:

docker run -d \
  --memory 512m \
  --memory-swap 512m \
  --cpus 1.0 \
  --pids-limit 200 \
  myimage

The same settings exist in Compose as mem_limit, cpus and pids_limit under deploy.resources (honoured by Compose in recent versions). Resource limits are a security control as much as a stability one: they turn denial-of-service against a single container into a contained incident. Combine them with a hardened host firewall — our UFW guide explains the firewall side, including the caveat that Docker edits iptables directly.

5. Keep Images Clean and Scanned

Your container is only as safe as the software inside it. Pin base images to specific versions, rebuild regularly to pick up distribution patches, and remove build tools from final images. Then scan images for known vulnerabilities as part of your pipeline:

# Built-in scanning via Docker Scout (requires Docker Hub login)
docker scout cves myimage:1.0.0

# Popular open-source alternative
trivy image myimage:1.0.0

Scanning does not fix anything by itself — it tells you which base image update or dependency bump removes the critical findings. Treat high-severity results in internet-facing images as release blockers.

6. Protect Secrets and the Docker Socket

Never bake passwords or API keys into images — anyone who pulls the image can extract them from its layers. Pass secrets via environment files with tight permissions, or use Compose/Docker secrets for anything sensitive. Most importantly, guard /var/run/docker.sock: mounting it into a container is equivalent to giving that container root on the host. Only monitoring tools you fully trust should ever receive it, and the Docker API should never be exposed on a TCP port without TLS authentication.

Troubleshooting

App fails after switching to non-root. It probably writes to a root-owned path. Give it a named volume or tmpfs for that path instead of going back to root.

Permission denied with –read-only. Run once without the flag and watch which paths the app touches (docker diff helps), then add targeted writable mounts.

Container killed under limits. Exit code 137 plus dmesg OOM entries mean the memory limit is doing its job — profile the app, then raise the limit deliberately rather than removing it.

Conclusion

None of these measures is exotic: non-root users, minimal capabilities, read-only filesystems, resource limits, scanned images and protected secrets. Stacked together, they convert “container compromised” from a host-level disaster into a contained, restartable event. Work through the official Docker security documentation for the full checklist, including daemon hardening and content trust.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.