Skip to content

docker run — the flags that matter in production

A guided tour of the docker run flags you must understand before shipping anything: detach, restart, env, mount, network, user, and the rest of the production essentials.

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:

  1. --detach runs in background. Without it, the container is tied to your shell — bad for production, fine for one-off runs.
  2. --name is a stable identifier. Random names (stoic_einstein) are fine for ephemeral work; named containers can be referenced in scripts and dashboards.
  3. --restart unless-stopped brings the container back after host reboot or daemon restart, but respects an operator docker stop. always ignores manual stops and is rarely what you want; on-failure retries N times then gives up.
  4. --user 1000:1000 runs the process as a non-root UID. Image must support this (writable paths, no hardcoded /root). Pair with user namespaces for defense in depth.
  5. --read-only plus --tmpfs is the immutable-rootfs pattern. The container can write only to explicitly mounted paths. Most applications need /tmp writable; some need /var/run or /var/cache.
  6. --cap-drop ALL then --cap-add only what’s needed. NET_BIND_SERVICE lets a non-root user bind ports below 1024. For most apps that’s the only capability you need.
  7. --security-opt no-new-privileges blocks any setuid escalation inside the container.
  8. --memory is a hard cap (OOM-kill above this), --memory-reservation is a soft floor for scheduling. --cpus is fractional CPU shares.
  9. --pids-limit caps the process table — a fork bomb in a container without this limit will starve the whole host.
  10. --network app-net puts the container on a user-defined bridge with DNS-based service discovery between containers. The default bridge network has no DNS resolution between containers.
  11. --publish 127.0.0.1:8080:8080 binds only to loopback — the reverse proxy on the host (or sidecar) handles TLS termination. Binding to 0.0.0.0 (the default with -p 8080:8080) exposes the container to every interface, often including the public one.
  12. --env-file keeps secrets off the command line and out of ps aux. Better still, use Docker Secrets or mount via tmpfs.
  13. --mount is the explicit form of -v. Named volumes for stateful data, bind mounts for configuration files, tmpfs for caches and secrets.
  14. --health-cmd and friends give the orchestrator (or docker ps) a real health signal. Without this, docker ps reports “Up” as long as the process is alive — even if the application is wedged.
  15. --log-opt overrides the daemon-wide log rotation for noisy containers.

Common pitfalls

  • Using --privileged because 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 --device flag instead.
  • Forgetting -it for 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.sock into 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 --memory without --memory-swap equal 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.