Writing a Docker Compose File

This lesson guides you through the process of writing a Docker Compose file, enabling you to define and deploy multi-container applications with ease.

By Kevin McAleer,    3 Minutes

Crafting Your Docker Compose Configuration

Docker Compose files are YAML files that describe a multi-container application. Writing an effective Docker Compose file is a foundational skill for deploying applications in both development environments and production Docker Swarms. This lesson will walk you through creating a basic Docker Compose file to define a simple web application stack.

Understanding Docker Compose File Structure

A Docker Compose file is structured into several sections, including services, networks, and volumes, each defining different aspects of your application:

  • Services: Define the containers you want to run, their Docker images, and configuration options like ports, environment variables, and dependencies.
  • Networks: Specify the networks your containers should use to communicate with each other.
  • Volumes: Define persistent storage for your containers, ensuring data is maintained across container restarts.

Basic Docker Compose File Example

Here’s a simple example of a Docker Compose file that defines a web application and a database service:

version: '3.8'
    image: nginx:latest
      - "80:80"
      - db
      - webnet

    image: postgres:latest
      POSTGRES_PASSWORD: example
      - db-data:/var/lib/postgresql/data
      - webnet



Key Components Explained

  • version: Specifies the Docker Compose file version. It’s recommended to use 3.8 or above for Docker Swarm compatibility.
  • services: This section defines two services, web (an Nginx web server) and db (a PostgreSQL database).
    • image: The Docker image to use for the container.
    • ports: Maps ports from the container to the host, formatted as <host>:<container>.
    • depends_on: Specifies that the web service depends on the db service, ensuring db starts before web.
    • environment: Sets environment variables in the container. Here, it’s used to set the PostgreSQL password.
    • volumes: Maps persistent storage to the container. For db, it ensures data persists across container restarts.
  • networks: Defines a network named webnet for inter-service communication.
  • volumes: Declares a named volume db-data for the database storage, ensuring data persists.

Tips for Writing Docker Compose Files

  • Use environment variables: For sensitive information like passwords, consider using environment variables instead of hardcoding them in the file.
  • Leverage volumes for persistence: Define volumes for data that should persist between container restarts or updates, such as database files.
  • Networks for communication: Define networks to facilitate communication between services, especially when deploying to Docker Swarm.


Writing a Docker Compose file is an essential step in deploying containerized applications. By understanding and utilizing the key components of a Docker Compose file, you can effectively define, deploy, and manage complex applications with multiple interdependent services. This foundational knowledge will be invaluable as you progress to deploying and scaling applications across a Docker Swarm cluster.

< Previous Next >