version: "3"

# optional ports are specified throughout for more advanced use cases.

services:
  app-service:
    restart: always
    image: budibase.docker.scarf.sh/budibase/apps
    container_name: bbapps
    environment:
      SELF_HOSTED: 1
      COUCH_DB_URL: http://${COUCH_DB_USER}:${COUCH_DB_PASSWORD}@couchdb-service:5984
      WORKER_URL: http://worker-service:4003
      MINIO_URL: http://minio-service:9000
      MINIO_ACCESS_KEY: ${MINIO_ACCESS_KEY}
      MINIO_SECRET_KEY: ${MINIO_SECRET_KEY}
      INTERNAL_API_KEY: ${INTERNAL_API_KEY}
      BUDIBASE_ENVIRONMENT: ${BUDIBASE_ENVIRONMENT}
      PORT: 4002
      JWT_SECRET: ${JWT_SECRET}
      LOG_LEVEL: info
      SENTRY_DSN: https://a34ae347621946bf8acded18e5b7d4b8@o420233.ingest.sentry.io/5338131
      ENABLE_ANALYTICS: "true"
      REDIS_URL: redis-service:6379
      REDIS_PASSWORD: ${REDIS_PASSWORD}
    networks:
      - budibase-network
      - traefik-proxy
    depends_on:
      - worker-service
      - redis-service

  worker-service:
    restart: always
    image: budibase.docker.scarf.sh/budibase/worker
    container_name: bbworker
    environment:
      SELF_HOSTED: 1
      PORT: 4003
      CLUSTER_PORT: ${MAIN_PORT}
      JWT_SECRET: ${JWT_SECRET}
      MINIO_ACCESS_KEY: ${MINIO_ACCESS_KEY}
      MINIO_SECRET_KEY: ${MINIO_SECRET_KEY}
      MINIO_URL: http://minio-service:9000
      APPS_URL: http://app-service:4002
      COUCH_DB_USERNAME: ${COUCH_DB_USER}
      COUCH_DB_PASSWORD: ${COUCH_DB_PASSWORD}
      COUCH_DB_URL: http://${COUCH_DB_USER}:${COUCH_DB_PASSWORD}@couchdb-service:5984
      SENTRY_DSN: https://a34ae347621946bf8acded18e5b7d4b8@o420233.ingest.sentry.io/5338131
      INTERNAL_API_KEY: ${INTERNAL_API_KEY}
      REDIS_URL: redis-service:6379
      REDIS_PASSWORD: ${REDIS_PASSWORD}
    networks:
      - budibase-network
      - traefik-proxy
    depends_on:
      - redis-service
      - minio-service
      - couch-init

  minio-service:
    restart: always
    image: minio/minio
    volumes:
      - minio_data:/data
    environment:
      MINIO_ACCESS_KEY: ${MINIO_ACCESS_KEY}
      MINIO_SECRET_KEY: ${MINIO_SECRET_KEY}
      MINIO_BROWSER: "off"
    command: server /data
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
      interval: 30s
      timeout: 20s
      retries: 3
    networks:
      - budibase-network

  proxy-service:
    restart: always
    labels:
      - traefik.enable=true
      - traefik.http.routers.budibase.rule=Host(`${URL_BUDIBASE}`)
      - traefik.http.routers.budibase.entrypoints=websecure
      - traefik.http.routers.budibase.tls=true
      - traefik.http.routers.budibase.tls.certresolver=lets-encrypt
      - traefik.http.routers.budibase.service=budibase
      - traefik.http.services.budibase.loadbalancer.server.port=${MAIN_PORT}
      - traefik.docker.network=${NETWORK_TRAEFIK}
    container_name: bbproxy
    image: budibase/proxy
    networks:
      - traefik-proxy
      - budibase-network
    depends_on:
      - minio-service
      - worker-service
      - app-service
      - couchdb-service


  couchdb-service:
    restart: always
    image: ibmcom/couchdb3
    networks:
      - budibase-network
    environment:
      - COUCHDB_PASSWORD=${COUCH_DB_PASSWORD}
      - COUCHDB_USER=${COUCH_DB_USER}
    volumes:
      - couchdb3_data:/opt/couchdb/data

  couch-init:
    image: curlimages/curl
    networks:
      - budibase-network
    environment:
      PUT_CALL: "curl -u ${COUCH_DB_USER}:${COUCH_DB_PASSWORD} -X PUT couchdb-service:5984"
    depends_on:
      - couchdb-service
    command: ["sh","-c","sleep 10 && $${PUT_CALL}/_users && $${PUT_CALL}/_replicator; fg;"]

  redis-service:
    restart: always
    image: redis
    command: redis-server --requirepass ${REDIS_PASSWORD}
    networks:
      - budibase-network
    volumes:
      - redis_data:/data

  watchtower-service:
    image: containrrr/watchtower
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    command: --debug --http-api-update bbapps bbworker bbproxy
    environment:
      - WATCHTOWER_HTTP_API=true
      - WATCHTOWER_HTTP_API_TOKEN=budibase
      - WATCHTOWER_CLEANUP=true
    networks:
      - budibase-network
    labels:
     - "com.centurylinklabs.watchtower.enable=false"

networks:
  traefik-proxy:
    external:
      name: ${NETWORK_TRAEFIK}
  budibase-network:
    internal: true
      

volumes:
  couchdb3_data:
    driver: local
    driver_opts:
      type: none
      device: ${VOLUME_COUCHDB_BUDIBASE}
      o: bind

  minio_data:
    driver: local
    driver_opts:
      type: none
      device: ${VOLUME_MINIO_BUDIBASE}
      o: bind

  redis_data:
    driver: local
    driver_opts:
      type: none
      device: ${VOLUME_REDIS_BUDIBASE}
      o: bind

