Skip to content

docker-compose-version-3-syntax

docker-compose-version-3-syntax refers to the configuration structure and file format utilized in Docker Compose to define and run multi-container Docker applications. The "version: 3" specification designates the specific schema and compatibility rules for the compose file.^[600-developer__docker__docker-compose__docker-compouse-02.md]

Key Components

The syntax is structured around a root version declaration and a services dictionary^[600-developer__docker__docker-compose__docker-compouse-02.md]. Within services, individual containers are defined as configuration objects, such as databases or web applications^[600-developer__docker__docker-compose__docker-compouse-02.md].

Service Definition

Each service typically consists of the following elements:

  • Image: Specifies the Docker image to use (e.g., mysql:5.7)^[600-developer__docker__docker-compose__docker-compouse-02.md].
  • Ports: Defines the mapping between the host port and the container port (e.g., "33060:3306")^[600-developer__docker__docker-compose__docker-compouse-02.md].
  • Volumes: Mounts a path or named volume from the host machine into the container, often used for data persistence^[600-developer__docker__docker-compose__docker-compouse-02.md].
  • Restart Policy: Determines the container's behavior upon exit or failure; restart: always ensures the container restarts automatically^[600-developer__docker__docker-compose__docker-compouse-02.md].
  • Environment: A list of environment variables passed to the container, which can be used to configure the application at runtime^[600-developer__docker__docker-compose__docker-compouse-02.md].

Example Configuration

The following example demonstrates a typical service configuration using the Version 3 syntax, specifically for a MySQL database:

version: "3"
services:

  db:
    image: mysql:5.7
    ports:
      - "33060:3306"
    volumes:
      - ./data:/var/lib/mysql
    restart: always
    environment:
      - MYSQL_USER=tommy
      - MYSQL_PASSWORD=tommy
      - MYSQL_DATABASE=dockerDb
      - MYSQL_ROOT_PASSWORD=root
^[600-developer__docker__docker-compose__docker-compouse-02.md]

Sources