What are the common docker run flags you'd use in practice?

5 minbeginnerdocker-runcli-flagsfundamentals

Quick Answer

-d runs the container in the background (detached). -p host:container publishes a port. -v/--mount attaches a volume or bind mount. -e sets an environment variable. --name gives the container a memorable name instead of a random one. --rm automatically removes the container when it exits, useful for one-off/throwaway runs. --network attaches it to a specific network. These cover most everyday docker run invocations.

Detailed Answer

A representative real-world command

docker run -d \
  --name my-api \
  -p 8080:80 \
  -e NODE_ENV=production \
  -v api-data:/app/data \
  --network my-network \
  --restart unless-stopped \
  myapp:1.0

Flag by flag

  • -d (--detach) — runs the container in the background and returns your terminal immediately, instead of attaching to the container's stdout/stderr (the default "foreground" mode). Almost always what you want for a long-running server; foreground mode is more useful for quick, interactive, or debugging runs.
  • -p host_port:container_port — publishes a port, mapping a host port to a container port. Without this, the container's port is only reachable from other containers on the same Docker network, not from the host or the outside world.
  • -e KEY=value — sets an environment variable inside the container, just like a Dockerfile ENV instruction, but specified at run time instead of baked into the image. The standard way to inject runtime configuration.
  • -v or --mount — attaches persistent storage, like a named volume or bind mount, so data survives beyond this one container's lifetime, or so the container can access files from the host.
  • --name — gives the container a memorable name, like my-api, instead of Docker's random default like happy_euler. Makes later commands like docker logs my-api or docker stop my-api easier to work with.
  • --rm — automatically removes the container, and its writable layer, the moment it exits. Ideal for short-lived, one-off tasks — a quick script, a database migration, a debug shell — where you don't want leftover stopped containers cluttering docker ps -a.
  • --network — attaches the container to a specific, named Docker network instead of the default bridge. Needed for containers that rely on DNS-based service discovery of each other.
  • --restart — sets the container's restart policy: how Docker should behave if the container exits unexpectedly or the host reboots.

A useful one-off, throwaway invocation

docker run --rm -it ubuntu bash

-it combines -i (interactive, keeps stdin open) and -t (allocates a pseudo-TTY). Together they give you an interactive shell inside a freshly started container. --rm cleans it up automatically the moment you exit the shell, leaving nothing behind.

Why fluency with these specific flags matters

These flags answer the handful of questions every container needs answered: how does it run (foreground or background), how is it reachable (ports and networks), what does it need to know (environment variables), where does its data live (volumes), what happens if it dies (restart policy), and how is it identified for later management (name). Real production docker run invocations, or their Compose or Kubernetes equivalents, are almost always some combination of exactly these concerns.

Related Resources