Privacy Policy
© 2026 linux101.dev

docker compose Command Cheatsheet

docker compose is used to define and run multi-container Docker applications using a YAML file (docker-compose.yaml) which stores configuration of all docker containers to be run. It replaces the legacy docker-compose command and is integrated into Docker CLI.

Quick Start

Use a docker-compose.yml file in the current directory to define services.

docker compose up -d

Start services in detached mode.

docker compose down

Stop and remove containers, networks, and default volumes.

Common Commands

  • docker compose ps - list running compose services.
  • docker compose logs -f - follow logs from services.
  • docker compose restart - restart services.
  • docker compose exec service-name sh - open shell inside a service container.
  • docker compose build - build or rebuild service images.

Compose file location & path rules

By default, docker compose resolves docker-compose.yml from the current working directory. For a different location use -f /path/to/docker-compose.yml.

Example: docker compose -f docker/docker-compose.yml up -d. -f works with all compose commands (up, down, start, ps, logs, etc.).

Path resolution inside compose file

Relative paths in compose config (e.g., build.context, volume source paths in volumes etc.) are resolved relative to the location of the compose file, not the shell’s current folder.

So if compose file is docker/docker-compose.yml and has context: ./app, the real context is docker/app.

Service Management

docker compose start

Start existing stopped services.

docker compose stop

Stop running services without removing containers.

docker compose rm -f

Remove stopped service containers.

Configuration Overview

Key fields in docker-compose.yml:

  • services: set one or more container services.
  • image: Docker image name (or use build: to build Dockerfile).
  • ports: host/container port mapping, e.g. "8080:80".
  • volumes: mount host paths or named volumes.
  • environment: set environment variables.
  • depends_on: define start order dependencies.

Example docker-compose.yml

version: '3.9'
# definition of services (logical definitions of containers + runtime settings like:
# image/build, ports, env, volumes, etc.)
services:
  web:
    image: nginx:alpine
    ports:
      # mapping host OS port 8080 to container port 80
      - "8080:80"
    volumes:
      # bind host html dir readonly (using ':ro')
      - ./html:/usr/share/nginx/html:ro 
    depends_on:
      - api

  api:
    build:
      context: ./api
      # we define path to the Dockerfile relative to the 'context' path defined above
      dockerfile: Dockerfile
    environment:
      # 'postgres' is scheme/protocol (DB dialect for client lib)
      # 'user:pass' are credentials
      # 'db' is hostname/service name (Compose DNS for db service) defined below
      # '5432' port
      # '/app' database name
      - DATABASE_URL=postgres://user:pass@db:5432/app
    ports:
      # mapping host OS port 5000 to container port 5000
      - "5000:5000"

  db:
    image: postgres:15-alpine
    environment:
      # defining environment variables
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=pass
      - POSTGRES_DB=app
    volumes:
      # mapping db-data volume to a path within container
      - db-data:/var/lib/postgresql/data

volumes:
  # creating a named volume (Docker creates it in /var/lib/docker/volumes/... on Linux)
  # named volumes are persistent by default and survive 'docker compose down' and 'docker compose stop'
  # 'docker compose down --volumes' would remove containers/networks with volumes so data is deleted
  # and recreted on next 'docker compose up'
  db-data: {}
                

Troubleshooting

docker compose config

Validate and view final composed configuration after interpolation.

docker compose logs --tail 100 --no-color service-name

View recent logs for a service.

docker compose ps

Check container state and port mapping.

Networking

Docker Compose creates a dedicated network per project by default.

Services can reach each other by service name or container hostname on that network.

External processes (outside compose network) can only access containers via published ports or explicitly attached networks.

docker compose ps

Check published ports and service connection status.

Managing single service within compose

You can control individual services without bringing up the full stack.

docker compose up -d api

Start only the api service (also starts dependencies like db by default).

docker compose up -d --no-deps api

Start only api without starting dependencies.

docker compose restart api

Restart just the api service.

docker compose stop api

Stop only the api service.