Skip to main content

Command Palette

Search for a command to run...

🐳 Docker Compose – Day 7 : Complete Detailed Notes

Managing Multi-Container Applications with Docker Compose

Published
4 min read
🐳 Docker Compose – Day 7 : Complete Detailed Notes
A

Cloud & DevOps enthusiast learning in public ☁️⚙️ Documenting my journey through systems, automation, and real-world engineering problems. Focused on fundamentals, practical learning, and continuous growth.

Docker Compose is a game-changer for managing multi-container Docker applications. Instead of manually running multiple docker run commands, Compose allows you to define, manage, and orchestrate containers using a single YAML file.

📷 Single YAML file managing multiple containers

What and Why of Docker Compose?. What is Docker Compose? | by Javeriasohail  | DevOps.dev


1️⃣ What is Docker Compose?

Docker Compose is a tool used to define and run multi-container applications with one command.

Instead of manually running containers, Compose lets you define:

  • Services (containers)

  • Networks

  • Volumes

✅ Compose is mainly used for:

  • Local development

  • Testing

  • CI environments

  • Small production setups

Installation

sudo apt install docker-compose -y

2️⃣ Why Docker Compose?

Without Compose, you need multiple commands:

docker run -d nginx
docker run -d mysql
docker network create app_net
docker volume create db_data

With Compose, all of this is one command:

docker compose up -d

3️⃣ Key Concepts in Docker Compose

  • Service – One container definition (e.g., web, db)

  • Project – Group of services; project name defaults to directory name

  • Compose File – YAML file (docker-compose.yml) defining the project declaratively


4️⃣ Docker Compose File Structure

version: "3.9"

services:
  service_name:
    image:
    build:
    ports:
    volumes:
    networks:
    environment:
    depends_on:

networks:
volumes:

5️⃣ Version Field

version: "3.9"
  • Defines the Compose file format

  • Version 3+ supports Swarm features


6️⃣ Services Section (Core Options)

OptionPurpose
imagePulls an image from registry (e.g., nginx:latest)
buildBuilds image from Dockerfile
container_nameSet custom container name
portsMap host → container ports (e.g., "8080:80")
exposeExpose port internally (not accessible from host)
volumesMount named volume or bind mount
environmentSet environment variables
env_fileLoad environment variables from file
depends_onControl startup order (does NOT wait for readiness)
restartRestart policy (always, on-failure, unless-stopped)
commandOverride default container command
entrypointOverride container entrypoint
working_dirSet working directory inside container
healthcheckDefine health check commands

7️⃣ Networks in Docker Compose

🔹 Default Network

services:
  web:
    image: nginx
  db:
    image: mysql
  • Docker automatically creates: projectname_default

  • Services communicate via service names

🔹 Custom Network

networks:
  app_net:

services:
  web:
    networks:
      - app_net

8️⃣ Volumes in Docker Compose

🔹 Named Volume

volumes:
  db_data:

services:
  db:
    volumes:
      - db_data:/var/lib/mysql
  • Persistent data stored outside container filesystem

  • Safe for production


9️⃣ Docker Compose Commands (Important)

CommandPurpose
docker compose upStart services
docker compose up -dStart services in detached mode
docker compose downStop and remove containers
docker compose down -vStop and remove containers + volumes
docker compose buildBuild images
docker compose psView running containers
docker compose logsView logs
docker compose logs -fFollow logs in real-time
docker compose exec web bashExecute command inside a container
docker compose restartRestart services
docker compose pullPull updated images
docker compose stopStop containers without removing
docker compose rmRemove stopped containers

🔟 Complete Industrial Example – Python + Flask + MySQL

docker-compose.yml

version: "3.9"

services:
  web:
    build: .
    ports:
      - "5000:5000"
    depends_on:
      - db
    environment:
      DB_HOST: db
    volumes:
      - .:/app
    networks:
      - app_net

  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: appdb
    volumes:
      - db_data:/var/lib/mysql
    networks:
      - app_net

volumes:
  db_data:

networks:
  app_net:
    driver: bridge

Dockerfile

FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]

Start the Containers

docker compose up -d