Docker security hardening for a public VPS
A practical hardening checklist for a self-hosted Docker host that's reachable from the internet. Each item is rated for impact vs effort so you can prioritize. None of this requires Kubernetes-class tooling.
The threat model
A typical self-hosted Docker host on a public VPS faces three classes of risk:
- Container escape — a vulnerable app inside a container escalates to the host. Mostly mitigated by capability dropping, read-only roots, and not running as root inside the container.
- Exposed services — a database or admin UI accidentally bound to
0.0.0.0. Mitigated by binding to127.0.0.1and using UFW. - Compromised image — you pulled a backdoored image from Docker Hub. Mitigated by pinning to digests, scanning, and preferring official / well-known publishers.
The host: daemon hardening
userns-remap (high impact, medium effort)
User namespace remapping makes container UID 0 (root) map to an unprivileged host UID. A container escape gets you a non-root user on the host, not root. Set in /etc/docker/daemon.json:
{
"userns-remap": "default",
"log-driver": "json-file",
"log-opts": { "max-size": "10m", "max-file": "3" },
"live-restore": true,
"no-new-privileges": true
}
Restart Docker. Caveats: some images that hard-code UIDs (especially older databases) need bind-mount permission adjustments after enabling this. Test in a dev environment first.
no-new-privileges default (low effort)
The flag above makes every container default to no-new-privileges:true — child processes can't elevate via setuid. Stops a class of post-exploit privilege escalation.
Disable inter-container communication you don't need
By default all containers on the default bridge can reach each other. If you use Compose, each project gets its own network and this isn't a problem. If you run ad-hoc docker run containers, set "icc": false in daemon.json and explicitly create networks where you want connectivity.
Per-container hardening (low effort, high impact)
Add these to every service in your Compose file unless there's a specific reason not to:
services:
app:
image: ghcr.io/example/app:1.2.3
read_only: true
tmpfs:
- /tmp
- /run
cap_drop:
- ALL
cap_add:
- NET_BIND_SERVICE # only if app binds < 1024
security_opt:
- no-new-privileges:true
user: "1000:1000" # don't run as root inside the container
read_only + tmpfs
The container's filesystem is mounted read-only. The app gets writable tmpfs for paths it actually needs (/tmp, /run, sometimes a logs dir). A compromised process can't drop a webshell on disk.
cap_drop / cap_add
Drop every Linux capability, add back only what's needed. Most apps need nothing. Webservers binding port 80/443 need NET_BIND_SERVICE. Don't add SYS_ADMIN ever — it's effectively root.
Run as a non-root user inside the container
Many official images already do this (nginx, postgres, n8n). For your own images, add USER 1000 in the Dockerfile or override with user: "1000:1000" in Compose.
Network: bind to localhost + reverse proxy
The single most important pattern. Every container that doesn't need to be public-facing should bind to 127.0.0.1:
ports:
- "127.0.0.1:5432:5432" # postgres
- "127.0.0.1:5678:5678" # n8n
- "127.0.0.1:9000:9000" # portainer
Put Nginx (or Traefik) in front of the ones that need HTTPS. UFW then only allows 22, 80, 443.
For containers that absolutely never need any external access, omit the ports: declaration entirely — they're reachable only on the Compose-internal network.
Image supply chain
Pin to specific tags
Never image: postgres or image: postgres:latest. Always image: postgres:16.4-alpine. latest changes silently between pulls.
Pin to digests for sensitive deployments
Tags are mutable; a malicious publisher (or an account compromise) can repoint v1.4.2 to a different image. Digests are immutable:
image: postgres:16.4-alpine@sha256:abc123...
Get the digest with docker inspect postgres:16.4-alpine --format='{{.RepoDigests}}' after a pull.
Scan images
Trivy is the de facto open-source scanner. Run against your image before deploying:
docker run --rm aquasec/trivy:latest image ghcr.io/example/app:1.4.2
Filter for HIGH and CRITICAL findings; address those, accept the rest with eyes open.
Prefer slim / alpine variants
Smaller base images mean fewer installed packages, which means a smaller CVE footprint. nginx:alpine has a fraction of the surface area of nginx:latest.
Secrets
- Never bake secrets into Dockerfiles. They stay in image layers and any pull recipient can extract them.
- Use
.envwithchmod 600for Compose. See production Compose setup. - For multi-host deployments or higher assurance, mount secrets from a vault (HashiCorp Vault, Bitwarden Secrets Manager).
- Rotate the encryption keys and database passwords on incident, not on schedule — but keep the rotation procedure documented and tested.
The Docker socket is root
The Docker daemon socket (/var/run/docker.sock) is the most powerful endpoint on the host. Anything with write access can spawn privileged containers that mount / and own the box.
- Don't bind-mount it into containers casually. If you must (Portainer, Watchtower), accept that those containers are now effectively root.
- If you need to expose Docker metadata to a less-trusted container, put
tecnativa/docker-socket-proxyin front and grant read-only access to specific API endpoints.
Auditing: Docker Bench
CIS published a Docker security benchmark; the Docker Bench tool checks your host against it:
docker run --rm --net host --pid host --userns host --cap-add audit_control \
-e DOCKER_CONTENT_TRUST=$DOCKER_CONTENT_TRUST \
-v /etc:/etc:ro \
-v /usr/bin/containerd:/usr/bin/containerd:ro \
-v /usr/bin/runc:/usr/bin/runc:ro \
-v /usr/lib/systemd:/usr/lib/systemd:ro \
-v /var/lib:/var/lib:ro \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
--label docker_bench_security \
docker/docker-bench-security
It produces a colored report. Treat WARN as a backlog and INFO as informational. PASS counts include a lot of "Docker enabled this by default," which is fine.
Rootless Docker (high impact, high effort)
Rootless mode runs the entire Docker daemon as an unprivileged user. A daemon compromise no longer gives an attacker root on the host. Trade-offs: networking is slower, some volume permissions are awkward, and not every image works out of the box.
Worth it for: multi-tenant servers, hosts running untrusted code (CI runners, code-sandbox setups). Probably not worth it for: a single-user homelab or a self-hosted productivity stack.
Official guide: dockerd-rootless-setuptool.sh install from the docker-ce-rootless-extras package.
Logging and detection
- Forward container logs to a host syslog (or a separate log host) — see
logging.driver: syslog. - Run fail2ban on the host for SSH and any public HTTP endpoints with auth.
- Subscribe to upstream image security mailing lists (Postgres, Nginx, Redis) so you know when to pull a patched tag.
The minimum-viable hardened Compose
services:
app:
image: ghcr.io/example/app:1.2.3
restart: unless-stopped
ports:
- "127.0.0.1:8080:8080"
read_only: true
tmpfs: ["/tmp", "/run"]
cap_drop: ["ALL"]
cap_add: ["NET_BIND_SERVICE"]
security_opt: ["no-new-privileges:true"]
user: "1000:1000"
healthcheck:
test: ["CMD", "wget", "-qO-", "http://127.0.0.1:8080/health"]
interval: 30s
deploy:
resources:
limits: { memory: 512M, cpus: "1.0" }
Stack this with the install guide's daemon settings and a localhost-bound port behind Nginx, and you're past the 80th percentile of Docker hosts on the public internet.
Hardened VPS to host this on?
Inferno VPS plans include DDoS protection up to 3.5 Tbps on every tier — one less attack vector to worry about, on top of the in-host hardening above.
What to do next
- Harden SSH — the host's main entry point.
- UFW firewall on a Docker host — the patterns that actually work with Docker's iptables manipulation.
- Install fail2ban — throttle brute-force on SSH and admin panels.
- Watchtower — automated image updates with tradeoffs.
Author: Marek Vainionpää — SRE, Helsinki. Last reviewed: 2026-05-22 against current Docker CE 27.3 documentation and CIS Docker Benchmark v1.6.