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 个赞

您好,

未创建管理员帐户

当通过 d/ 中的脚本从外部使用 docker 容器时(例如,按照 Install Discourse for development using Docker 中的说明运行 d/boot_dev --init),它会要求我在过程中设置一个管理员帐户。

但是,当将其用作开发容器并运行构建步骤(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

..请确保 \u003ckbd\u003e在卷中克隆\u003c/kbd\u003e

5 个赞

也许有更好的方法,但在插件上工作时,我有一个与我的主 discourse 存储库文件夹并排的 discourse-plugins 文件夹。这会挂载到 /workspace/plugins,因此我可以在容器内创建符号链接。

这是我添加到 devcontainer.json 中 mounts 的内容:
\"source=${localWorkspaceFolder}/../${localWorkspaceFolderBasename}-plugins,target=/workspace/plugins,type=bind\"

2 个赞

这确实有用,感谢。

1 个赞

… 或者只需 git reset --hard
对我有效
然后 Dev Container: Rebuild ContainerCtrl-Shift-B

如果您正在本地 macOS 环境中使用 OrbStack(非官方),并且希望使用自定义域名以 HTTPS 运行 Discourse,请在您的 devcontainer.json 中进行以下更新:

  1. 为容器命名。
  2. RAILS_DEVELOPMENT_HOSTS 环境变量中添加 .orb.local 通配符域名(主机名必须用逗号分隔)。
--- 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 文件中缺少一个记录。添加此行后,我便能够使用步骤 2 中的 .orb.local 通配符域名。

通过对 devcontainer.json 文件进行这些更改,我现在可以在 https://discourse.orb.local/ 上运行我的本地 Discourse 实例。

/etc/hosts

如果您的 /etc/hosts 文件中还没有,请添加此行。

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

奖励提示 1
如果您的网络设置或公司 VPN 网络等与 OrbStack 的容器 IP 地址范围发生冲突,请在 OrbStack 中更新为不同的范围。

奖励提示 2
如果省略步骤 1,OrbStack 将创建一个随机命名的容器,但您仍然可以使用 HTTPS,而无需附加任何端口号。缺点是容器名称(因此域名)会在每次重建容器时刷新。