I run several self-hosted services in Docker, a vector database, my memory layer, and a few dashboards, and the question that kept coming back was how much container flags actually do. The direct answer: the three Docker security flags I rely on are a read-only root filesystem where possible, no-new-privileges, and dropped capabilities. None of them is a wall, but together they shrink the surface a container exposes, and they cost almost nothing to set. This post is what I actually configure and why.
Read-only root filesystem
The first flag is --read-only. Mounting the container's root filesystem read-only means a compromised process cannot write to system paths it has no business touching. Many of my services are stateless enough that this works out of the box. The ones that need to write scratch data get a writable volume mounted at a single explicit path, usually /tmp or a dedicated data directory.
The honest caveat: some images just will not run read-only without work. When one of my services refuses, I trace the write and give it the narrowest writable mount I can. The goal is not perfection; it is making the default filesystem unwritable so an attacker who gets code execution has to work harder to persist.
no-new-privileges
The --security-opt no-new-privileges flag tells the kernel that the process cannot gain additional privileges later in its lifetime. In practical terms it blocks the escalation paths that rely on setuid binaries and similar mechanisms. For a container that should run as an unprivileged user anyway, no-new-privileges removes a whole class of escalation without breaking anything.
I treat it as a default, not an exception. There is no reason a web-facing service of mine needs to escalate privileges mid-run, so I add it everywhere and only think about the rare case that genuinely needs it.
Dropped capabilities
Capabilities are the granular permissions the kernel hands out, and containers start with a surprisingly broad set by default. The pattern I follow is to drop rather than grant: I remove the capabilities a service does not need instead of listing the ones it should have. The useful ones are rare in my workloads, and trimming the defaults leaves a small, boring surface.
This mirrors the general principle I apply to systemd units: each service gets the minimal set of privileges its function needs. The same instinct carries over to containers. If a service needs no special capability, the container needs almost none.
Image discipline matters more than flags
The flags only help if the image underneath is sound. Three habits I keep: pin images to a specific tag or digest so updates are deliberate rather than accidental, scan images for known vulnerabilities as part of my routine, and avoid running as root inside the container.
Running as root in the container undoes much of the flag work, because a root process has far more to work with if it is compromised. I build images with an unprivileged user and run services as that user. Combined with read-only and no-new-privileges, a compromise in a container has a small box to move around in.
Containers are a boundary, not a wall
The most important framing shift for me was accepting that a container is a boundary, not a wall. The host kernel and the container configuration both matter, and neither replaces the other. A misconfigured container on a patched host is different from a misconfigured container on an unpatched one, so I keep the base image and the kernel current as well.
For me this folds into the same workflow as the broader self-hosting setup I run for my agents. The vector database and memory pieces are the same containers I care about here. If you are running agents you are probably carrying similar state, so hardening these defaults is cheap insurance.
Do you reach for the read-only flag on every container, or do you only enable it for the services that tolerate it?
Originally published at agentjournal.dev.
Top comments (0)