Sobes.tech
Junior — Middle

Can you explain how the structure of a Docker Compose file is organized?

sobes.tech AI

Answer from AI

A Docker Compose file is a YAML file that describes the configuration of a multi-container Docker application. It defines services, networks, and volumes needed for the application to run.

Main sections of the file:

  • version: the syntax version of Docker Compose.
  • services: description of containers to be launched. For each service, the image or build instructions, ports, environment variables, volumes, and other parameters are specified.
  • volumes: description of volumes for storing data outside of containers.
  • networks: configuration of networks for container interaction.

Example of a simple docker-compose.yml:

version: '3.8'
services:
  web:
    build: ./web
    ports:
      - "8000:8000"
    volumes:
      - ./web:/app
    environment:
      - DEBUG=1
  db:
    image: postgres:13
    volumes:
      - db-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=example
volumes:
  db-data:

This file describes two services: web (built from a local folder) and db (uses the official Postgres image). It also defines a volume named db-data for storing database data.