Sobes.tech
Junior

What is Docker Compose?

sobes.tech AI

Answer from AI

Tool for defining and running multi-container Docker applications. Allows describing services, networks, and volumes in a single YAML file and launching them with one command.

Main features:

  • Defining multiple services in one file.
  • Creating and starting all services with one command.
  • Managing the application lifecycle (start, stop, restart, scaling).
  • Network interaction between containers.
  • Applying configuration on the fly.

Example of a docker-compose.yml file:

version: '3.8' # File format version

services:
  web: # Service name
    image: nginx:latest # Used image
    ports:
      - "80:80" # Port forwarding
    volumes:
      - ./html:/usr/share/nginx/html # Volume mapping

  app: # Another service name
    build: . # Build image from Dockerfile in current directory
    ports:
      - "5000:5000"
    depends_on: # Dependency on another service
      - db

  db: # Database service name
    image: postgres:latest
    environment: # Environment variables
      POSTGRES_DB: mydatabase
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypassword

volumes: # Defined volumes
  data: # Named volume for data

Commands:

  • docker-compose up: Starts containers.
  • docker-compose down: Stops and removes containers.
  • docker-compose build: Builds images.
  • docker-compose ps: Shows the status of running containers.

Advantages:

  • Simplifies management of complex multi-container applications.
  • Ensures repeatability of deployment.
  • Facilitates local development and testing.