Tuesday, May 26, 2026

Docker Compose Command Reference

Command Description Key Flags Targets Single/Selective Services? How to Target Works with Profiles?
docker compose up Starts your application -d (background), --build (rebuild images) Yes docker compose up <service> Yes, activates profile services
docker compose down Stops and removes containers -v (delete volumes) No (always stops everything) N/A No (stops all active profiles)
docker compose stop Freezes containers without deleting None Yes docker compose stop <service> Yes
docker compose start Re-activates stopped containers None Yes docker compose start <service> Yes
docker compose restart Restarts running/stopped containers None Yes docker compose restart <service> Yes
docker compose ps Shows current container status --all (shows stopped containers) Yes docker compose ps <service> Yes
docker compose logs Views application output -f (live stream) Yes docker compose logs <service> Yes
docker compose exec Runs a command inside a container Requires service name and command Required (always targets one) docker compose exec <service> <cmd> Yes
docker compose build Compiles project images --no-cache (fresh build) Yes docker compose build <service> Yes
docker compose pull Downloads updated registry images None Yes docker compose pull <service> Yes
docker compose config Validates YAML file syntax None No (validates whole file) N/A No


A note about docker compose profiles


Profiles let you define services that only start when explicitly requested (e.g., debugging tools or frontend dev servers).

YAML
services:
  web:
    image: nginx
    # Starts by default

  db-admin:
    image: phpmyadmin
    profiles:
      - debug
    # Only starts if the "debug" profile is called

To run the default services plus the profile services, use the --profile flag:

Bash
docker compose --profile debug up

Example Configuration : 

Below is an example of a docker compose file which uses profiles and "depends_on" section to manage dependency (e.g. backend will wait till DB is healthy) and the use of "--profile"  option to mange which services to create.
YAML
services:
  database:
    image: postgres:16-alpine
    environment:
      POSTGRES_DB: app_db
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    # Starts automatically because 'backend' depends on it

  backend:
    image: node:20-alpine
    command: npm start
    depends_on:
      database:
        condition: service_healthy 
        # Backend waits until database passes its healthcheck

  frontend:
    image: nginx:alpine
    profiles:
      - UI
    # Kept hidden unless the 'UI' profile is explicitly activated

  db-admin:
    image: dpage/pgadmin4
    profiles:
      - debug
    # Kept hidden unless the 'debug' profile is explicitly activated

Quick Commands for This File

Start Core Backend Only: Runs database first, then backend.
Bash
docker compose up -d backend
Start Full Stack (with UI): Activates the frontend service.
Bash
docker compose --profile UI up -d
Run Debug Tools Only: Starts just the administration panel.
Bash
docker compose --profile debug up -d db-admin

No comments:

Post a Comment

GPG, PGP and SSH

All these terms relate to asymmetric (public-key) cryptography, a system where you use two mathematically linked keys: a public...