Docker Compose Commands Up Down Scale and More

Docker Compose gives you a set of commands to manage your entire application stack. Each command operates on the services defined in your compose.yaml file. This topic covers every essential command with practical examples and explains exactly what each one does under the hood.

The Command Structure

docker compose [OPTIONS] COMMAND [SERVICE...]

OPTIONS run before the command:
  -f myfile.yaml       ← use a specific Compose file
  --project-name myapp ← set a project name (default: folder name)

SERVICE at the end limits the command to one service:
  docker compose restart webapp   ← restart only webapp, not db
  docker compose logs db          ← logs only from db

Starting and Stopping the Stack

Start all services (foreground, shows logs):
docker compose up

Start in background (detached):
docker compose up -d

Start and rebuild images first:
docker compose up -d --build

Start only specific services:
docker compose up -d webapp redis

Stop all running containers (keeps containers, volumes, networks):
docker compose stop

Stop and REMOVE containers + networks (keeps volumes by default):
docker compose down

Stop, remove containers + networks + volumes:
docker compose down -v

Stop, remove everything including images:
docker compose down --rmi all -v

up vs start — What Is the Difference

docker compose up:
  → Creates containers if they don't exist
  → Starts containers
  → Recreates containers if compose.yaml changed
  → Use this most of the time

docker compose start:
  → Only starts already-created (stopped) containers
  → Does NOT create new ones
  → Use after docker compose stop

docker compose stop:
  → Stops containers gracefully (SIGTERM)
  → Containers still exist, just stopped

docker compose down:
  → Stops AND removes containers
  → Removes the network Compose created
  → Does NOT remove named volumes by default

Checking Status and Logs

See running services and their status:
docker compose ps

SERVICE   IMAGE         STATUS         PORTS
webapp    my-app:1.0    running        0.0.0.0:5000->5000/tcp
db        postgres:15   running        5432/tcp
redis     redis:7       running        6379/tcp

View logs from all services:
docker compose logs

Follow logs in real-time:
docker compose logs -f

Logs from one service only:
docker compose logs -f webapp

Last 100 lines from all services:
docker compose logs --tail 100

Running Commands Inside Services

Execute a command in a running service container:
docker compose exec webapp bash
docker compose exec db psql -U postgres

Run a one-off command in a new container (does not start with normal config):
docker compose run --rm webapp python manage.py migrate
docker compose run --rm webapp python manage.py createsuperuser

The --rm flag removes the temporary container after the command finishes.

The difference between exec and run: exec runs inside an already running container; run spins up a fresh container just for that command.

Scaling Services

Docker Compose can run multiple instances of a service. This is useful for load testing or simulating a scaled environment locally.

Run 3 instances of the webapp service:
docker compose up -d --scale webapp=3

docker compose ps
SERVICE   STATUS    PORTS
webapp    running   (no port mapped — avoid conflicts)
webapp    running
webapp    running
db        running   5432/tcp

Important: When scaling, do NOT use fixed port mappings (- "5000:5000")
because all 3 instances would fight for the same host port.
Use dynamic port mapping: - "5000"  (Docker picks a free host port)

Rebuilding and Updating

Rebuild images without starting:
docker compose build

Rebuild a specific service:
docker compose build webapp

Pull the latest versions of all images:
docker compose pull

Rebuild and restart only changed services:
docker compose up -d --build

Force recreate all containers even if nothing changed:
docker compose up -d --force-recreate

Watching Resource Usage

Real-time CPU and memory for all Compose services:
docker compose stats

SERVICE   CPU %   MEM USAGE / LIMIT   NET I/O         BLOCK I/O
webapp    3.2%    45MiB / 1GiB        2.1MB / 800KB   0B / 0B
db        1.8%    220MiB / 1GiB       1.2MB / 500KB   50MB / 10MB
redis     0.3%    8MiB / 1GiB         500KB / 200KB   0B / 0B

The Full Lifecycle in One Diagram

┌─────────────────────────────────────────────────────────────┐
│                DOCKER COMPOSE LIFECYCLE                     │
│                                                             │
│  compose.yaml                                               │
│       │                                                     │
│       ↓                                                     │
│  docker compose up -d  ──→  Containers Running              │
│       │                           │                         │
│       │                    docker compose ps                │
│       │                    docker compose logs              │
│       │                    docker compose exec              │
│       │                    docker compose stats             │
│       │                           │                         │
│  docker compose stop  ←───────────┘                         │
│       │                                                     │
│  Containers Stopped (still exist)                           │
│       │                                                     │
│  docker compose start ──→  Containers Running again         │
│       │                                                     │
│  docker compose down  ──→  Containers + Network Removed     │
│                             (Volumes kept unless -v)        │
└─────────────────────────────────────────────────────────────┘

Key Points

  • Use docker compose up -d to start everything in the background; docker compose down to tear it down.
  • docker compose logs -f follows real-time output from all services at once.
  • docker compose exec opens a shell in a running container; docker compose run --rm runs a one-off command.
  • Scaling with --scale requires dynamic port mappings to avoid port conflicts.

Leave a Comment