version: '3'

services:
  lb:
    image: haproxy:latest
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - "./data/certificates:/certificates"
      - "./lb:/usr/local/etc/haproxy"
    depends_on:
      - "certbot"
      - "flask"
      - "osmtile"
    restart: always
    networks:
      - internal
    labels:
      - meta.role=lb
  certbot:
    image: "certbot/certbot"
    hostname: certbot
    volumes:
      - "./data/certificates:/certificates"
      - "./data/certbot/etc:/etc/letsencrypt"
      - "./data/certbot/var:/var/lib/letsencrypt"
      - "./logs/certbot:/var/log/letsencrypt"
    restart: on-failure
    networks:
      - internal
    labels:
      - meta.role=certbot
    #entrypoint: sh -c 'while true; do sleep 1; done'
    entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 15d & wait $${!}; done;'"
  db:
    image: postgres:13-alpine
    volumes:
      - "./data/db/pgdata:/var/lib/postgresql/data/"
    env_file:
      - ./.env.prod
    restart: always
    networks:
      - internal
  pgadmin:
    image: dpage/pgadmin4
    #volumes:
    #  - "./data/dbadmin:/var/lib/pgadmin"
    env_file:
      - ./.env.prod
    restart: always
    networks:
      - internal
    depends_on:
      - db
  flask:
    build:
      context: ./flask
      dockerfile: Dockerfile.prod
    command: gunicorn --bind 0.0.0.0:5000 manage:app
    expose:
      - 5000
    env_file:
      - ./.env.prod
    networks:
      - internal 
    depends_on:
      - db
  osmtile:
    image: "overv/openstreetmap-tile-server:latest"
    hostname: osmtile
    environment:
      - "DOWNLOAD_PBF=https://download.geofabrik.de/europe/bulgaria-latest.osm.pbf"
      - "DOWNLOAD_POLY=https://download.geofabrik.de/europe/bulgaria.poly"
    volumes:
      - "./data/osmtile/pgdata:/var/lib/postgresql/12/main"
    networks:
      - internal
    #entrypoint: "/run.sh import"
    entrypoint: "/run.sh run"
    restart: always
networks:
  internal: {} 
