version: "3.6"

volumes:
  logs:
  media:
  postgres-data:

services:
  db:
    image: postgres:15
    healthcheck:
      test: /usr/bin/pg_isready
      interval: 5s
      timeout: 10s
      retries: 10
    environment:
      POSTGRES_DB: "${POSTGRES_DB:-newsreader}"
      POSTGRES_USER: "${POSTGRES_USER:-newsreader}"
      POSTGRES_PASSWORD: "${POSTGRES_PASSWORD:-newsreader}"
    volumes:
      - postgres-data:/var/lib/postgresql/data

  rabbitmq:
    image: rabbitmq:3.7

  memcached:
    image: memcached:1.6
    entrypoint:
      - memcached
      - -m 64

  celery:
    build:
      context: .
      dockerfile: ./docker/django
    command: |
      celery worker
      -n worker1@%h
      -n worker2@%h
      --app newsreader
      --loglevel INFO
      --concurrency 2
      --workdir /app/src/
      --beat
      --scheduler django
    environment:
      - DJANGO_SETTINGS_MODULE=newsreader.conf.production
    depends_on:
      rabbitmq:
        condition: service_started
      memcached:
        condition: service_started
      db:
        condition: service_healthy
      django:
        condition: service_healthy
    volumes:
      - logs:/app/logs

  django:
    build:
      context: .
      dockerfile: ./docker/django
      target: production
    entrypoint: /app/bin/docker-entrypoint.sh
    command: |
      gunicorn
      --bind 0.0.0.0:${DJANGO_PORT:-8000}
      --workers 3
      newsreader.wsgi:application
    healthcheck:
      test: /usr/bin/curl --fail http://django:${DJANGO_PORT:-8000} || exit 1
      interval: 30s
      timeout: 10s
      retries: 10
    environment:
      - DJANGO_SETTINGS_MODULE=newsreader.conf.production
    depends_on:
      memcached:
        condition: service_started
      db:
        condition: service_healthy
    volumes:
      - logs:/app/logs
      - media:/app/media
