Docker Compose is a config file used for defining and running multi-container applications.

  • It simplifies the management of services, networks, and volumes
  • It help define dependencies btw the above mentioned
  • The compose file can have one of the following names,
    • compose.yaml
    • compose.yaml
    • docker-compose.yaml
    • docker-compose.yml

Parts of a docker-compse.yml

  • version: Specifies the version of the Compose file format being used.
  • services: Defines the different services that make up your application, including their configurations like image, ports, volumes, environment variables, and more.
  • networks: Allows you to define custom networks for your services to communicate with each other.
  • volumes: Defines named volumes that can be mounted into containers for persistent storage.
  • configs: Specifies configurations that can be used by services in the stack.
  • secrets: Manages sensitive data that can be securely used by services.

Example

services:
 
  app:
    image: nginx:latest
    volumes:
      - ./webapp:/usr/share/nginx/html
    ports:
      - 80:80
 
  db:
    image: kartoza/postgis:16-3.4
    volumes:
      - ./data-generation/init.sql:/docker-entrypoint-initdb.d/init.sql
    restart: always
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: gfs_db
    ports:
      - "5432:5432"
  
  test-db:
    container_name: test_gfs_db
    image: kartoza/postgis:16-3.4
    volumes:
      - ./data-generation/init.sql:/docker-entrypoint-initdb.d/init.sql
    restart: always
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: gfs_db
    ports:
      - "5433:5432"
docker compose up -d
docker compose down


Refs

  1. https://docs.docker.com/compose/