version: '3.4'
services:

# Common Django template for Geonode, Celery services below
x-common-django:
  &default-common-django
  image: geonode/geonode:latest
  restart: on-failure
  build:
    context: ./
    dockerfile: Dockerfile
  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

services:

  # Our custom django application. It includes Geonode.
  django:
    << : *default-common-django
    container_name: django4${COMPOSE_PROJECT_NAME}
    healthcheck:
      test: "curl --fail --silent --write-out 'HTTP CODE : %{http_code}\n' --output /dev/null http://127.0.0.1:8001/"
      interval: 60s
      timeout: 10s
      retries: 1
      start_period: 60s
    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}
    environment:
      - IS_CELERY=True
    entrypoint: ["/usr/src/geonode/entrypoint.sh"]
    command: "celery -A geonode.celery_app:app worker --without-gossip --without-mingle -Ofair -B -E --statedb=/mnt/volumes/statics/worker.state -s /mnt/volumes/statics/celerybeat-schedule --loglevel=INFO --concurrency=10 -n worker1@%h -f /var/log/celery.log"

  # Nginx is serving django static and media files and proxies to django and geonode
  geonode:
    image: geonode/nginx:3.x
    build: ./scripts/docker/nginx/
    container_name: nginx4${COMPOSE_PROJECT_NAME}
    environment:
      - HTTPS_HOST=${HTTPS_HOST}
      - HTTP_HOST=${HTTP_HOST}
      - HTTPS_PORT=${HTTPS_PORT}
      - HTTP_PORT=${HTTP_PORT}
      - LETSENCRYPT_MODE=${LETSENCRYPT_MODE}
      - 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: on-failure

  # Gets and installs letsencrypt certificates
  letsencrypt:
    image: geonode/spcgeonode:letsencrypt-3.1
    build: ./scripts/spcgeonode/letsencrypt/
    container_name: letsencrypt4${COMPOSE_PROJECT_NAME}
    environment:
      - HTTPS_HOST=${HTTPS_HOST}
      - HTTP_HOST=${HTTP_HOST}
      - ADMIN_EMAIL=${ADMIN_EMAIL}
      - LETSENCRYPT_MODE=${LETSENCRYPT_MODE}
    volumes:
      - nginx-certificates:/geonode-certificates
    restart: on-failure

  # Geoserver backend
  geoserver:
    image: geonode/geoserver:2.17.4
    container_name: geoserver4${COMPOSE_PROJECT_NAME}
    healthcheck:
      test: "curl --fail --silent --write-out 'HTTP CODE : %{http_code}\n' --output /dev/null http://127.0.0.1:8080/geoserver/rest/workspaces/geonode.html"
      interval: 60s
      timeout: 10s
      retries: 1
      start_period: 60s
    env_file:
      - ./scripts/docker/env/${DOCKER_ENV}/geoserver.env
    volumes:
      - statics:/mnt/volumes/statics
      - geoserver-data-dir:/geoserver_data/data
      - backup-restore:/backup_restore
      - data:/data
      - tmp:/tmp
    restart: on-failure

  data-dir-conf:
    image: geonode/geoserver_data:2.17.4
    container_name: gsconf4${COMPOSE_PROJECT_NAME}
    command: /bin/true
    volumes:
      - geoserver-data-dir:/geoserver_data/data
    restart: on-failure

  # PostGIS database.
  db:
    # use geonode official postgis 11 image
    image: geonode/postgis:latest
    container_name: db4${COMPOSE_PROJECT_NAME}
    env_file:
      - ./scripts/docker/env/${DOCKER_ENV}/db.env
    volumes:
      - dbdata:/var/lib/postgresql/data
      - dbbackups:/pg_backups
    restart: on-failure
    # uncomment to enable remote connections to postgres
    #ports:
    #  - "5432:5432"

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

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