version: "3.8"

services:

  redis:
    image: redis:alpine
    command: --save 900 1
    ports:
      - "6379:6379"
    volumes:
      - /data/redis:/data
    deploy:
      replicas: 1
      placement:
        constraints:
          - node.role == manager
      restart_policy:
        condition: on-failure
  
  postgres:
    image: postgres:alpine
    ports:
      - "5432:5432"
    volumes:
      - /data/postgres:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=airflow
      - POSTGRES_PASSWORD=airflow
      - POSTGRES_DB=airflow
    deploy:
      replicas: 1
      placement:
        constraints:
          - node.role == manager
      restart_policy:
        condition: on-failure

  webserver:
    image: vimagick/airflow
    command: webserver
    ports:
      - "8080:8080"
    volumes:
      - airflow_data:/opt/airflow
    deploy:
      replicas: 1
      placement:
        constraints:
          - node.role == manager
      restart_policy:
        condition: on-failure
    depends_on:
      - postgres
      - redis

  scheduler:
    image: vimagick/airflow
    command: scheduler
    volumes:
      - airflow_data:/opt/airflow
    deploy:
      replicas: 1
      placement:
        constraints:
          - node.role == manager
      restart_policy:
        condition: on-failure
    depends_on:
      - webserver

  flower:
    image: vimagick/airflow
    command: flower
    ports:
      - "5555:5555"
    volumes:
      - airflow_data:/opt/airflow
    deploy:
      replicas: 1
      placement:
        constraints:
          - node.role == manager
      restart_policy:
        condition: on-failure
    depends_on:
      - webserver

  worker:
    image: vimagick/airflow
    command: worker
    volumes:
      - airflow_data:/opt/airflow
    deploy:
      replicas: 3
      placement:
        constraints:
          - node.role == worker
        max_replicas_per_node: 1
      restart_policy:
        condition: on-failure
    depends_on:
      - webserver

volumes:

  airflow_data:
    driver: local
    driver_opts:
      type: nfs
      o: "addr=10.0.0.1,nolock,soft,rw"
      device: ":/export/airflow"
