Docker Compose Basics

Define and run multi-container Docker applications

What is Docker Compose?

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.

Key Features:

  • Define multi-container applications using YAML files
  • Run applications with a single command
  • Manage the entire application lifecycle
  • Support for environment-specific configurations

Compose Specs:

The Compose specification is a standard for defining multi-container applications.

https://compose-spec.io

Docker Compose V2

Key Improvements:

  • GA at DockerCon Live 2022
  • Incorporates the docker compose command into the Docker CLI
    • You type docker compose instead of docker-compose
    • Drop-in replacement for docker-compose
    • docker compose commands map directly to the docker-compose ones
  • Installed with Docker Desktop
    • Linux: apt-get install docker-compose-plugin
  • Written in Go
    • docker-compose is written in Python

In summary, it's simply a faster version of the good old docker-compose tool that is shipped as a plugin instead of a Python app.

Docker Compose - Use Cases

Primary Use Cases:

  • Workloads that don't require a full orchestrator
  • Development and testing environments
  • Use of a service that can run Docker Compose files
    • Azure App Service
    • AWS ECS
    • Virtual machines

Common Scenarios:

  • Multi-service applications (web app + database + cache)
  • Local development environments
  • CI/CD pipeline testing
  • Demonstration and prototyping

Docker Compose Example

Sample docker-compose.yml:

# Docker Compose
version: '3.9'

services:
  webapi1:
    image: academy.azurecr.io/webapi1
    ports:
      - '8081:80'
    restart: always

  webapi2:
    image: academy.azurecr.io/webapi2
    ports:
      - '8082:80'
    restart: always

  apigateway:
    image: academy.azurecr.io/apigateway
    ports:
      - '80:80'
    restart: always

Compose Commands:

  • docker compose up → Start all services
  • docker compose down → Stop and remove containers
  • docker compose ps → List running services
  • docker compose logs → View service logs
  • docker compose build → Build service images
  • docker compose exec → Execute command in a service

Running the Application:

docker compose up -d
docker compose down