0Pricing
Docker & DevOps Fundamentals · Lesson

Writing a Compose File

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

Writing a Compose File is a free Docker & DevOps Fundamentals lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Docker & DevOps Fundamentals learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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!

Frequently asked questions

Is the “Writing a Compose File” lesson free?

Yes — the full text of “Writing a Compose File” is free to read here on the web, and the Docker & DevOps Fundamentals course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Docker & DevOps Fundamentals course, upgrade to CoddyKit PRO.

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 Docker & DevOps Fundamentals 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 Docker & DevOps Fundamentals?

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

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 Docker & DevOps Fundamentals lesson?

Yes. Every Docker & DevOps Fundamentals 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 Docker & DevOps Fundamentals