docker run has around eighty flags. Most operators learn five of them, ship something to production, and find out the hard way which of the remaining seventy-five mattered. This article walks the flag set you actually need before exposing a container to traffic — what each one does, why the default is what it is, and which combinations bite.
How to verify
Inspect a running container to see what was actually set at start time:
docker inspect <container> --format '{{json .HostConfig}}' | jq
docker inspect <container> --format '{{json .Config}}' | jq
docker top <container>
docker stats --no-stream <container>
HostConfig shows resource limits, restart policy, network mode, capabilities, and mount bindings. Config shows env vars, command, entrypoint, working directory, and user. If both look empty besides the image name, the container was started with docker run <image> and no other flags — that’s a starting point, not a production setup.
What’s happening
docker run is a composite of docker create followed by docker start. The flags split into three groups: image-level overrides (env, command, entrypoint, working dir, user), runtime configuration (network, mounts, ports, restart, resource limits, security options), and operator ergonomics (detach, name, tty, interactive). The image’s CMD and ENTRYPOINT are defaults; everything else is the runtime decision that turns a generic image into a specific workload.
The defaults exist for the laptop use-case: foreground, no restart, root user inside the container, no resource limits, ports unexposed, and a single anonymous volume per VOLUME directive. A production container needs almost the opposite of every one of those.
The procedure
The minimal production-ready docker run:
docker run \
--detach \
--name web-prod \
--restart unless-stopped \
--user 1000:1000 \
--read-only \
--tmpfs /tmp:rw,size=64m \
--cap-drop ALL \
--cap-add NET_BIND_SERVICE \
--security-opt no-new-privileges \
--memory 512m \
--memory-reservation 256m \
--cpus 1.0 \
--pids-limit 200 \
--network app-net \
--publish 127.0.0.1:8080:8080 \
--env-file /etc/web/.env.production \
--mount type=volume,source=web-data,target=/var/lib/app \
--health-cmd "curl -fsS http://localhost:8080/healthz || exit 1" \
--health-interval 30s \
--health-timeout 5s \
--health-retries 3 \
--log-opt max-size=10m \
--log-opt max-file=3 \
ghcr.io/example/web:1.42.0
That command tells the operator (and any future reader) everything important about the workload. Walking through it:
--detachruns in background. Without it, the container is tied to your shell — bad for production, fine for one-off runs.--nameis a stable identifier. Random names (stoic_einstein) are fine for ephemeral work; named containers can be referenced in scripts and dashboards.--restart unless-stoppedbrings the container back after host reboot or daemon restart, but respects an operatordocker stop.alwaysignores manual stops and is rarely what you want;on-failureretries N times then gives up.--user 1000:1000runs the process as a non-root UID. Image must support this (writable paths, no hardcoded/root). Pair with user namespaces for defense in depth.--read-onlyplus--tmpfsis the immutable-rootfs pattern. The container can write only to explicitly mounted paths. Most applications need/tmpwritable; some need/var/runor/var/cache.--cap-drop ALLthen--cap-addonly what’s needed.NET_BIND_SERVICElets a non-root user bind ports below 1024. For most apps that’s the only capability you need.--security-opt no-new-privilegesblocks anysetuidescalation inside the container.--memoryis a hard cap (OOM-kill above this),--memory-reservationis a soft floor for scheduling.--cpusis fractional CPU shares.--pids-limitcaps the process table — a fork bomb in a container without this limit will starve the whole host.--network app-netputs the container on a user-defined bridge with DNS-based service discovery between containers. The defaultbridgenetwork has no DNS resolution between containers.--publish 127.0.0.1:8080:8080binds only to loopback — the reverse proxy on the host (or sidecar) handles TLS termination. Binding to0.0.0.0(the default with-p 8080:8080) exposes the container to every interface, often including the public one.--env-filekeeps secrets off the command line and out ofps aux. Better still, use Docker Secrets or mount via tmpfs.--mountis the explicit form of-v. Named volumes for stateful data, bind mounts for configuration files, tmpfs for caches and secrets.--health-cmdand friends give the orchestrator (ordocker ps) a real health signal. Without this,docker psreports “Up” as long as the process is alive — even if the application is wedged.--log-optoverrides the daemon-wide log rotation for noisy containers.
Common pitfalls
- Using
--privilegedbecause something didn’t work — it grants every capability, mounts the host’s devices, and effectively cancels the security boundary. Almost always the wrong fix; identify the specific capability or--deviceflag instead. - Forgetting
-itfor an interactive shell (docker run -it ubuntu bash) and watching the container exit immediately because there’s no TTY. - Publishing ports without a network namespace (
--net=host) and finding the container’s process listening on every host IP including the public one. - Mounting
/var/run/docker.sockinto a container — that container now has root on the host. Only do this for trusted tools like Watchtower or Portainer, and understand the blast radius. - Setting
--memorywithout--memory-swapequal to it; Docker defaults to allowing double the memory in swap, which silently masks memory pressure.
We codify these flag sets as base templates and ship them via managed operations — every container we run for a client starts from one of a handful of vetted shapes, not from a fresh docker run.