Skip to content

Docker Compose File Syntax Guide

Docker Compose is a tool that allows you to define and run multi-container Docker applications. The Compose file is used to specify the configuration of the application's services, networks, and volumes, and it can be used to launch and manage the application. In this guide, we will explore the syntax of the Docker Compose file.

Version

The first line in the Docker Compose file should be the version of the Compose file format to use. The version specifies the version of the Docker Compose file format to use. For example, version: "3.9" specifies that we are using version 3.9 of the Compose file format.

Services

The services section defines the containers that make up the application. Each service is defined by a name, an image, and optionally other properties such as ports, environment variables, and volumes. For example:

services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"

This defines a service named web that uses the nginx:latest image and maps port 8080 on the host to port 80 in the container.

Networks

The networks section defines the networks used by the services. Networks can be used to enable communication between containers. For example:

networks:
  app_net:
    driver: bridge

This defines a network named app_net that uses the bridge driver.

Volumes

The volumes section defines the volumes used by the services. Volumes can be used to persist data between containers or to share data between containers. For example:

volumes:
  data:
    driver: local

This defines a volume named data that uses the local driver.

Environment Variables

Environment variables can be specified for each service. These variables are passed to the container at runtime. For example:

services:
  web:
    image: nginx:latest
    environment:
      - ENV_VAR1=value1
      - ENV_VAR2=value2

This defines a service named web that uses the nginx:latest image and sets the ENV_VAR1 and ENV_VAR2 environment variables.

Dockerfile

The Dockerfile section allows you to specify a custom Dockerfile for a service. This allows you to define the image used by a service in more detail. For example:

services:
  web:
    build:
      context: .
      dockerfile: Dockerfile.dev

This defines a service named web that builds the image using the Dockerfile.dev Dockerfile in the current directory.

In conclusion, the Docker Compose file is a powerful tool for defining and managing multi-container Docker applications. The syntax is simple and easy to understand, but it provides a lot of flexibility and customization options. Understanding the syntax is essential for creating and managing Docker Compose files effectively.