Developing Discourse using a Dev Container

Dev Containers is an open standard for configuring a development environment inside a container. This almost entirely eliminates the need to install/configure Discourse-specific tools/dependencies on your local machine, and makes it very easy to keep up-to-date as Discourse evolves over time.

Dev Containers can be used in a number of different IDEs, or directly using their reference CLI. This guide will describe the setup process for VSCode.

Getting started

  1. Download and install VSCode

  2. Install the Dev Containers extension in VSCode

  3. Clone the Discourse repository onto your machine

    git clone https://github.com/discourse/discourse
    
  4. In VSCode, use FileOpen Folder, then choose the Discourse directory

  5. Open the folder in its Dev Container. This can be done via the popup prompt, or by opening the command palette (Cmd/Ctrl + Shift + P) and searching for “Open folder in container…”

  6. If this is your first time launching a container, you will be prompted to install and start Docker Desktop. Once complete, go back to VSCode re-run “Open folder in container…”

  7. Wait for the container to download and start. When it’s done, the README will appear, and you’ll see the Discourse filesystem in the sidebar.

  8. Run the default build task using Ctrl + Shift + B (Cmd + Shift + B on mac).

    This will install dependencies, migrate the database, and start the server. It’ll take a few minutes, especially on the lower-end machines. You’ll see “Build successful” in the terminal when it’s done.

  9. Visit http://localhost:4200 in your browser to see your new Discourse instance

  10. All done! You can now make changes to Discourse’s source code and see them reflected in the preview.

Applying config/container updates

Every so often, the devcontainer config and the associated container image will be updated. VSCode should prompt you to “rebuild” to apply the changes. Alternatively, you can run “Dev Containers: Rebuild Container” from the VSCode command palette. The working directory, and your Redis/Postgres data will be preserved across rebuilds.

If you’d like to start from scratch with fresh database, you’ll need to delete the discourse-pg and discourse-redis docker volumes. This can be done from the “Remote Explorer” tab of the VSCode sidebar.

Discourse’s sample vscode .vscode/settings.json and .vscode/tasks.json will be copied when you first boot the codespace. From that point forward, if you want to use the latest sample config, you’ll need to manually copy .vscode/settings.json.sample to .vscode/settings.json.

References


This document is version controlled - suggest changes on github.

13 إعجابًا

مرحباً،

لم يتم إنشاء حساب مسؤول

عند استخدام حاوية Docker من الخارج عبر البرامج النصية في d/ (على سبيل المثال، d/boot_dev --init كما هو محدد في https://meta.discourse.org/t/install-discourse-for-development-using-docker/102009، يطلب مني إعداد حساب مسؤول كجزء من العملية.

ومع ذلك، عند استخدامه كحاوية تطوير وتشغيل خطوات البناء (Ctrl/Cmd + Shift + B)، فإنه لا ينشئ مسؤولاً.

من خلال إلقاء نظرة سريعة على التعليمات، كان لدي انطباع في البداية أن إنشاء مسؤول أمر شاق للغاية؛ ولكن بعد ذلك أدركت أن كل ما يتطلبه الأمر هو هذا الأمر، تاركاً إياه هنا لمن يواجهون نفس المشكلة:

rake admin:create

(أو، إذا اشتكى من إصدار rake مختلف مطلوب: bundle exec rake admin:create)

6 إعجابات

على نظام التشغيل Windows 11، إذا كنت لا ترغب في مواجهة مشكلات في نهايات الأسطر، مثل:

[23963 ms] Start: Run in container: /bin/sh -c ./.devcontainer/scripts/start.rb
/usr/bin/env: ‘ruby\r’: No such file or directory
/usr/bin/env: use -[v]S to pass options in shebang lines

.. تأكد من الاستنساخ في وحدة التخزين

5 إعجابات

ربما تكون هناك طريقة أفضل، ولكن للعمل على المكونات الإضافية لدي مجلد أشقاء discourse-plugins بجوار مجلد مستودع discourse الرئيسي الخاص بي. هذا يتم تحميله إلى /workspace/plugins حتى أتمكن بعد ذلك من إنشاء روابط رمزية داخل الحاوية.

هذا ما أضفته إلى mounts في devcontainer.json:
"source=${localWorkspaceFolder}/../${localWorkspaceFolderBasename}-plugins,target=/workspace/plugins,type=bind"

إعجابَين (2)

هذا مفيد حقًا ، شكرًا لك.

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

… أو فقط git reset --hard
لقد نجحت معي
ثم Dev Container: Rebuild Container و Ctrl-Shift-B

إذا كنت تستخدم OrbStack (غير تابع) على بيئة macOS المحلية الخاصة بك، وترغب في تشغيل Discourse باستخدام HTTPS مع نطاق مخصص، فقم بتحديث ملف devcontainer.json الخاص بك بالإضافات التالية:

  1. امنح اسمًا للحاوية.
  2. أضف النطاق الفرعي .orb.local إلى متغير البيئة RAILS_DEVELOPMENT_HOSTS (يجب فصل أسماء المضيفين بفاصلة).
--- a/.devcontainer/devcontainer.json
+++ b/.devcontainer/devcontainer.json
@@ -13,10 +13,11 @@
   ],
   "remoteUser": "discourse",
   "remoteEnv": {
-    "RAILS_DEVELOPMENT_HOSTS": ".app.github.dev",
+    "RAILS_DEVELOPMENT_HOSTS": ".app.github.dev,.orb.local", // الخطوة 2
     "PGUSER": "discourse",
     "SELENIUM_FORWARD_DEVTOOLS_TO_PORT": "9229",
   },
+  "runArgs": ["--name","discourse"], // الخطوة 1
   "mounts": [
     "source=${localWorkspaceFolderBasename}-node_modules,target=${containerWorkspaceFolder}/node_modules,type=volume",
     "source=${localWorkspaceFolderBasename}-pg,target=/shared/postgres_data,type=volume",

ملاحظة: يرجى إخباري إذا كنت تعرف كيف يمكنني تعيين اسم المضيف *.orb.local واسم الحاوية ديناميكيًا، كما هو محدد لـ GitHub Codespaces. لم ينجح معي تعيين القيمة كـ .app.github.dev,.orb.local.

تحديث: بطريقة ما، كنت أفتقد سجلًا في ملف /etc/hosts الخاص بي. بعد إضافة هذا السطر، تمكنت من استخدام النطاق الفرعي .orb.local في الخطوة 2.

مع هذه التغييرات في ملف devcontainer.json، يمكنني الآن تشغيل نسخة Discourse المحلية الخاصة بي على https://discourse.orb.local/

/etc/hosts

أضف هذا السطر إلى ملف /etc/hosts الخاص بك إذا لم يكن موجودًا بالفعل.

##
# Docker و OrbStack
##
127.0.0.1 host.docker.internal

نصيحة إضافية 1
إذا كانت إعدادات شبكتك، أو شبكة VPN الخاصة بشركتك، وما إلى ذلك تتعارض مع نطاقات IP للحاويات الخاصة بـ OrbStack، فقم بتحديث OrbStack الخاص بك بنطاق مختلف.

نصيحة إضافية 2
إذا حذفت الخطوة 1، فسيقوم OrbStack بإنشاء حاوية باسم عشوائي، ولكن لا يزال بإمكانك استخدام HTTPS دون إضافة أي رقم منفذ. العيب هو اسم الحاوية، وبالتالي سيتم تحديث اسم النطاق في كل مرة تقوم فيها بإعادة بناء الحاوية.