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/ のスクリプト(例: Install Discourse for development using Docker に記載されている d/boot_dev --init)を使用して外部から Docker コンテナを使用すると、プロセスの一部として管理者アカウントの設定を求められます。

しかし、Dev Container として使用し、ビルドステップ(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.jsonmounts に追加したものです。
"source=${localWorkspaceFolder}/../${localWorkspaceFolderBasename}-plugins,target=/workspace/plugins,type=bind"

「いいね!」 2

これは本当に役に立ちます、ありがとうございます。

「いいね!」 1

または、単に git reset --hard\nこれでうまくいきました\n次に Dev Container: Rebuild ContainerCtrl-Shift-B

macOS ローカル環境で OrbStack (非公式) を使用しており、カスタムドメインで Discourse を HTTPS で実行したい場合は、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",

追伸: GitHub Codespaces のように、*.orb.local ホスト名とコンテナ名を動的に設定する方法を知っていたら教えてください。.app.github.dev,.orb.local のように値を設定しても機能しませんでした。

更新: なぜか /etc/hosts ファイルにレコードが欠落していました。この行を追加した後、ステップ 2 で .orb.local ワイルドカードドメインを使用できるようになりました。

これらの変更を devcontainer.json ファイルに加えることで、ローカルの Discourse インスタンスを https://discourse.orb.local/ で実行できるようになります。

/etc/hosts

まだ /etc/hosts ファイルにこの行を追加していない場合は、追加してください。

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

ボーナスのヒント 1
ネットワーク設定や会社の VPN ネットワークなどが OrbStack のコンテナ IP 範囲と競合する場合は、OrbStack を別の範囲に更新してください。

ボーナスのヒント 2
ステップ 1 を省略すると、OrbStack はランダムな名前のコンテナを作成しますが、ポート番号を付けずに HTTPS を使用できます。欠点は、コンテナ名、したがってドメイン名がコンテナを再構築するたびに更新されることです。