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:
RUN addgroup --system app && adduser --system --ingroup app appUSER appImage 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:
FROM node:20-slim@sha256:DIGEST_VALUEPrefer 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:
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.3A 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.