version: '3.9'

# Common Django template for GeoNode and Celery services below
x-common-django:
  &default-common-django
  image: geonode/geonode:latest-ubuntu-22.04
  build:
      context: ./
      dockerfile: Dockerfile
  restart: unless-stopped
  env_file:
    - .env_test
  volumes:
    # - '.:/usr/src/geonode'
    - statics:/mnt/volumes/statics
    - geoserver-data-dir:/geoserver_data/data
    - backup-restore:/backup_restore
    - data:/data
    - tmp:/tmp
  depends_on:
    db:
      condition: service_healthy

services:

  # Our custom django application. It includes Geonode.
  django:
    << : *default-common-django
    container_name: django4${COMPOSE_PROJECT_NAME}
    healthcheck:
      test: "curl -m 10 --fail --silent --write-out 'HTTP CODE : %{http_code}\n' --output /dev/null http://django:8000/"
      start_period: 60s
      interval: 60s
      timeout: 10s
      retries: 5
    environment:
      - IS_CELERY=False
    entrypoint: ["/usr/src/geonode/entrypoint.sh"]
    command: "uwsgi --ini /usr/src/geonode/uwsgi.ini"

  # Celery worker that executes celery tasks created by Django.
  celery:
    << : *default-common-django
    container_name: celery4${COMPOSE_PROJECT_NAME}
    depends_on:
      django:
        condition: service_healthy
    environment:
      - IS_CELERY=True
    entrypoint: ["/usr/src/geonode/entrypoint.sh"]
    command: "celery-cmd"

  # Nginx is serving django static and media files and proxies to django and geonode
  geonode:
    image: geonode/nginx:1.25.3-latest
    container_name: nginx4${COMPOSE_PROJECT_NAME}
    env_file:
      - .env_test
    environment:
      - RESOLVER=127.0.0.11
    ports:
      - "${HTTP_PORT}:80"
      - "${HTTPS_PORT}:443"
    volumes:
      - nginx-confd:/etc/nginx
      - nginx-certificates:/geonode-certificates
      - statics:/mnt/volumes/statics
    restart: unless-stopped

  # memcached service
  memcached:
    image: memcached:alpine
    container_name: memcached4${COMPOSE_PROJECT_NAME}
    command: memcached ${MEMCACHED_OPTIONS}
    restart: on-failure
    healthcheck:
      test: nc -z 127.0.0.1 11211
      interval: 30s
      timeout: 30s
      retries: 5
      start_period: 30s

  # Gets and installs letsencrypt certificates
  letsencrypt:
    image: geonode/letsencrypt:2.6.0-latest
    container_name: letsencrypt4${COMPOSE_PROJECT_NAME}
    env_file:
      - .env_test
    volumes:
      - nginx-certificates:/geonode-certificates
    restart: unless-stopped

  # Geoserver backend
  geoserver:
    image: geonode/geoserver:2.24.3-latest
    container_name: geoserver4${COMPOSE_PROJECT_NAME}
    healthcheck:
      test: "curl -m 10 --fail --silent --write-out 'HTTP CODE : %{http_code}\n' --output /dev/null http://geoserver:8080/geoserver/ows"
      start_period: 60s
      interval: 60s
      timeout: 10s
      retries: 2
    env_file:
      - .env_test
    ports:
      - "8080:8080"
    volumes:
      - statics:/mnt/volumes/statics
      - geoserver-data-dir:/geoserver_data/data
      - backup-restore:/backup_restore
      - data:/data
      - tmp:/tmp
    restart: unless-stopped
    depends_on:
      data-dir-conf:
        condition: service_healthy
      django:
        condition: service_healthy

  data-dir-conf:
    image: geonode/geoserver_data:2.24.3-latest
    container_name: gsconf4${COMPOSE_PROJECT_NAME}
    entrypoint: sleep infinity
    volumes:
      - geoserver-data-dir:/geoserver_data/data
    restart: unless-stopped
    healthcheck:
      test: "ls -A '/geoserver_data/data' | wc -l"

  # PostGIS database.
  db:
    # use geonode official postgis 15 image
    image: geonode/postgis:15.3-latest
    command: postgres -c "max_connections=${POSTGRESQL_MAX_CONNECTIONS}"
    container_name: db4${COMPOSE_PROJECT_NAME}
    env_file:
      - .env_test
    volumes:
      - dbdata:/var/lib/postgresql/data
      - dbbackups:/pg_backups
    restart: unless-stopped
    healthcheck:
      test: "pg_isready -d postgres -U postgres"
    # uncomment to enable remote connections to postgres
    #ports:
    #  - "5432:5432"

  # Vanilla RabbitMQ service. This is needed by celery
  rabbitmq:
    image: rabbitmq:3-alpine
    container_name: rabbitmq4${COMPOSE_PROJECT_NAME}
    volumes:
      - rabbitmq:/var/lib/rabbitmq
    restart: unless-stopped

volumes:
  statics:
    name: ${COMPOSE_PROJECT_NAME}-statics
  nginx-confd:
    name: ${COMPOSE_PROJECT_NAME}-nginxconfd
  nginx-certificates:
    name: ${COMPOSE_PROJECT_NAME}-nginxcerts
  geoserver-data-dir:
    name: ${COMPOSE_PROJECT_NAME}-gsdatadir
  dbdata:
    name: ${COMPOSE_PROJECT_NAME}-dbdata
  dbbackups:
    name: ${COMPOSE_PROJECT_NAME}-dbbackups
  backup-restore:
    name: ${COMPOSE_PROJECT_NAME}-backup-restore
  data:
    name: ${COMPOSE_PROJECT_NAME}-data
  tmp:
    name: ${COMPOSE_PROJECT_NAME}-tmp
  rabbitmq:
    name: ${COMPOSE_PROJECT_NAME}-rabbitmq
