version: "3.8"

volumes:
  righttree-postgres-data:
    name: righttree-postgres-data

services:

  backend_migrate:
    restart: on-failure
    image: right-tree
    container_name: backend_migrate
    depends_on:
      - postgres
    volumes:
      - ./backend:/app
    user: "$UID:$GID"
    environment:
      DATABASE_NAME: righttree
      DATABASE_USER: righttree
      DATABASE_PASSWORD: righttree
      DATABASE_HOST: postgres
    command:
      - bash
      - -c
      - python manage.py makemigrations --noinput && python manage.py migrate --noinput

  backend:
    restart: unless-stopped
    image: right-tree
    container_name: backend
    depends_on:
      - postgres
      - backend_migrate
    volumes:
      - ./backend:/app
    user: "$UID:$GID"
    expose:
      - "8000"
    environment:
      LINZ_API_KEY: myapikeyhere
      DATABASE_NAME: righttree
      DATABASE_USER: righttree
      DATABASE_PASSWORD: righttree
      DATABASE_HOST: postgres
    command:
      - gunicorn
      - --reload
      - --bind=0.0.0.0:8000
      - --timeout=300
      - right_tree.wsgi

  frontend:
    image: node:16-bullseye
    restart: unless-stopped
    container_name: frontend
    volumes:
      - ./frontend:/app
    working_dir: /app
    user: "$UID:$GID"
    ports:
      - 3000:3000
    command:
      - npm
      - start

  postgres:
    image: postgis/postgis:13-3.1
    restart: unless-stopped
    container_name: postgres
    volumes:
      - righttree-postgres-data:/var/lib/postgresql/data
      - ./create_database.sql:/docker-entrypoint-initdb.d/create_database.sql
    ports:
      - 5432:5432
    environment:
      POSTGRES_PASSWORD: postgres

  nginx:
    image: nginx:1.23.3
    restart: unless-stopped
    container_name: nginx
    depends_on:
      - backend
      - frontend
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
      - ./backend/right_tree/staticfiles:/etc/nginx/html/staticfiles:ro
    ports:
      - "9000:80"
