Skip to main content

Docker Compose File Structure

Think of a docker-compose.yml file as the blueprint of a city. Each building (service) has its own purpose, roads (networks) connect them, and storage units (volumes) preserve their belongings. By organizing everything in one file, Docker Compose makes it easy to launch the entire city with a single command.


File Structure Foundations

1. File Format

  • Written in YAML (human‑readable configuration language).
  • Indentation is critical — spaces, not tabs.
  • Top‑level keys define the structure.

2. Key Sections of a Compose File

  • version:
    • Defines the Compose file format version.
    • Example: version: '3'.
  • services:
    • Defines containers that make up the application.
    • Each service has its own configuration (image, ports, environment, etc.).
  • networks:
    • Defines custom networks for communication between services.
    • Optional, but useful for isolation and service discovery.
  • volumes:
    • Defines persistent storage shared across services.
    • Ensures data survives container restarts.

3. Common Service Configuration Options

  • image: Which image to use (nginx, redis).
  • build: Build from a Dockerfile.
  • ports: Map host ports to container ports (8080:80).
  • environment: Set environment variables.
  • volumes: Mount host directories or named volumes.
  • depends_on: Define service dependencies.

The Hackers Notebook

  • The docker-compose.yml file is the heart of Compose.
  • Services, networks, and volumes are the three pillars.
  • Indentation and structure are critical for YAML correctness.

Hands‑On Lab

Step 1: Create a Basic Compose File

version: '3'
services:
  web:
    image: nginx
    ports:
      - "8080:80"
  db:
    image: redis

Step 2: Add Volumes and Networks

version: '3'
services:
  web:
    image: nginx
    ports:
      - "8080:80"
    networks:
      - appnet
    volumes:
      - webdata:/usr/share/nginx/html
  db:
    image: redis
    networks:
      - appnet
    volumes:
      - dbdata:/data

networks:
  appnet:

volumes:
  webdata:
  dbdata:

Step 3: Run the Stack

docker-compose up -d
docker-compose ps

Step 4: Stop and Clean Up

docker-compose down

Practice Exercise

  1. Create a docker-compose.yml file with three services:
    • frontend (nginx) exposed on port 8081.
    • backend (node.js app built from a Dockerfile).
    • database (mysql with environment variables for user/password).
  2. Define a custom network appnet.
  3. Define volumes for frontend and database.
  4. Run the stack and confirm all services are connected.

Visual Learning Model

docker-compose.yml
   ├── version
   ├── services
   │     ├── web
   │     └── db
   ├── networks
   └── volumes

The Hackers Notebook

The Docker Compose file structure organizes services, networks, and volumes in a YAML blueprint. Services define containers, networks connect them, and volumes persist data. Mastering this structure is the foundation for building complex, multi‑container applications with Compose.


Tips, Tricks, Roadmaps, Resources, Networking, Motivation, Guidance, and Cool Stuff ♥

Updated on Dec 26, 2025