Skip to content

Latest commit

 

History

History
274 lines (186 loc) · 4.35 KB

File metadata and controls

274 lines (186 loc) · 4.35 KB

Docker Compose


What is Docker Compose?

Docker Compose is a tool used to define and manage multi-container Docker applications using a docker-compose.yml file.

It allows you to:

  • Define multiple services
  • Configure networking
  • Manage volumes
  • Set environment variables
  • Start and stop everything with a single command

Core File: docker-compose.yml

Basic Structure

version: "3.9"

services:
  service_name:
    image: image_name
    ports:
      - "host_port:container_port"
    environment:
      - KEY=value
    volumes:
      - volume_name:/path/in/container

volumes:
  volume_name:

Key Concepts

🔹 Services

  • Each service = one container
  • Defined under services:
  • Automatically connected through a default network

Networking

  • Docker Compose automatically creates a default network
  • Services communicate using service names
  • No manual linking required

Example:

services:
  wordpress:
    image: wordpress
    environment:
      WORDPRESS_DB_HOST: db

  db:
    image: mysql

Here db acts as the hostname for MySQL.


Volumes (Data Persistence)

Named Volume Example

services:
  db:
    image: mysql
    volumes:
      - db_data:/var/lib/mysql

volumes:
  db_data:

Why important?

  • Data remains after:

    docker compose down
  • Without volumes → data is lost when containers are removed


Environment Variables

Option 1: Directly inside compose file

environment:
  MYSQL_ROOT_PASSWORD: example

Option 2: Using .env file (Best Practice)

.env file:

MYSQL_ROOT_PASSWORD=example

Compose file:

environment:
  MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}

Advantages: - Cleaner configuration - Easier environment switching - Production-ready setup


Essential Docker Compose Commands

Start services

docker compose up

Start in detached mode

docker compose up -d

Stop services (without removing)

docker compose stop

Remove containers and networks

docker compose down

View running services

docker compose ps

View logs (all services)

docker compose logs

View logs (specific service)

docker compose logs db

Rebuild images after changes

docker compose up --build

Single vs Multi-Container Architecture

Bad Practice

One container running: - Web server - Application - Database

Good Practice

One container per service.

Example: - WordPress container - MySQL container

Advantages:

  • Separation of concerns
  • Scalability
  • Easier debugging
  • Independent updates

WordPress + MySQL Exam Scenario

Requirements:

  • Two services
  • Same network (automatic)
  • MySQL with named volume
  • WordPress connects using service name

Persistence Test:

  1. Run docker compose down
  2. Run docker compose up
  3. Data should still exist

Reason: Named volumes preserve database data.


Important Command

  • Start services docker compose up
  • Start in background docker compose up -d
  • Stop containers only docker compose stop
  • Remove containers + networks docker compose down
  • Show logs docker compose logs
  • Show running containers docker compose ps

Compose V1 vs V2

Old command:

docker-compose

New command (Compose V2):

docker compose

Modern Docker installations use V2.


Advantages of Docker Compose

  • Single command application startup
  • Automatic service networking
  • Named volumes for persistence
  • Environment variable support
  • Ideal for development and testing
  • Simplifies multi-container management

Quick Memorization Summary

  • Compose = Multi-container tool
  • Uses docker-compose.yml
  • Services auto-networked
  • Service names act as internal DNS
  • Named volumes = data persistence
  • .env file = configuration management
  • up -d = background mode
  • down removes containers and networks