version: "2.1"

services:
  nocodb:
    container_name: nocodb
    depends_on:
      nocodb-db:
        condition: service_healthy
    environment:
      - NC_DB=pg://nocodb-db:5432?u=postgres&p=${NOCODB_DB_PASSWORD}&d=nocodb
      - NC_PUBLIC_URL=${APP_PROTOCOL:-http}://${APP_DOMAIN}
      - NC_AUTH_JWT_SECRET=${NOCODB_JWT_SECRET}
      - NC_REDIS_URL=redis://default:${NOCODB_REDIS_PASSWORD}@nocodb-redis:6379
    image: "nocodb/nocodb:0.202.10"
    ports:
      - "${APP_PORT}:8080"
    restart: always
    volumes:
      - "${APP_DATA_DIR}/data/nocode-data:/usr/app/data"
    networks:
      - tipi_main_network
    labels:
      # Main
      traefik.enable: true
      traefik.http.middlewares.nocodb-web-redirect.redirectscheme.scheme: https
      traefik.http.services.nocodb.loadbalancer.server.port: 8080
      # Web
      traefik.http.routers.nocodb-insecure.rule: Host(`${APP_DOMAIN}`)
      traefik.http.routers.nocodb-insecure.entrypoints: web
      traefik.http.routers.nocodb-insecure.service: nocodb
      traefik.http.routers.nocodb-insecure.middlewares: nocodb-web-redirect
      # Websecure
      traefik.http.routers.nocodb.rule: Host(`${APP_DOMAIN}`)
      traefik.http.routers.nocodb.entrypoints: websecure
      traefik.http.routers.nocodb.service: nocodb
      traefik.http.routers.nocodb.tls.certresolver: myresolver
      # Local domain
      traefik.http.routers.nocodb-local-insecure.rule: Host(`nocodb.${LOCAL_DOMAIN}`)
      traefik.http.routers.nocodb-local-insecure.entrypoints: web
      traefik.http.routers.nocodb-local-insecure.service: nocodb
      traefik.http.routers.nocodb-local-insecure.middlewares: nocodb-web-redirect
      # Local domain secure
      traefik.http.routers.nocodb-local.rule: Host(`nocodb.${LOCAL_DOMAIN}`)
      traefik.http.routers.nocodb-local.entrypoints: websecure
      traefik.http.routers.nocodb-local.service: nocodb
      traefik.http.routers.nocodb-local.tls: true

  nocodb-db:
    container_name: nocodb-db
    environment:
      POSTGRES_DB: nocodb
      POSTGRES_PASSWORD: ${NOCODB_DB_PASSWORD}
      POSTGRES_USER: postgres
    healthcheck:
      interval: 10s
      retries: 10
      test: 'pg_isready -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"'
      timeout: 2s
    image: postgres
    restart: always
    volumes:
      - ${APP_DATA_DIR}/data/postgres:/var/lib/postgresql/data
    networks:
      - tipi_main_network

  nocodb-redis:
    image: redis:alpine
    container_name: nocodb-redis
    restart: unless-stopped
    command: "redis-server --requirepass ${NOCODB_REDIS_PASSWORD}"
    volumes:
      - ${APP_DATA_DIR}/data/redis:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 1s
      timeout: 3s
      retries: 30
    networks:
      - tipi_main_network