حاويات تطبيقات متعددة لموقع Discourse واحد

You can host multiple standalone Discourse installs on a single server (separate containers / separate ports / separate app.yml), without using Discourse “multisite”.

It’s more manual than multisite, but it keeps instances isolated and makes it easier to migrate an individual site to its own server later.

One practical pattern is:

• external Postgres (single instance)
• external Redis (single instance)
• multiple Discourse web containers
• one Sidekiq node
• reverse proxy with health checks

This avoids multisite entirely while still allowing cost savings on low-traffic setups.



DISCOURSE MULTI-CONTAINER RUNBOOK
External Postgres + Redis + HAProxy + app1 / app2


  1. HOST PACKAGES
Step Command
Update system apt-get update
Install base tools apt-get install -y ca-certificates curl gnupg lsb-release
Install HAProxy + certbot + socat apt-get install -y haproxy certbot socat

  1. DOCKER NETWORK (REQUIRED)

A user-defined Docker network is required so containers can resolve each other by name.

Step Command
Create network docker network create discourse-net
Verify docker network ls | grep discourse-net

This allows:

• DISCOURSE_DB_HOST=pg
• DISCOURSE_REDIS_HOST=redis

to work correctly.


  1. SECRETS
Purpose Command
Postgres superuser export PG_SUPERPASS='REPLACE_ME_super_strong'
Discourse DB password export DISCOURSE_DBPASS='REPLACE_ME_discordb_strong'
Redis password export REDIS_PASS='REPLACE_ME_redis_strong'
Secret key base export SECRET_KEY_BASE="$(openssl rand -hex 64)"

  1. POSTGRES CONTAINER
Step Command
Create directory mkdir -p /var/discourse/external/postgres
Run container docker run -d --name pg --restart=always --network=discourse-net -e POSTGRES_PASSWORD="$PG_SUPERPASS" -v /var/discourse/external/postgres:/var/lib/postgresql/data postgres:15
Verify docker ps | grep pg

  1. CREATE DATABASE
Step Command
Create role docker exec -it pg psql -U postgres -c "CREATE ROLE discourse LOGIN PASSWORD '$DISCOURSE_DBPASS';"
Create DB docker exec -it pg psql -U postgres -c "CREATE DATABASE discourse OWNER discourse ENCODING 'UTF8' TEMPLATE template0;"
Text search docker exec -it pg psql -U postgres -d discourse -c "ALTER DATABASE discourse SET default_text_search_config = 'pg_catalog.english';"
Test login docker exec -it pg psql -U discourse -d discourse -c "select 1;"

  1. PGVECTOR EXTENSION

Required for modern Discourse versions.

Step Command
Install docker exec -it pg bash -lc 'apt-get update && apt-get install -y postgresql-15-pgvector && rm -rf /var/lib/apt/lists/*'
Create extension docker exec -it pg psql -U postgres -d discourse -c "CREATE EXTENSION IF NOT EXISTS vector;"
Verify docker exec -it pg psql -U postgres -d discourse -c "SELECT extname FROM pg_extension WHERE extname='vector';"

  1. REDIS CONTAINER
Step Command
Create directory mkdir -p /var/discourse/external/redis

Redis config template:

requirepass REPLACE_ME_REDIS
appendonly yes
save 900 1
save 300 10
save 60 10000
Step Command
Write config tee /var/discourse/external/redis/redis.conf >/dev/null <<EOF
Insert password sed -i "s/REPLACE_ME_REDIS/$REDIS_PASS/" /var/discourse/external/redis/redis.conf
Run redis docker run -d --name redis --restart=always --network=discourse-net -v /var/discourse/external/redis:/data -v /var/discourse/external/redis/redis.conf:/usr/local/etc/redis/redis.conf redis:7-alpine redis-server /usr/local/etc/redis/redis.conf
Test auth docker exec -it redis redis-cli -a "$REDIS_PASS" ping

  1. DISCOURSE DIRECTORY LAYOUT
Step Command
Create base dir mkdir -p /var/discourse
Enter cd /var/discourse
Clone repo git clone https://github.com/discourse/discourse_docker.git
Containers dir mkdir -p /var/discourse/containers
Shared logs mkdir -p /var/discourse/shared/web-only/log/var-log
Link containers ln -sfn /var/discourse/containers /var/discourse/discourse_docker/containers
Link launcher ln -sfn /var/discourse/discourse_docker/launcher /var/discourse/launcher

  1. APPLICATION CONTAINERS

app1.yml
• web + sidekiq
• port 8001

docker_args: "--network=discourse-net"
expose:
  - "8001:80"

app2.yml
• web only
• port 8002
• sidekiq disabled

docker_args: "--network=discourse-net"
expose:
  - "8002:80"

run:
  - exec: bash -lc 'mkdir -p /etc/service/sidekiq && touch /etc/service/sidekiq/down'

  1. BOOTSTRAP
Step Command
Enter cd /var/discourse/discourse_docker
Bootstrap app1 ./launcher bootstrap app1
Start app1 ./launcher start app1
Bootstrap app2 ./launcher bootstrap app2
Start app2 ./launcher start app2

  1. HEALTH CHECKS
Step Command
app1 curl -sSf http://127.0.0.1:8001/srv/status
app2 curl -sSf http://127.0.0.1:8002/srv/status
sidekiq app1 docker exec -it app1 pgrep -fa sidekiq
sidekiq app2 `docker exec -it app2 pgrep -fa sidekiq

  1. TLS CERTIFICATE
Step Command
Stop proxy systemctl stop haproxy
Issue cert certbot certonly --standalone -d example.com --agree-tos -m you@example.com --non-interactive
Start proxy systemctl start haproxy

  1. HAPROXY LOGIC
frontend fe_discourse
    bind :80
    bind :443 ssl crt /etc/letsencrypt/live/example.com/haproxy.pem

    http-request set-header X-Forwarded-Proto https if { ssl_fc }
    http-request set-header X-Forwarded-Proto http if !{ ssl_fc }

    redirect scheme https code 301 if !{ ssl_fc }

    use_backend be_discourse if { nbsrv(be_discourse) gt 0 }
    default_backend be_maint
backend be_discourse
    balance roundrobin
    option httpchk GET /srv/status
    server app1 127.0.0.1:8001 check
    server app2 127.0.0.1:8002 check
backend be_maint
    http-request return status 503 content-type text/html string "<h1>Maintenance</h1>"

  1. ZERO-DOWNTIME REBUILDS
Step Command
Disable app1 echo "disable server be_discourse/app1" | socat stdio /run/haproxy/admin.sock
Rebuild app1 ./launcher rebuild app1
Enable app1 echo "enable server be_discourse/app1" | socat stdio /run/haproxy/admin.sock
Step Command
Disable app2 echo "disable server be_discourse/app2" | socat stdio /run/haproxy/admin.sock
Rebuild app2 ./launcher rebuild app2
Enable app2 echo "enable server be_discourse/app2" | socat stdio /run/haproxy/admin.sock

END

Docker networking required
External Postgres and Redis
pgvector installed
Sidekiq isolated to app1
HAProxy health checks enabled
Maintenance fallback active
Rolling rebuilds supported

Migrating one site to its own server later

One advantage of running fully standalone Discourse installs (instead of multisite) is that migration is straightforward and low-risk.

Each Discourse instance already has:

• its own container
• its own uploads
• its own database
• its own Redis usage
• its own app.yml

No multisite disentangling is required.


High-level migration steps

  1. Provision a new VPS

Install Docker and Discourse normally on the new server.
Do not configure multisite.


  1. Create a full backup

From the source site:

Admin → Backups → Create Backup

Download the backup file.

This includes:

• database
• uploads
• users
• settings
• themes


  1. Restore on the new server

On the new server:

• complete initial setup
• log in as admin
• upload the backup
• restore

Discourse handles schema compatibility automatically.


  1. DNS cutover

Update the domain’s A record to point to the new server IP.

Once DNS propagates, users are transparently moved.


  1. Decommission old container

On the original server:

• stop the old container
• remove it when confident

Other Discourse installs on the same host are unaffected.


Why this is simpler than multisite

In multisite setups, migration often requires:

• separating databases
• extracting site-specific data
• adjusting multisite.yml
• reworking Sidekiq
• reconfiguring uploads and email

With standalone installs, none of that is necessary.

Each site is already independent.


Summary

This approach trades a little operational complexity early on
for very simple separation later.

It works particularly well during experimentation
or early-stage community building.


When this approach is probably not a good fit

This setup is usually not a good idea if:

• the sites expect moderate or high traffic early on
• you rely heavily on official Discourse support
• you are uncomfortable debugging Docker, networking, or reverse proxies
• uptime requirements are strict or business-critical
• multiple sites are tightly coupled operationally
• you expect frequent plugin experimentation across all instances

In these cases, either:

• a supported multisite setup
or
• one Discourse install per server

will usually result in fewer operational surprises.


Important note

This approach increases infrastructure flexibility,
but also increases responsibility on the administrator.

It works best when the person running it is comfortable owning the full stack
and treating occasional breakage as part of the learning process.

If stability and supportability are the primary goals,
a supported configuration is almost always the better choice.

إعجاب واحد (1)

ملاحظة إضافية واحدة ترتبط مباشرة بجزء HAProxy في الإعداد أعلاه.

هناك سلوك شائع مع HAProxy + Discourse حيث يؤدي إعادة بناء حاوية الويب (على سبيل المثال باستخدام ./launcher rebuild app1) إلى إرجاع استجابات 503 Service Unavailable لفترة وجيزة لأن HAProxy لا يزال يرسل حركة المرور إلى هذا الواجهة الخلفية أثناء إعادة تشغيلها. هذا ليس خطأ في Discourse بحد ذاته - يحدث لأن الواجهة الخلفية غير متاحة للحظة أثناء إعادة البناء.

الحل البديل الموصى به هو استخدام مقبس إدارة HAProxy لـ:
\t1. تعطيل الخادم في HAProxy قبل إعادة البناء، و
\t2. إعادة تمكينه بعد انتهاء إعادة البناء

هذا يمنع أخطاء 503 العابرة تلك.

هناك مناقشة موجودة في Meta توثق هذا السلوك وشرح الحل البديل:

إذا كان أي شخص هنا يستخدم HAProxy لإعادة البناء المتدحرج، فإن هذا الموضوع يوفر سياقًا مفيدًا لسبب تضمين أوامر مقبس الإدارة في دليل التشغيل.

أنا أفعل شيئًا مشابهًا، مع حاوية واحدة بأسلوب الويب فقط لكل موقع و Traefik (على الرغم من أنني قمت أيضًا بإعداد يستخدم nginx-proxy) كوكيل عكسي. لقد جربت HAproxy لفترة من الوقت (وهو ما تستخدمه CDCK، آخر مرة علمت بها)، ولكني وجدته مرهقًا.

أنا متأكد تمامًا من أنك بحاجة إلى Redis واحد لكل خادم Discourse.

أعتقد أن هناك اختلافًا بسيطًا في المصطلحات هنا.

عندما تقول “Redis واحد لكل خادم Discourse”، أتفق إذا كنا نعني بخادم موقع Discourse منطقي واحد.

في حالتي:

  • يتم استخدام HAProxy فقط للتبديل التلقائي / الواجهة الأمامية
  • لا يوجد تكوين متعدد المواقع (multisite)
  • يوجد موقع Discourse واحد فقط (اسم مضيف واحد، قاعدة بيانات Postgres واحدة)
  • هناك فقط حاويتا تطبيق (app containers) قادرتان على خدمة نفس الموقع

لذا، هذا أقرب إلى تخطيط متعدد العمال/الويب (multi-web / HA layout)، وليس تثبيتَي Discourse مستقلين.

في هذا الإعداد، يعد مشاركة Redis أمرًا متوقعًا ومطلوبًا - وإلا ستفقد:

  • الجلسات المشتركة
  • تسليم MessageBus
  • تحديد المعدل (rate limiting)
  • تنسيق المهام في الخلفية (background job coordination)

هذا هو نفس نمط تشغيل حاويات web_only متعددة أو توسيع نطاق عمال الويب أفقيًا:
حاويات تطبيق متعددة ← قاعدة بيانات Postgres واحدة + Redis واحد.

حيث يجب ألا تتم مشاركة Redis عندما يكون هناك موقعان منفصلان لـ Discourse (أسماء مضيفات/قواعد بيانات مختلفة). في هذه الحالة، يحتاج كل موقع إلى قاعدة بيانات Redis خاصة به (أو مثيل) لتجنب تضارب المفاتيح.

لذا، أعتقد أننا متفقون من الناحية المفاهيمية - الأمر يتعلق فقط بـ:

  • :white_check_mark: Redis واحد لكل موقع Discourse
  • :cross_mark: Redis واحد لكل حاوية تطبيق فردية

يسعدني التوضيح أكثر إذا كنت قد أسأت فهم أي شيء - أردت فقط شرح الطوبولوجيا بمزيد من الوضوح.

أوه. هذا هو عكس ما اعتقدت أننا نتحدث عنه. العنوان هو “خادم واحد لمجتمعي ديسكورس” أنت تتحدث عن “خادمين لمجتمع ديسكورس واحد”

أنت على حق - لقد خلطت بين طوبولوجيتين مختلفتين، وعنوان الموضوع هو الدليل.

هذا الموضوع يدور حول “خادم واحد لمجتمعين” (موقعان مستقلان).
تعليقي السابق حول “Redis خارجي (مثيل واحد)” كان يصف نمطًا مختلفًا: “حاويتا تطبيق لمجتمع واحد” (التوافر العالي / تعدد الويب لموقع واحد).

لذا، لإعادة التأكيد بوضوح:

أ) موقعا Discourse مستقلان على خادم واحد (ما يسأل عنه صاحب الموضوع الأصلي)

  • تعامل معهما على أنهما تثبيتان منفصلان
  • يجب أن يكون لديهما قواعد بيانات Postgres منفصلة ومثيلات Redis منفصلة (أو عزل كافٍ لـ MessageBus Pub/Sub، وهو المأزق الذي اقتبسته)

ب) موقع Discourse واحد مع حاويات ويب/تطبيق متعددة (ما كنت أصفه)

  • يجب أن تشترك في قاعدة بيانات Postgres نفسها و Redis نفسه لذلك الموقع
    (الجلسات، تحديد المعدل، MessageBus، إلخ.)

إذًا: :white_check_mark: تحذيرك بشأن Redis ينطبق على أ (مجتمعان / موقعان).
ملاحظتي حول “Redis مشترك” تنطبق فقط على ب (مجتمع واحد موسع عبر حاويات متعددة).

شكرًا لك على التصحيح - سأبقي الحالتين منفصلتين بشكل صريح في أي أدلة تشغيل/منشورات متابعة.

إعجاب واحد (1)