Docker Docs

Get Started with Docker Compose

Simplify multi-container setups for development, testing, and deployment.

Start Now

1. Install Docker Compose

Linux

Install using the official package manager:

sudo apt update
sudo apt install docker.compose

macOS (Intel)

Install via Docker Desktop on macOS:

brew install docker-compose

Ensure Docker Desktop is running.

Windows (WSL2)

Install with WSL2 and Docker Desktop:

wsl -d docker-desktop
docker-compose -v

2. Your First Project

Create a docker-compose.yml file:

version: '3.8'
services:
  web:
    image: nginx
    ports:
      - 80:80

Start the service:

docker-compose up -d

This setup starts an Nginx web server on port 80.

Access at: localhost:80

3. Multi-Service Projects

version: '3.8'
services:
  web:
    image: myapp/web
    ports:
      - '80:80'
    depends_on:
      - db

  db:
    image: postgres
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      POSTGRES_DB: mydb
The example above sets up two services: a web application and a PostgreSQL database.

When the docker-compose up command runs, Docker Compose does the following:

  1. Starts the postgres database service
  2. Waits for the database to be ready
  3. Starts the web service
  4. Maps port 80 from the container to 80 on your machine
  5. Runs both in the background

4. Key Commands

Start Services

docker-compose up -d

Start containers in detached mode.

Stop Services

docker-compose down

Stop and remove containers created by up.

Build and Run

docker-compose up --build

Build and start services from source.

Check Status

docker-compose ps

Display status of containers.

5. Advanced Concepts

Environment Variables

Use .env files to manage configuration values across multiple instances.

DB_NAME=mydb
DB_USER=postgres
DB_PASSWORD=securepass

Custom Networks

Connect services that should communicate while remaining isolated.

services:
  web:
    networks:
      - backend
  db:
    networks:
      - backend

networks:
  backend:

6. Frequently Asked Questions

What about ports?

Docker Compose maps container ports to your local machine using - HOST:CONTAINER in the ports section of the Compose config file.

How do I build custom images?

Use the build: directive in your file and provide a Dockerfile folder.

services:
  app:
    build: ./app
    ports:
      - "3000:3000"

How to share data?

Use Docker volumes to persist data across service restarts.

volumes:
  - app_data:/myapp/data

volumes:
  app_data:

Logs?

Use docker-compose logs -f to read real-time log output from services.

docker-compose logs -f web

Ready to Build?

Create powerful, scalable, and production-ready applications using Docker Compose today.

Download Docker Compose