Mathchast CMS / Build Series / 11

Docker & Production Deploy

Docker/Compose, сети, env/secrets, PostgreSQL, Payload/Next.js, Meilisearch, Gorse, healthchecks, migrations, backups, restore и безопасный release workflow.

1. Цель

Система должна разворачиваться воспроизводимо из репозитория без ручных правок production-контейнеров.

Не делать: ручную настройку базы/контейнеров, которую нельзя повторить из git + documented secrets.

2. Production topology

Internet
   ↓
Reverse Proxy / HTTPS
   ↓
Mathchast web
Payload CMS + Next.js
   │
   ├── PostgreSQL
   ├── Meilisearch
   ├── Gorse
   └── media storage

Worker/jobs
   ↓
Payload / PostgreSQL

3. Compose services

ServiceRolePublic?
webNext.js + Payloadчерез reverse proxy
postgressource of truthNO
meilisearchsearch indexNO
gorse-masterrecommendation coordinatorNO
gorse-serverrecommendation servingNO
gorse-workerrecommendation workerNO
backupscheduled backupNO

4. Networks

networks:
  edge:
    external: true
  app_internal:
    internal: true

5. Volumes

6. Environment

NODE_ENV=production
DATABASE_URL=postgresql://...
PAYLOAD_SECRET=...
NEXT_PUBLIC_SERVER_URL=https://...
MEILI_HOST=http://meilisearch:7700
MEILI_MASTER_KEY=...
GORSE_API_URL=http://gorse-server:8087
GORSE_API_KEY=...
D30_SERVICE_TOKEN=...
Production secrets не коммитить в git.

7. Dockerfile

multi-stage:
1. deps
2. build
3. runner

runner:
- production dependencies only
- non-root user
- build artifacts
- no source secrets

8. Compose skeleton

services:
  web:
    build: .
    restart: unless-stopped
    env_file: [.env.production]
    depends_on:
      postgres:
        condition: service_healthy
      meilisearch:
        condition: service_healthy

  postgres:
    image: postgres:<pinned-version>
    restart: unless-stopped
    volumes:
      - postgres_data:/var/lib/postgresql/data

  meilisearch:
    image: getmeili/meilisearch:<pinned-version>
    restart: unless-stopped
    volumes:
      - meili_data:/meili_data
Не использовать плавающий latest в production.

9. Healthchecks

ServiceCheck
webHTTP /api/health
postgrespg_isready
meilisearchhealth endpoint
gorseservice health endpoint

10. Health endpoint

{
  app: "ok",
  database: "ok",
  search: "ok|degraded",
  recommendations: "ok|degraded"
}

Search/Gorse могут быть degraded, но core определяется доступностью Payload/PostgreSQL.

11. Migrations

backup current DB
↓
run migrations
↓
start new web version
↓
health checks
↓
traffic
Не запускать migrations одновременно из нескольких replica.

12. Scripts

package.json:
  migrate
  migrate:status
  seed
  reindex
  backup
  restore:test

13. Backup

pg_dump -Fc DATABASE > backup.dump

14. Restore drill

new empty PostgreSQL
↓
restore backup
↓
run required migrations
↓
start app
↓
verify counts
↓
sample public pages

15. Media backup

16. Search/Gorse recovery

Meilisearch должен полностью rebuild из Payload.

17. Reverse proxy

18. Security

19. Monitoring

20. Release workflow

git commit/tag
↓
build image
↓
test
↓
backup
↓
migrate
↓
deploy
↓
healthcheck
↓
smoke tests

21. Rollback

22. Worker strategy

Если jobs мешают web traffic, вынести worker process/container. Иначе отдельный worker не обязателен.

Не вводить Kubernetes только ради теоретического zero-downtime.

23. Production directories

/opt/mathchast/
├── compose.yml
├── .env.production
├── backups/
└── scripts/
    ├── backup.sh
    ├── restore-test.sh
    └── deploy.sh

24. Disaster recovery priority

  1. PostgreSQL.
  2. Media.
  3. Secrets/config.
  4. Application image/repo.
  5. Meili rebuild.
  6. Gorse rebuild/resync.

25. Deploy acceptance

#ПроверкаPASS
DP01Fresh Compose startservices healthy
DP02DB migrationssuccess
DP03Public home200
DP04Payload Adminauth works
DP05Searchworks/degraded
DP06Gorse unavailablesite works
DP07Restart webdata preserved
DP08Restart postgresvolume preserved
DP09Backupvalid dump
DP10Restore testcontent recovered

26. Что нельзя делать

  • Не использовать floating latest images.
  • Не публиковать DB/Meili/Gorse ports наружу без причины.
  • Не хранить secrets в git.
  • Не запускать seed при каждом production start.
  • Не считать backup успешным без restore test.
  • Не хранить единственную backup-копию на том же диске.
  • Не делать manual schema edits вне migrations.

27. ТЗ ИИ-кодеру

  1. Создать production Dockerfile.
  2. Создать compose.yml.
  3. Создать .env.example.
  4. Настроить networks/volumes.
  5. Добавить healthchecks.
  6. Добавить migration/deploy scripts.
  7. Добавить backup/restore scripts.
  8. Добавить reindex command.
  9. Документировать Gorse recovery.
  10. Прогнать deploy/security/restore tests.

28. Deliverables

1. Dockerfile
2. compose.yml
3. .env.example
4. health endpoint
5. healthchecks
6. migration script
7. deploy script
8. backup script
9. restore-test script
10. reindex command
11. reverse-proxy config/example
12. deploy test report
13. restore test report
14. security report
15. deviations.md
16. commit hash

29. Gate Build 11

PASS если:
FAIL → не переходить к Build 12.

30. Следующий документ

После PASS: build_12_coder_master_instruction.html — единая последовательная инструкция ИИ-кодеру, связывающая Build 01–11 в один контролируемый процесс сборки.
Mathchast CMS · build_11_docker_deploy.html · 05.10.2026