DevOps Bootcamp · Lesson

Writing a Compose File

Learn the YAML syntax for `docker-compose.yml` to define services, networks, and volumes.

Lesson 2 of 411 steps

Writing a Compose File is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, and your progress syncs across the web and the CoddyKit app. The DevOps Bootcamp course includes 4 lessons in total.

Compose File: The Blueprint

Welcome to writing Docker Compose files! The docker-compose.yml file is your blueprint for defining and running multi-container Docker applications.

It uses YAML syntax to configure your application's services, networks, and volumes in a single file.

Writing a Compose File — illustration 1

YAML: A Quick Refresher

Docker Compose files are written in YAML (YAML Ain't Markup Language). It's a human-friendly data serialization standard.

  • Key-Value Pairs: key: value
  • Lists: Items start with a hyphen (- item)
  • Indentation: Crucial for defining structure and hierarchy. Use spaces, not tabs!

The `version` Key

Every docker-compose.yml file starts with a version key. This specifies the Compose file format version, helping Docker Compose understand how to interpret your file.

For most modern applications, '3.8' or a similar '3.x' version is recommended.

version: '3.8'

Defining `services`

The services section is where you define the individual containers (services) that make up your application. Each service represents a component, like a web server or a database.

Under services, you list each service by a name you choose.

version: '3.8'
services:
  web:
    # Service configuration goes here
  database:
    # Another service's configuration

Service `image` and `ports`

Inside a service definition, image specifies which Docker image to use (e.g., nginx:latest). The ports key maps container ports to host ports, allowing external access.

  • "80:80" maps host port 80 to container port 80.
version: '3.8'
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"

Service `volumes`

The volumes key within a service lets you mount paths from your host machine into the container, or use named volumes for persistent storage.

This example mounts a local directory ./app to /usr/src/app inside the container.

version: '3.8'
services:
  app:
    image: my-node-app
    volumes:
      - ./app:/usr/src/app

Service `environment` Variables

Use the environment key to pass environment variables to your service's containers. This is crucial for configuration that might change between environments (e.g., database connection strings).

Variables can be listed as key-value pairs or in a list format.

version: '3.8'
services:
  backend:
    image: my-api
    environment:
      - DATABASE_URL=postgres://user:pass@host:5432/db
      - NODE_ENV=development

Top-Level `networks` and `volumes`

Beyond defining them within services, you can also declare custom named volumes and networks at the top level of your docker-compose.yml file.

This allows for more organized and reusable definitions for data persistence and inter-container communication.

version: '3.8'
services:
  # ... service definitions ...

networks:
  my_app_network:
    # custom network configuration

volumes:
  db_data:
    # named volume configuration

Putting it All Together

Here's a small example combining what we've learned. It defines a web service and a database service, using an image, ports, and a named volume.

Notice how indentation creates the structure.

version: '3.8'
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - web_content:/usr/share/nginx/html
  db:
    image: postgres:13
    environment:
      - POSTGRES_DB=mydb
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=secret

volumes:
  web_content:
  db_data:

Compose File Check

Which of the following YAML snippets correctly defines a Docker Compose service named 'api' that uses the 'myapi:1.0' image and exposes container port 3000 to host port 8080?

Recap: Compose File Essentials

Great job! You've learned the fundamentals of writing a docker-compose.yml file.

  • It starts with a version.
  • services define your application components.
  • Each service uses an image, can expose ports, use volumes for persistence, and set environment variables.
  • Top-level networks and volumes allow for advanced configuration.

Mastering this YAML structure is key to orchestrating multi-container apps!

Free to start

Learn DevOps Bootcamp with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
142
Lessons
568

Frequently Asked Questions

Is the “Writing a Compose File” lesson free?

Yes — any 3 lessons of the DevOps Bootcamp learning path, including “Writing a Compose File”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The DevOps Bootcamp course includes 4 lessons in total.

What will I learn in “Writing a Compose File”?

Learn the YAML syntax for `docker-compose.yml` to define services, networks, and volumes. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Writing a Compose File” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. What is Docker Compose?
  2. Writing a Compose File
  3. Deploying Multi-Service Apps
  4. Environment Variables and .env Files
← Back to DevOps Bootcamp