Docker Compose Complete Guide | Multi-Container Apps, Networks, Volumes & Environment Variables

Docker Compose Complete Guide | Multi-Container Apps, Networks, Volumes & Environment Variables

What this post covers

This is a complete guide to managing multi-container applications with Docker Compose. It walks through service definitions, networks, volumes, environment variables, and production-oriented settings—with practical examples.

From the field: Standardizing development on Docker Compose cut onboarding from three days to about thirty minutes for our team and eliminated “works on my machine” issues entirely.

Introduction: “Setting up the dev environment is painful”

Real-world scenarios

Scenario 1: Installing PostgreSQL and Redis is tedious

You have to install and configure each one separately. Docker Compose runs them together in one shot. Scenario 2: Everyone on the team has a different environment

Versions and settings diverge. Docker Compose keeps things consistent. Scenario 3: You run multiple containers by hand

You run docker run over and over. Docker Compose runs the stack with a single command.

1. What is Docker Compose?

Key characteristics

Docker Compose is a tool for defining and running multi-container Docker applications. Main benefits:

  • Simple configuration: a single YAML file
  • One-command startup: docker compose up
  • Automatic networking: communication between containers
  • Volume management: data persistence
  • Environment variables: .env file support

2. Installation

Run the following commands in your terminal.

# Docker Desktop (Windows, macOS)
# Includes Docker Compose
# Linux
sudo apt install docker-compose-plugin
# Verify
docker compose version

3. Basics

docker-compose.yml

Below is a detailed example using YAML. Read through the code while understanding what each part does.

version: '3.8'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
    volumes:
      - ./html:/usr/share/nginx/html
  db:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: mydb
    volumes:
      - postgres-data:/var/lib/postgresql/data
volumes:
  postgres-data:

Commands

Below is a detailed example using Bash. Read through the code while understanding what each part does.

# Start
docker compose up
# Run in the background
docker compose up -d
# Stop
docker compose down
# View logs
docker compose logs
# Logs for a specific service
docker compose logs web
# Restart
docker compose restart

4. Hands-on example: full-stack app

Below is a detailed example using YAML. Read through the code while understanding what each part does.

Sample configuration.

# docker-compose.yml
version: '3.8'
services:
  # Frontend (React)
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    environment:
      - REACT_APP_API_URL=http://localhost:8000
    depends_on:
      - backend
  # Backend (FastAPI)
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    ports:
      - "8000:8000"
    environment:
      - DATABASE_URL=postgresql://postgres:secret@db:5432/mydb
      - REDIS_URL=redis://redis:6379
    depends_on:
      - db
      - redis
    volumes:
      - ./backend:/app
  # Database (PostgreSQL)
  db:
    image: postgres:16
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: mydb
    volumes:
      - postgres-data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
  # Cache (Redis)
  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
    volumes:
      - redis-data:/data
  # Nginx (Reverse Proxy)
  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    depends_on:
      - frontend
      - backend
volumes:
  postgres-data:
  redis-data:
networks:
  default:
    name: myapp-network

5. Environment variables

.env file

Below is an example using .env. Run it yourself to see how it behaves.

# .env
POSTGRES_USER=postgres
POSTGRES_PASSWORD=secret
POSTGRES_DB=mydb
REDIS_PASSWORD=redis-secret
API_PORT=8000
FRONTEND_PORT=3000

Usage

Below is an example using YAML. Run it yourself to see how it behaves.

# docker-compose.yml
services:
  db:
    image: postgres:16
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}

6. Networking

Custom networks

Below is a detailed example using YAML. Read through the code while understanding what each part does.

services:
  frontend:
    networks:
      - frontend-network
  backend:
    networks:
      - frontend-network
      - backend-network
  db:
    networks:
      - backend-network
networks:
  frontend-network:
  backend-network:

7. Health checks

Below is an example using YAML. Run it yourself to see how it behaves.

Sample configuration.

services:
  backend:
    image: myapp/backend
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

8. Production settings

docker-compose.prod.yml

Below is a detailed example using YAML. Read through the code while understanding what each part does.

version: '3.8'
services:
  backend:
    image: myapp/backend:1.0.0
    restart: always
    environment:
      - NODE_ENV=production
    deploy:
      replicas: 3
      resources:
        limits:
          cpus: '0.5'
          memory: 512M
        reservations:
          cpus: '0.25'
          memory: 256M
  db:
    image: postgres:16
    restart: always
    volumes:
      - /data/postgres:/var/lib/postgresql/data
# Production run
docker compose -f docker-compose.yml -f docker-compose.prod.yml up -d

Connecting to job search and interviews

Experience standardizing local and staging environments with Compose fits well into onboarding and collaboration stories. Pair it with the portfolio and teamwork sections in the developer job hunting guide, and for practical assignment prep see the coding test strategy guide.

Summary and checklist

Key takeaways

  • Docker Compose: manage multi-container applications
  • Simple configuration: YAML file
  • Networking: created automatically
  • Volumes: data persistence
  • Environment variables: .env file
  • Health checks: automatic recovery

Implementation checklist

  • Write docker-compose.yml
  • Define services
  • Configure networks
  • Configure volumes
  • Manage environment variables
  • Implement health checks
  • Production settings

Further reading


Keywords in this post

Docker, Docker Compose, Container, DevOps, Microservices, Infrastructure

Frequently asked questions (FAQ)

Q. Docker Compose vs Kubernetes—which should I use?

A. Docker Compose targets a single host. Kubernetes targets clusters. Prefer Docker Compose for small setups and Kubernetes for large-scale orchestration.

Q. Is it OK to use Docker Compose in production?

A. It can work for small applications, but for large-scale production, Kubernetes is usually the better fit.

Q. What is the difference between Swarm and Compose?

A. Swarm is an orchestration platform. Compose is primarily a development workflow tool. For production orchestration, use Swarm or Kubernetes.

Q. Where is volume data stored?

A. Docker stores it in Docker-managed directories on the host. Inspect with docker volume inspect <volume_name>.