Skip to content

Docker hardening: best practices for safer containers

Containers inherit the risks of their image and runtime configuration. See how to reduce surface and privilege.

Reading time
9 min
Published

Containers promise isolation, but that isolation depends on how the image was built and how the container runs. A bloated image full of packages, a process running as root inside the container, or an exposed Docker socket turn the container from a barrier into a springboard for the attacker. Docker hardening boils down to two things: reducing what is inside the image and reducing privilege at runtime.

The decisions that carry the most weight are few: build minimal images, run without root, handle secrets the right way, limit capabilities, and keep base images updated with continuous scanning. These are defensive practices, and their job is to reduce the impact if the application inside the container is compromised.

Minimal image and a non-root user

The smaller the image, the smaller the surface. Prefer minimal base images and install only what the application needs to run. Every extra tool inside the container is a potential vulnerability, and also one more resource in the hands of an attacker who manages to run commands there (a compiler left behind in the image, for example).

Do not run as root. Create an unprivileged user and set it in the Dockerfile, so the main process has no administrative powers inside the container. That greatly limits what an attacker can do after compromising the application. The snippet looks like this:

Setting an unprivileged user in the Dockerfile
RUN addgroup --system app && adduser --system --ingroup app appUSER app

Image supply chain

The image you run carries everything that came from the base and the dependencies, so provenance matters as much as content. Pin the base image by digest rather than by tag alone. A tag like latest can point to different content over time, while the digest makes sure the build uses exactly the image you reviewed. That makes builds reproducible, and a modified base cannot slip in without anyone noticing. The reference looks like this:

Pinning the base image by digest in the Dockerfile
FROM node:20-slim@sha256:DIGEST_VALUE

Prefer minimal bases from a trusted source and reduce the number of dependencies that go into the image. It is also worth generating an inventory of what the image contains, the so-called SBOM. When a new flaw is disclosed, that inventory tells you quickly whether the vulnerable component is in there. Where your maturity allows it, sign images at build time and verify the signature before promoting to production. Then only the images produced by your pipeline reach the environment.

Secrets, capabilities, and network

Never put secrets inside the image or in variables baked into the Dockerfile. They get stored in the layers, and anyone who has the image can extract them. Use the secrets mechanisms of your orchestrator or inject them at runtime. And at runtime, drop unnecessary capabilities and avoid privileged mode, which practically cancels the isolation.

  • Run as an unprivileged user, never as root
  • Drop unused capabilities and avoid privileged
  • Make the file system read-only when possible
  • Limit resources (CPU and memory) to contain abuse
  • Inject secrets at runtime, outside the image

Runtime isolation: seccomp, privileges, and read-only

After the image comes execution: the options used when running the container define how much it can do if the application is compromised. Drop all capabilities and add back only the few the process really uses. Turn on the flag that blocks privilege gain through setuid binaries, and keep the default seccomp profile, which already restricts dangerous system calls. Finally, run with a read-only file system and mount a writable space only where the application needs to write. Putting it all together, the command looks like this:

Run flags that reduce privilege at runtime
docker run --rm \  --user 10001:10001 \  --read-only --tmpfs /tmp \  --cap-drop ALL \  --security-opt no-new-privileges \  --pids-limit 200 \  my-image:1.2.3

A common mistake is turning off the seccomp profile to quickly fix a permission error. The container then gains access to system calls it does not need, and a compromise reaches further. When a legitimate call is blocked, the way out is a profile tailored to the service, not running with no profile at all. Segment the network too: containers that do not need to talk to each other should not share the same network.

Image scanning and the Docker socket

Scan images for vulnerabilities, preferably in the pipeline, and update base images regularly. An image that was safe six months ago may have accumulated known CVEs since then. Without updates and scanning, you end up shipping vulnerabilities together with the application.

Treat the Docker socket as a credential with very high privilege. Mounting it inside a container is the same as giving root access to the host, because whoever controls the socket controls the daemon. Avoid that mount and, when it is unavoidable, isolate it and restrict access as much as possible.

Practical checklist

  • Use a minimal base image and install only what is necessary
  • Set an unprivileged user with USER in the Dockerfile
  • Drop unused capabilities and avoid privileged mode
  • Make the filesystem read-only when possible
  • Inject secrets at runtime, never inside the image
  • Limit container CPU and memory
  • Scan images for vulnerabilities in the pipeline
  • Update base images regularly
  • Avoid mounting the Docker socket inside containers
  • Pin base versions by digest for reproducible builds
  • Apply --security-opt no-new-privileges to containers
  • Keep the default seccomp profile active and never use --privileged
  • Run with a read-only file system and tmpfs for writable directories
  • Generate an SBOM and verify image provenance and signatures
  • Use multi-stage builds to keep build tools out of the final image

Best practices

  • Cut the image down to the minimum to shrink the surface exposed to attacks
  • Treat the Docker socket as root access to the host
  • Build image scanning into the build pipeline
  • Use multi-stage builds to keep build tools out of the final image
  • Standardize a secure base Dockerfile for teams to reuse
  • Rebuild images periodically to pick up fixes from the base
  • Pin the base image by digest and verify the signature before promoting
  • Apply runtime isolation (seccomp, no-new-privileges, read-only) by default

Common mistakes

Process running as root in the container
Whoever compromises the application starts with elevated privilege, and escaping becomes easier.
Secrets baked into the image
Credentials stay in the layers and can be extracted by anyone who obtains the image.
Mounting the Docker socket in the container
It is the same as granting root access to the entire host.
Base images that are never updated
The application gets packaged together with known vulnerabilities.
Privileged mode for convenience
It cancels the isolation and opens the way to compromising the host.
Turning off the seccomp profile to avoid a permission error
It allows system calls the container does not need and widens the options for escape.

Authorship and review

Cesar Gargiulo

CEO and founder of GUARDIASEC. Technical and editorial lead for the content published in this library.

Published on . Found a technical error? Write to [email protected] and we will correct it and keep a record of the change.

Frequently asked questions

Aren't containers already isolated by default?
Containers do offer isolation, but it is weaker than that of a virtual machine and depends on configuration. Running as root, using privileged mode, or mounting the Docker socket weakens or cancels that isolation. Hardening exists precisely to keep the barrier standing and to limit the impact of a compromise.
Why can't I keep secrets in the Dockerfile?
Because the Dockerfile produces layers, and every value baked in is stored in them. Anyone who obtains the image can inspect the history and extract the secrets, even if they do not appear in the running container. The right way is to inject secrets at runtime through the mechanisms of the orchestrator.
What makes the Docker socket risky?
The Docker socket controls the Docker daemon, which runs with root privilege on the host. Anyone with access to the socket can create privileged containers, mount the host file system and, in practice, take over the machine. That is why mounting it inside a container amounts to handing over the host.
Does image scanning replace hardening?
No. Scanning finds known vulnerabilities in the image packages, which is important, but it does not fix runtime configuration, excessive privilege, or poorly managed secrets. Scanning and hardening complement each other: one takes care of the image content, and the other of how the image is built and run.
What is seccomp, and does Docker already apply a profile?
Seccomp filters which system calls a process can make to the kernel. Docker already applies a default profile, which blocks calls that are dangerous and unnecessary for most applications without you having to configure anything. The risk appears when someone disables that profile to work around an error, and the surface grows. If a legitimate call is blocked, the right fix is a custom profile, not running without one.
Should I reference images by tag or by digest?
By digest, in production builds. A tag like latest, or even a version tag, can point to different content over time, so the same Dockerfile can produce different images at different moments. The digest pins exactly the image you reviewed, which makes the build reproducible and keeps a modified base from getting in without review. Tags are still useful for human reading, but the digest is what ties down provenance.
What does no-new-privileges do, and why turn it on?
The no-new-privileges flag prevents a process inside the container from gaining additional privileges during execution, for example through setuid binaries. With it on, even if the application is compromised, the attacker cannot use that mechanism to raise privilege inside the container. The setting costs little, closes a known escalation path, and makes sense as a default.

When to bring in specialist help

Defining a secure standard for images and runtime and building it into the pipeline is part of the GUARDIASEC hardening service. The assessment covers the images, the runtime configuration, and the container surface, and the recommendations come prioritized by risk.