
# Usage
#   Start:          docker-compose up
#   With helpers:   docker-compose -f docker-compose.yml -f ./dev/docker-compose.dev.yml up
#   Stop:           docker-compose down
#   Destroy:        docker-compose -f docker-compose.yml -f ./dev/docker-compose.dev.yml down -v --remove-orphans

version: "3.8"

services:
  studio:
    container_name: supabase-studio
    image: supabase/studio:latest
    restart: unless-stopped
    labels:
      - traefik.enable=true
      - traefik.http.routers.supabase-front.rule=Host(`${URL_SUPABASE_FRONT}`)
      - traefik.http.routers.supabase-front.entrypoints=websecure
      - traefik.http.routers.supabase-front.tls=true
      - traefik.http.routers.supabase-front.tls.certresolver=lets-encrypt
      - traefik.http.routers.supabase-front.middlewares=supabase-front-auth
      - "traefik.http.middlewares.supabase-front-auth.basicauth.users=$creditentials"
      - traefik.http.routers.supabase-front.service=supabase-front-srv
      - traefik.http.services.supabase-front-srv.loadbalancer.server.port=3000
      - traefik.docker.network=${NETWORK_TRAEFIK}
    environment:
      SUPABASE_URL: https://${URL_SUPABASE_BACK}
      STUDIO_PG_META_URL: http://meta:8080
      SUPABASE_ANON_KEY: ${ANON_KEY}
      SUPABASE_SERVICE_KEY: ${SERVICE_ROLE_KEY}
    networks:
      - traefik-proxy
      - supabase-network  

  kong:
    container_name: supabase-kong
    image: kong:2.1
    restart: unless-stopped
    labels:
      - traefik.enable=true
      - traefik.http.routers.supabase-back.rule=Host(`${URL_SUPABASE_BACK}`)
      - traefik.http.routers.supabase-back.entrypoints=websecure
      - traefik.http.routers.supabase-back.tls=true
      - traefik.http.routers.supabase-back.tls.certresolver=lets-encrypt
      - traefik.http.routers.supabase-back.service=supabase-back-srv
      - traefik.http.services.supabase-back-srv.loadbalancer.server.port=8000
      - traefik.docker.network=${NETWORK_TRAEFIK}
    environment:
      KONG_DATABASE: "off"
      KONG_DECLARATIVE_CONFIG: /var/lib/kong/kong.yml
      # https://github.com/supabase/cli/issues/14
      KONG_DNS_ORDER: LAST,A,CNAME
      KONG_PLUGINS: request-transformer,cors,key-auth,acl
    volumes:
      - ./volumes/api/kong.yml:/var/lib/kong/kong.yml
    networks:
      - traefik-proxy
      - supabase-network 

  auth:
    container_name: supabase-auth
    image: supabase/gotrue:v2.2.6
    depends_on:
      - db
    restart: unless-stopped
    environment:
      GOTRUE_API_HOST: 0.0.0.0
      GOTRUE_API_PORT: 9999

      GOTRUE_DB_DRIVER: postgres
      GOTRUE_DB_DATABASE_URL: postgres://postgres:${POSTGRES_PASSWORD}@db:5432/postgres?sslmode=disable&search_path=auth

      GOTRUE_SITE_URL: ${SITE_URL}
      GOTRUE_URI_ALLOW_LIST: ${ADDITIONAL_REDIRECT_URLS}
      GOTRUE_DISABLE_SIGNUP: ${DISABLE_SIGNUP}

      GOTRUE_JWT_SECRET: ${JWT_SECRET}
      GOTRUE_JWT_EXP: ${JWT_EXPIRY}
      GOTRUE_JWT_DEFAULT_GROUP_NAME: authenticated

      GOTRUE_EXTERNAL_EMAIL_ENABLED: ${ENABLE_EMAIL_SIGNUP}
      GOTRUE_MAILER_AUTOCONFIRM: ${ENABLE_EMAIL_AUTOCONFIRM}
      GOTRUE_SMTP_ADMIN_EMAIL: ${SMTP_ADMIN_EMAIL}
      GOTRUE_SMTP_HOST: ${SMTP_HOST}
      GOTRUE_SMTP_PORT: ${SMTP_PORT}
      GOTRUE_SMTP_USER: ${SMTP_USER}
      GOTRUE_SMTP_PASS: ${SMTP_PASS}
      GOTRUE_SMTP_SENDER_NAME: ${SMTP_SENDER_NAME}
      GOTRUE_MAILER_URLPATHS_INVITE: /auth/v1/verify
      GOTRUE_MAILER_URLPATHS_CONFIRMATION: /auth/v1/verify
      GOTRUE_MAILER_URLPATHS_RECOVERY: /auth/v1/verify
      GOTRUE_MAILER_URLPATHS_EMAIL_CHANGE: /auth/v1/verify

      GOTRUE_EXTERNAL_PHONE_ENABLED: ${ENABLE_PHONE_SIGNUP}
      GOTRUE_SMS_AUTOCONFIRM: ${ENABLE_PHONE_AUTOCONFIRM}
    networks:
      - supabase-network 

  rest:
    container_name: supabase-rest
    image: postgrest/postgrest:v8.0.0.20211102
    depends_on:
      - db
    restart: unless-stopped
    environment:
      PGRST_DB_URI: postgres://postgres:${POSTGRES_PASSWORD}@db:5432/postgres
      PGRST_DB_SCHEMA: public, storage
      PGRST_DB_ANON_ROLE: anon
      PGRST_JWT_SECRET: ${JWT_SECRET}
      PGRST_DB_USE_LEGACY_GUCS: "false"
    networks:
      - supabase-network 

  realtime:
    container_name: supabase-realtime
    image: supabase/realtime:v0.19.0
    depends_on:
      - db
    restart: unless-stopped
    environment:
      DB_HOST: db
      DB_PORT: 5432
      DB_NAME: postgres
      DB_USER: postgres
      DB_PASSWORD: ${POSTGRES_PASSWORD}
      DB_SSL: "false"
      PORT: 4000
      JWT_SECRET: ${JWT_SECRET}
      REPLICATION_MODE: RLS
      REPLICATION_POLL_INTERVAL: 100
      SECURE_CHANNELS: "true"
      SLOT_NAME: supabase_realtime_rls
      TEMPORARY_SLOT: "true"
    command: >
      bash -c "./prod/rel/realtime/bin/realtime eval Realtime.Release.migrate
      && ./prod/rel/realtime/bin/realtime start"
    networks:
      - supabase-network 
      
  storage:
    container_name: supabase-storage
    image: supabase/storage-api:v0.9.3
    depends_on:
      - db
      - rest
    restart: unless-stopped
    environment:
      ANON_KEY: ${ANON_KEY}
      SERVICE_KEY: ${SERVICE_ROLE_KEY}
      POSTGREST_URL: http://rest:3000
      PGRST_JWT_SECRET: ${JWT_SECRET}
      DATABASE_URL: postgres://postgres:${POSTGRES_PASSWORD}@db:5432/postgres
      PGOPTIONS: -c search_path=storage
      FILE_SIZE_LIMIT: 52428800
      STORAGE_BACKEND: file
      FILE_STORAGE_BACKEND_PATH: /var/lib/storage
      # TODO: https://github.com/supabase/storage-api/commit/a836fc9666c2434d89ca4b31402f74772d50fb6d
      PROJECT_REF: stub
      # TODO: https://github.com/supabase/storage-api/issues/55
      REGION: stub
      GLOBAL_S3_BUCKET: stub
    volumes:
      - ./volumes/storage:/var/lib/storage
    networks:
      - supabase-network 

  meta:
    container_name: supabase-meta
    image: supabase/postgres-meta:v0.28.0
    depends_on:
      - db
    restart: unless-stopped
    environment:
      PG_META_PORT: 8080
      PG_META_DB_HOST: db
      PG_META_DB_PASSWORD: ${POSTGRES_PASSWORD}
    networks:
      - supabase-network 

  db:
    container_name: supabase-db
    image: supabase/postgres:13.3.0
    command: postgres -c wal_level=logical
    restart: unless-stopped
    environment:
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      PGDATA: /data/postgres
    volumes:
      - postgressupa:/data/postgres
      - ./volumes/db/init:/docker-entrypoint-initdb.d
    networks:
      - supabase-network

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


volumes:
 postgressupa:
    driver: local
    driver_opts:
      type: none
      device: ${VOLUME_POSTGRES_SUPABASE}
      o: bind