Files
backend-python/template/{{ part_dir }}/docker-compose.yml.jinja
T

145 lines
4.8 KiB
Django/Jinja

{%- set has_scripts = dynamic_config or 'kurigram_userbot' in backend_services -%}
{%- set svc_networks = [] -%}
{%- if database != 'none' %}{% set _ = svc_networks.append('database') %}{% endif -%}
{%- if use_redis %}{% set _ = svc_networks.append('redis') %}{% endif -%}
{%- set external_services = [] -%}
{%- if database == 'mongo' %}{% set _ = external_services.append('mongodb') %}{% endif -%}
{%- if database == 'postgres' %}{% set _ = external_services.append('postgres') %}{% endif -%}
{%- if use_redis %}{% set _ = external_services.append('redis') %}{% endif -%}
{%- set app_services = [] -%}
{%- if 'api' in backend_services %}{% set _ = app_services.append({'name': 'api', 'command': '[api]'}) %}{% endif -%}
{%- if 'aiogram_bot' in backend_services %}{% set _ = app_services.append({'name': 'bot', 'command': '[bot]'}) %}{% endif -%}
{%- if 'taskiq_worker' in backend_services %}{% set _ = app_services.append({'name': 'worker', 'command': '[worker]'}) %}{% endif -%}
{%- if 'kurigram_userbot' in backend_services %}{% set _ = app_services.append({'name': 'userbot', 'command': '[userbot]'}) %}{% endif -%}
{%- if 'scheduler' in backend_services %}{% set _ = app_services.append({'name': 'scheduler', 'command': ('[worker, scheduler]' if 'taskiq_worker' in backend_services else '[scheduler]')}) %}{% endif -%}
{%- macro backend_service(name, command) %} {{ name }}:
build: {{ part_dir }}
image: {{ project_slug }}/backend
profiles: [{{ name }}, services]
restart: unless-stopped
env_file:
- path: .env
required: false
{% if layout == 'part' %} - path: backend/.env
required: false
{% endif %} environment:
RUN_ENVIRONMENT: prod
TZ: ${TZ:-UTC}
volumes:
- ./{{ prefix }}src:/app/src
{% if has_scripts %} - ./{{ prefix }}scripts:/app/scripts
{% endif %}{% if name == 'userbot' %} - ./{{ prefix }}sessions:/app/sessions
{% endif %}{% if name == 'api' and serve_spa %} - ./frontend/build:/app/static:ro
{% endif %} command: {{ command }}
{% if external_services %} depends_on:
{% for ext in external_services %} {{ ext }}:
condition: service_healthy
required: false
{% endfor %}{% endif %}{% if svc_networks %} networks:
{% if name == 'api' %} default:
{% endif %}{% for net in svc_networks %} {{ net }}:
{% endfor %}{% endif %}{% endmacro -%}
services:
{% for svc in app_services %}{{ backend_service(svc.name, svc.command) }}{% if not loop.last %}
{% endif %}{% endfor %}
{%- if database == 'mongo' %}
mongodb:
image: mongo:8
profiles: [mongodb, external]
restart: unless-stopped
environment:
MONGO_INITDB_ROOT_USERNAME: ${DB__USER}
MONGO_INITDB_ROOT_PASSWORD: ${DB__PASSWORD}
command: mongod --port ${DB__PORT:-27017}
volumes:
- database:/data/db
healthcheck:
test: ["CMD", "mongosh", "--quiet", "--port", "${DB__PORT:-27017}", "--eval", "db.adminCommand('ping')"]
interval: 5s
timeout: 5s
retries: 5
networks:
database:
aliases:
- ${DB__HOST:-mongodb}
{% endif %}
{%- if database == 'postgres' %}
postgres:
image: postgres:17-alpine
profiles: [postgres, external]
restart: unless-stopped
environment:
POSTGRES_USER: ${DB__USER}
POSTGRES_PASSWORD: ${DB__PASSWORD}
POSTGRES_DB: ${DB__DB_NAME}
volumes:
- database:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${DB__USER}"]
interval: 5s
timeout: 5s
retries: 5
networks:
database:
aliases:
- ${DB__HOST:-postgres}
migrator:
build: {{ part_dir }}
image: {{ project_slug }}/backend
profiles: [migrate]
env_file:
- path: .env
required: false
{% if layout == 'part' %} - path: backend/.env
required: false
{% endif %} environment:
RUN_ENVIRONMENT: prod
volumes:
- ./{{ prefix }}src:/app/src
- ./{{ prefix }}migrations:/app/migrations
- ./{{ prefix }}alembic.ini:/app/alembic.ini
entrypoint: [alembic]
command: [upgrade, head]
depends_on:
postgres:
condition: service_healthy
networks:
database:
{% endif %}
{%- if use_redis %}
redis:
image: redis:7-alpine
profiles: [redis, external]
restart: unless-stopped
command: ["redis-server", "--appendonly", "yes"]
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 5s
retries: 5
volumes:
- redis_data:/data
networks:
redis:
aliases:
- ${REDIS__HOST:-redis}
{% endif %}
{%- if database != 'none' or use_redis %}
volumes:
{%- if database != 'none' %}
database:
{%- endif %}
{%- if use_redis %}
redis_data:
{%- endif %}
networks:
{%- if database != 'none' %}
database:
{%- endif %}
{%- if use_redis %}
redis:
{%- endif %}
{%- endif %}