Supabase Blog

Beginner Guide for Docker Compose

0 views

Beginner Guide for Docker Compose

Docker Compose lets you define and run multi-container applications in a single file and with one command.

Why Docker Compose?

  • One file: Describe all services, networks, and volumes in docker-compose.yml.
  • One command: docker compose up starts (and builds) everything.
  • Reproducible: Same file works on any machine with Docker and Compose.

Basic example

services:
  web:
    build: .
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
    depends_on:
      - db

  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_USER: app
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: app
    volumes:
      - db_data:/var/lib/postgresql/data

volumes:
  db_data:

Common commands

CommandDescription
docker compose up -dStart all services in the background
docker compose downStop and remove containers (add -v to remove volumes)
docker compose psList running services
docker compose logs -fFollow logs from all services
docker compose buildBuild images defined with build:

Key concepts

  • services: Each container (e.g. web, db) is a service.
  • build: Use build: . to build from a Dockerfile in the current directory.
  • image: Use image: name:tag to use a pre-built image.
  • ports: Map host port to container port ("HOST:CONTAINER).
  • environment: Set environment variables for the container.
  • volumes: Persist data; named volumes (e.g. db_data) are managed by Docker.
  • depends_on: Start order; wait for listed services to be started (not “healthy” unless you use condition).

Tips for beginners

  1. Start with docker compose up (no -d) to see logs and Ctrl+C to stop.
  2. Use docker compose config to validate your file.
  3. Use depends_on with condition: service_healthy when a service must wait for another to be ready.
  4. Put .env next to docker-compose.yml and reference variables with ${VAR}.

Once you’re comfortable, try adding a reverse proxy (e.g. Nginx) or multiple app services in the same Compose file.