background

Docker Compose: Managing Multi-Container Applications

Last updated June 15, 2023

DevOps

Docker Compose: Managing Multi-Container Applications

Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services, networks, and volumes, then create and start all the services with a single command.

Why Use Docker Compose?

Most real-world applications consist of multiple interconnected services. For example, a web application might need:

  • A frontend web server
  • A backend API
  • A database
  • A cache service
  • Message queues

Managing these containers individually becomes cumbersome. Docker Compose solves this by allowing you to:

  • Define all services in a single file
  • Start all services with one command
  • Create isolated environments for each project

The docker-compose.yml File

The core of Docker Compose is the docker-compose.yml file. Here's a basic example:

version: "3"

services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
    volumes:
      - ./website:/usr/share/nginx/html
    depends_on:
      - api

  api:
    build: ./api
    ports:
      - "3000:3000"
    environment:
      - DB_HOST=db
      - DB_USER=postgres
      - DB_PASSWORD=example
    depends_on:
      - db

  db:
    image: postgres:14
    volumes:
      - postgres_data:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=example
      - POSTGRES_USER=postgres
      - POSTGRES_DB=myapp

volumes:
  postgres_data:

This configuration defines three services: a web server, an API, and a database.

Essential Docker Compose Commands

CommandDescription
docker-compose upCreate and start all services
docker-compose up -dStart in detached mode (background)
docker-compose downStop and remove containers, networks
docker-compose down -vAlso remove volumes
docker-compose psList running services
docker-compose logsView output from all services
docker-compose logs SERVICEView output from specific service
docker-compose buildBuild or rebuild services
docker-compose exec SERVICE COMMANDRun a command in a service
docker-compose restartRestart all services

A Real-World Example: MERN Stack Application

Here's a practical example of a MERN (MongoDB, Express, React, Node.js) stack application:

version: "3"

services:
  frontend:
    build: ./client
    ports:
      - "3000:3000"
    volumes:
      - ./client:/app
      - /app/node_modules
    depends_on:
      - backend
    environment:
      - REACT_APP_API_URL=http://localhost:5000/api

  backend:
    build: ./server
    ports:
      - "5000:5000"
    volumes:
      - ./server:/app
      - /app/node_modules
    depends_on:
      - mongodb
    environment:
      - MONGO_URI=mongodb://mongodb:27017/myapp
      - PORT=5000

  mongodb:
    image: mongo:latest
    ports:
      - "27017:27017"
    volumes:
      - mongo_data:/data/db
    environment:
      - MONGO_INITDB_DATABASE=myapp

volumes:
  mongo_data:

Best Practices for Docker Compose

  1. Use environment variables: Store sensitive information in .env files
  2. Define restart policies: Add restart: always or restart: unless-stopped for production
  3. Name your volumes: Use named volumes instead of host paths for persistence
  4. Use networks: Create custom networks for better isolation
  5. Version control: Keep your compose files in version control
  6. Health checks: Add health checks for critical services

Scaling Services

Docker Compose allows you to run multiple instances of a service:

docker-compose up -d --scale api=3

This command starts three instances of the API service, useful for load balancing.

In the next section, we'll explore how to deploy Docker applications to production environments.

Enjoyed the read? Help us spread the word — say something nice!

Guide Parts

logo
© 2025 Guidely. All rights reserved.