Skip to content

Docker Compose Tutorial — Multi-Container Applications

DodaTech 2 min read

In this tutorial, you'll learn about Docker Compose Tutorial. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

What You'll Learn

Use Docker Compose to define and run multi-container applications — web server, database, cache, and more — with a single command.

Why It Matters

Real applications need multiple services. Docker Compose orchestrates them with a single YAML file, replacing complex docker run chains.

Real-World Use

Running a Django app with PostgreSQL and Redis, a Node.js API with MongoDB, or a full LAMP stack for local development.

What is Docker Compose?

Docker Compose lets you define all your application's services in a docker-compose.yml file and start everything with one command.

A Simple Web + Database App

# docker-compose.yml
version: "3.8"

services:
  web:
    build: .
    ports:
      - "3000:3000"
    environment:
      - DATABASE_URL=postgres://user:pass@db:5432/myapp
    depends_on:
      - db

  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_DB: myapp
      POSTGRES_USER: user
      POSTGRES_PASSWORD: pass
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:

Run It

# Start all services
docker compose up -d

# View logs
docker compose logs -f

# Stop everything
docker compose down

# Rebuild and start
docker compose up -d --build

Full-Stack Example

services:
  frontend:
    build: ./frontend
    ports:
      - "3000:3000"
    environment:
      - REACT_APP_API_URL=http://localhost:8000
    depends_on:
      - backend

  backend:
    build: ./backend
    ports:
      - "8000:8000"
    environment:
      - DATABASE_URL=postgres://user:pass@db:5432/myapp
      - REDIS_URL=redis://redis:6379
    depends_on:
      - db
      - redis

  db:
    image: postgres:16-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data
    environment:
      POSTGRES_DB: myapp
      POSTGRES_USER: user
      POSTGRES_PASSWORD: pass

  redis:
    image: redis:7-alpine
    volumes:
      - redis_data:/data

volumes:
  postgres_data:
  redis_data:

Key Docker Compose Features

Networks

Services communicate over an isolated network:

services:
  app:
    networks:
      - frontend
      - backend

  db:
    networks:
      - backend

networks:
  frontend:
  backend:

Volumes

Persist data and share files:

volumes:
  # Named volume (managed by Docker)
  data-volume:

  # Bind mount (local directory)
  - ./src:/app/src

Environment Variables

# From .env file
services:
  web:
    env_file: .env

# Or directly
  web:
    environment:
      - NODE_ENV=production
      - PORT=${PORT:-3000}

Useful Commands

# Start in detached mode
docker compose up -d

# Rebuild images
docker compose build

# View running services
docker compose ps

# Execute command in a service
docker compose exec web sh

# View logs for a specific service
docker compose logs web

# Stop and remove containers, networks, volumes
docker compose down -v

# Restart a service
docker compose restart web

# Scale a service
docker compose up -d --scale worker=3

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro