iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

docker run

docker run creates and starts a container from an image. The flags here are the daily-driver options: ports, volumes, env, restart, network, user.

Daily-driver docker run flags

EXAMPLE
# 1) Basic — interactive shell
docker run --rm -it alpine sh

# Flags:
#   --rm   : delete the container when it exits
#   -i     : keep STDIN open (interactive)
#   -t     : allocate a TTY

# 2) Detached — for services
docker run -d --name web -p 8080:80 nginx:1.27
#   -d     : background
#   --name : named so you can refer to it
#   -p     : host:container port mapping

# 3) Multiple ports
docker run -d -p 8080:80 -p 8443:443 nginx

# 4) Environment variables
docker run -d \
    --name pg \
    -e POSTGRES_USER=app \
    -e POSTGRES_PASSWORD=secret \
    -e POSTGRES_DB=myapp \
    -p 5432:5432 \
    postgres:16

# From an env file
docker run --env-file .env myapp:1.0

# 5) Volumes — named volume + bind mount + read-only config
docker run -d \
    --name app \
    -v appdata:/app/data \
    -v $(pwd)/uploads:/app/uploads \
    -v $(pwd)/config.yml:/etc/app/config.yml:ro \
    myapp:1.0

# 6) Restart policy
docker run -d --restart=unless-stopped --name web nginx
#   no | on-failure | always | unless-stopped

# 7) Resource limits
docker run -d \
    --memory=512m \
    --memory-swap=512m \
    --cpus=1.0 \
    --pids-limit=200 \
    myapp:1.0

# 8) Networks
docker network create app-net
docker run -d --name db    --network app-net postgres:16
docker run -d --name web   --network app-net -p 8080:80 myapp:1.0
# Inside web container: DB hostname is `db` (DNS resolves to db container's IP)

# 9) User — never run as root in prod
docker run -d --user 1000:1000 myapp:1.0

# 10) Healthcheck override
docker run -d \
    --health-cmd 'curl -fs http://localhost:3000/health || exit 1' \
    --health-interval=30s \
    --health-timeout=3s \
    --health-retries=3 \
    myapp:1.0

# 11) Override CMD / ENTRYPOINT
docker run --rm myapp:1.0 --version       # pass args to ENTRYPOINT
docker run --rm --entrypoint sh alpine -c 'echo hi'

# 12) Logging driver
docker run -d --log-driver=json-file --log-opt max-size=10m --log-opt max-file=3 myapp
docker run -d --log-driver=fluentd --log-opt fluentd-address=10.0.0.5:24224 myapp

# 13) Common workflow — quick run, inspect, then docker compose
docker run --rm -it -v $(pwd):/app -w /app node:20 npm install
docker run --rm -it -v $(pwd):/app -w /app node:20 npm test

# 14) Useful diagnostic flags
docker run -it --rm --entrypoint sh myapp:1.0    # poke inside a build
docker run --rm myapp:1.0 env                    # see resolved env vars
docker run --rm myapp:1.0 cat /etc/os-release    # check base image OS

# 15) When to STOP using docker run
# More than 2 long-running containers → docker compose
# Production → orchestrator (Kubernetes, ECS, Nomad, Swarm)
# CI → docker-compose or testcontainers

# 16) Docs — every flag at a glance
docker run --help

Why it matters

--restart=unless-stopped, named volumes, dedicated networks, and a non-root --user are the four flags that separate a hobby docker run from a production-ready container.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
docker run --rm -it alpine sh
docker run -d --name api my-api:1.0
Try it Yourself »

Exercise

Run a container in the background.

docker run my-api

Test yourself

Q1. "docker run" flag for detached mode is…
Q2. Set env var inline with…
Q3. For an interactive shell use…

Discussion

Loading…