Sobes.tech
Junior — Middle

Can you explain the purpose and structure of the docker-compose.yml file in Docker, as well as its main functions?

sobes.tech AI

Answer from AI

The docker-compose.yml file is used to define and run multi-container Docker applications. It describes services (containers), their images, network settings, volumes, and dependencies between them.

Main functions and structure:

  • services — a list of services, each describing a container with parameters:

    • image — the Docker image to run
    • build — instruction to build the image from a Dockerfile
    • ports — port forwarding from container to host
    • volumes — mounting volumes
    • environment — environment variables
    • depends_on — dependencies between services
  • networks — configuration of custom networks

  • volumes — definition of volumes for data storage

Example:

version: '3'
services:
  web:
    build: ./webapp
    ports:
      - "80:80"
    volumes:
      - ./webapp:/app
    depends_on:
      - db
  db:
    image: postgres:13
    environment:
      POSTGRES_PASSWORD: example
    volumes:
      - db-data:/var/lib/postgresql/data
volumes:
  db-data:

Thus, docker-compose.yml simplifies the management of complex applications, allowing you to run and configure multiple containers with a single command.