C.W.K.
Stream
Lesson 02 of 05 · published

docker run — The Flag Vocabulary

~18 min · commands, lifecycle

Level 0Container Curious
0 XP0/36 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

One command, dozens of flags, eight that matter

docker run creates a container from an image and starts it. The vast majority of daily use comes down to a small set of flags.

The eight you'll use every day

  • -d — detached (background). Without it, the CLI attaches to the container's stdout.
  • --name foo — assign a human name. Without it, Docker generates one like amused_curie.
  • --rm — remove the container when it exits. Perfect for one-off tasks.
  • -it — interactive + TTY. For shells and REPLs.
  • -p 8080:80 — map host:container port.
  • -v name:/path or -v $(pwd):/path — volume / bind mount.
  • -e KEY=VALUE or --env-file .env — environment variables.
  • --restart unless-stopped — restart policy. The right default for long-running services.

Three modes you should remember

Foreground: docker run image — runs and attaches. Ctrl-C stops it.

One-off: docker run --rm -it image bash — interactive shell that auto-cleans.

Service: docker run -d --name app --restart unless-stopped -p 8080:80 image — long-running, named, port-mapped, auto-restart.

Code

Three patterns side by side·bash
# Pattern 1: One-off REPL
docker run --rm -it python:3.12 python
# Drops you into Python. Container vaporizes on exit.

# Pattern 2: Background service
docker run -d \
  --name web \
  --restart unless-stopped \
  -p 8080:80 \
  -e NGINX_HOST=example.com \
  nginx:1.27-alpine

# Pattern 3: Debug a base image
docker run --rm -it ubuntu:24.04 bash
# Inside: apt-get update, install tools, poke around.
# Exit and the container is gone.
Override CMD at run time·bash
# The image's default CMD is 'nginx -g daemon off;'
# Override it for a one-off check:
docker run --rm nginx:1.27-alpine nginx -v
# nginx version: nginx/1.27.x

# Or get a shell instead:
docker run --rm -it nginx:1.27-alpine sh

External links

Exercise

Run a redis:7-alpine container as a long-lived service: detached, named cache, port 6379 mapped to host, restart policy unless-stopped. Then run a one-off redis-cli from another container that connects to it and pings. Capture both commands.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.