使用 Docker 为开发安装 Discourse

使用 Docker 进行开发

由于 Discourse 在 Docker 中运行,您应该能够使用 Discourse 开发容器直接从源目录运行 Discourse。

:white_check_mark: 优点:无需安装任何系统依赖项,无需任何配置即可快速设置开发环境。

:x: 缺点:比 Ubuntu 上的原生开发环境稍慢,比 MacOS 上的原生安装慢得多。

另请参阅 Developing Discourse using a Dev Container

步骤 1:安装 Docker

Ubuntu

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install -y docker-ce

19.10

sudo apt-get install docker.io
sudo usermod -a -G docker $USER
sudo reboot

Windows WSL: Ubuntu

您可以在 WSL 中运行上述命令,但必须将所有文件放在 WSL 文件系统中。例如,如果它在 ~/discourse 内部,它可以正常工作,但如果它位于 /mnt/c/discourse 内部,它会失败。

MacOS

:warning: Discourse 开发 Docker 镜像仅适用于 x86_64 架构。M1 Mac 能够使用架构模拟启动该镜像,但由于 QEMU 中缺少 inotify 支持,Discourse 可能无法启动。

您应该改用 Install Discourse on macOS for development

选项 1:Docker 商店 下载打包的 .dmg
选项 2: brew install docker

步骤 2:启动容器

将 Discourse 仓库克隆到您的本地设备。

git clone https://github.com/discourse/discourse.git
cd discourse

(从您的源根目录)

d/boot_dev --init
    # 等待:
    #   - 依赖项已安装,
    #   - 数据库已迁移,以及
    #   - 已创建一个管理员用户(您需要与之交互)

# 在一个终端中:
d/rails s

# 并在一个单独的终端中
d/ember-cli

…然后在 http://localhost:4200 上打开浏览器,瞧!您应该能看到 Discourse。

插件符号链接

Docker 开发流程支持 plugins/ 目录下的符号链接,但有以下注意事项:

每当创建新的插件符号链接时,必须使用以下命令重启 Docker 容器:

d/shutdown_dev; d/boot_dev

注释:

  • 要测试电子邮件,请运行 MailHog:

    d/mailhog
    
  • 如果缺少 gem,请运行:

    d/bundle install
    
  • 如果需要数据库迁移:

    d/rake db:migrate RAILS_ENV=development
    
  • 完成后,您可以选择终止 Docker 容器:

    d/shutdown_dev
    
  • 数据在容器的调用之间保留在源目录 tmp/postgres 目录中。如果出于任何原因想要重置数据库,请运行:

    sudo rm -fr data
    
  • 如果看到类似“尝试连接到 Docker 时被拒绝权限”的错误,请运行:

    run `sudo usermod -aG docker ${USER}`
    sudo service docker restart
    
  • 如果希望全局向网络公开来自容器的端口(默认关闭),请使用:

    d/boot_dev -p
    
  • Dockerfile 来自 GitHub 上的 discourse/discourse_docker,特别是 image/discourse_dev

运行测试

d/rake autospec

要运行特定插件的测试,您也可以这样做:

d/rake plugin:spec["discourse-follow"]

或者甚至像这样更具体:

my-machine:~/discourse$ d/shell
discourse@discourse:/src$ RAILS_ENV=test /src/bin/rspec plugins/discourse-follow/spec/lib/updater_spec.rb:37

本文档已版本控制 - 请在 github 上建议更改。

72 个赞
Using a launcher built docker image in docker-compose
IDE and comfortable DEV environment for newbies
Docker install on MacOS
How to install Discourse locally?
Installing Discourse for macOS Development Using asdf and docker-compose
Discourse developement environment setup
Discourse on local pc
External database ENV VARs not documented (external PG Port, external Redis ENV VARs)
How to Install Discourse on LocalHost in Windows?
Unicode (Russian) username validation fails randomly
Qunit in Docker
Is it possible to use a local plugin directory with a docker install?
Is it possible to use a local plugin directory with a docker install?
Problem with development on Windows with Docker and mounted volume
[Resolved] Recent change breaks connectivity in Docker (+ non-Docker)
How might we better structure #howto?
Apple M1 for discourse development
How to open via internal IP?
Fast, efficient plugin development with Docker dev install (Windows WSL2)
How do I install a stable version of Discourse on Docker locally?
Backup fails on localhost with docker
How do I install a stable version of Discourse on Docker locally?
Problems with understanding the development of Discourse
Need to create custom static page as home page
Docker new licensing
Install Discourse Docker on Ubuntu 11
Backup restore failing on clean dev docker env: FATAL: Peer authentication failed for user “postgres”
Install Discourse on macOS for development
Can't build Discourse on Ubuntu 20.10
Installing discourse for development is failing
Create, download, and restore a backup of your Discourse database
Set up a local Discourse Development Environment?
How do you install Discourse using Windows 10 Docker Desktop?
NoMethodError: undefined method `min_tags_from_required_group'
Install plugins in your non-Docker development environment
NoMethodError: undefined method `min_tags_from_required_group'
Error installing `bullseye-backports` when trying to install for Development
What I should do for migration from Cusdis to Discourse
Migrate a MyBB forum to Discourse
Local installation internal use only
Maintaining an up-to-date development environment
PG::UndefinedTable: ERROR: relation "pg_range" does not exist
Docker-compose.yml to run discourse locally
Allow subscription of predetermined length
Cannot load any images “Uncaught (in promise) no-response: no-response”
Discovery document is missing
What are the benefits of a dev environment?
Installing Discourse on windows using Docker Desktop
'Arrive at topic' info missing from email when re-sending invites
External database ENV VARs not documented (external PG Port, external Redis ENV VARs)
How to troubleshoot database errors in an outdated plugin?
Error when building: discourse does not support compiling scss/sass files via sprockets
Integration into custom auth system where emails are not unique?
Installer un environnement developpement discourse sur windows
Contributing to Discourse development
Install Discourse on Ubuntu or Debian for Development
There was an error while trying to write to `/src/Gemfile.lock`. It is likely that you need to grant write permissions for that path
Tags rearranged, triggering an extra edit
Ember addons are resolving as incorrect peer dependencies. -- fixed by removing "content-tag@3.1.0": "patches/content-tag@3.1.0.patch",
Trying to install Discourse on portainer but it's not pulling the base container
Developing Discourse using a Dev Container
Subscribe to post an ad
Postgres doesn't seem to be running when running Discourse locally using Docker
Discourse OpenID Connect (OIDC)
How can I install Discourse on Windows? Getting restarting (100) error
Dev environment: recommended way to bootstrap the first admin account without email?
Dev environment: recommended way to bootstrap the first admin account without email?
Total newbie struggling to preview the signup modal with my test theme, what am I missing?
Configure Discourse to use a separate PostgreSQL server
Installing discourse follow plugin in development (boot_dev) setup — no app.yml file
Discourse API developers docker-compose basic image
Which is the better option to self-host?
Using a launcher built docker image in docker-compose
Without any coding knowledge, is it possible to install and maintain Discourse?
I want to install a plugin for development, what should I do?
Can't uninstall on Ubuntu 18.04
Developing Discourse using a Dev Container
Using a launcher built docker image in docker-compose
Bundle install fails after adding gem "mysql2" and gem "reverse_markdown"
Issue in setup discourse on ubuntu server
`script/osx_dev` script is pretty broken
Can't set up dev environment due to cppjieba_rb failing to install
I want to install a plugin for development, what should I do?
Stuck in infinite loop in Mac terminal after source ~/.bashrc
Install on macOS – Failure: Scheduler::Defer can pause and resume
Install on macOS – Failure: Scheduler::Defer can pause and resume
Install Discourse for Development using Docker is Failed
OAuth2 and Microsoft ADFS
Migrate a phpBB3 forum to Discourse
Availability of OpenID Connect in hosted plans
Can't set up dev environment due to cppjieba_rb failing to install
Adding command line tools support for user api keys
Adding command line tools support for user api keys
Discourse redirects to the OAuth server after loading `/?authComplete=true`
Hello from Gitpod! (installing on google cloud + automated dev setup)

设置非常简单!谢谢。

不过,我对本地安装的速度之慢感到惊讶。有没有办法调整容器规格?我预料到会有些变慢,但没想到会如此明显……

再次感谢。

7 个赞

我尝试在 Windows 10 上使用 Docker,并对该指南有一个疑问。我已经安装了 Docker,并且可以运行一个 Ubuntu 容器(容器内基本未安装任何内容)。该 Docker 指南只是说接下来要克隆仓库,但由于容器内未安装 git,我无法执行此操作。

此时,我是应该直接遵循 Ubuntu 指南 并在容器内安装所有前置条件,还是应该使用 Github 上引用的 discourse_dev 镜像?看起来该指南跳过了很多步骤,但我之前从未使用过 Docker,也许是我遗漏了什么。

4 个赞

我认为让你困惑的是,你试图在容器内克隆仓库,但 git clone 应该在本地机器上执行。所以,如果你本地已经安装了 git,那么你就快成功了。

3 个赞

我实际上首先做了那件事,这很好。所以我本地机器上已经有了一份仓库的副本。

我是否可以从 Docker 容器中自动访问我的主机文件系统?我觉得我部分被 d/boot_dev --init 搞糊涂了,因为我不知道 d/ 部分是什么意思(例如,我在仓库中看不到名为 d 的文件夹,尽管我确实看到了 boot_dev)。

3 个赞

d 位于仓库中(向下滚动查看)——它不是一个文件夹,而是指向 bin/docker 的符号链接。如果你执行 git clone,应该能获取到它。

我自己没有 extensively 使用过这个设置,但据我理解,你直接在主机文件系统(你刚刚检出)中编辑源文件,Docker 容器会挂载该目录并从中运行。d/* 脚本将为你完成大部分 Docker 设置工作。

6 个赞

谢谢。我甚至没想到要查看文件夹之后的内容。我想我现在有足够的信息再尝试一次了。

3 个赞

我仍然遇到了一些问题。在这个主题、这里这里 之间,没有任何地方明确说明要运行哪个 docker 命令。我最终选择了 docker run -ti -v LocalRepoPath:/src discourse/discourse_dev,它初始化了大量内容,并似乎将仓库挂载到了 /src。但出于某种原因,我无法运行 d/boot_dev --init。即使只是在仓库的顶层目录中运行 ls,也会提示:

ls: 无法访问 'adminjs': 输入/输出错误
ls: 无法访问 'd': 输入/输出错误
ls: 无法访问 'jsapp': 输入/输出错误

有人知道这是怎么回事吗?

3 个赞

你可能把这个问题想复杂了:首帖中的教程已经包含了启动所需的全部内容。

d/* 命令会自动执行所有 Docker 操作,无需你手动处理。克隆仓库后,只需在主机上运行 d/boot_dev --initd/unicorn 命令,即可让 http://localhost:9292 正常运行。这些脚本会自动调用 Docker 生成并启动容器。使用 d/shutdown_dev 命令即可停止容器。

如果你在浏览主机上的仓库文件时遇到这些错误,那可能是文件系统出现了其他问题。是否磁盘空间不足?

4 个赞

叹气,我明白了。我原以为那些命令必须在 Docker 中运行。话虽如此,d/boot_dev --init 似乎在 Windows 的 PowerShell 中无法运行(我想这也是为什么这份 Docker 指南只提及 Mac 和 Ubuntu 宿主机的原因)。我尝试在我的 Linux 子系统(v1)中运行该命令,但当然在 Docker 步骤失败了,因为我在 Windows 上安装了 Docker,而不是在 Linux 子系统中安装的。


接下来,我尝试在 Windows 的 Git Bash 中运行。我无法运行 d/boot_dev --init(错误提示“d/boot_dev: Is a directory”),但我能够运行 bin/docker/boot_dev --init。这一步进展了很多,执行了一系列拉取操作,但随后失败了:

C:\Program Files\Docker\Docker\resources\bin\docker.exe: Error response from daemon: Mount denied:
The source path “PathToRepo/data/postgres;C”
doesn’t exist and is not known to Docker.

我认为我通过转义挂载路径解决了这个问题,参考:
https://stackoverflow.com/questions/50608301/docker-mounted-volume-adds-c-to-end-of-windows-path-when-translating-from-linux

之后,我遇到了这个错误:

C:\Program Files\Docker\Docker\resources\bin\docker.exe: Error response from daemon: OCI runtime create failed: container_linux.go:346: starting container process caused “exec: "C:/Program Files/Git/sbin/boot": stat C:/Program Files/Git/sbin/boot: no such file or directory”: unknown.

C:/Program Files/Git/sbin/boot 在我的文件系统中并不存在,所以这个错误并不令人意外。在 boot_dev 文件中,唯一提到 sbin/boot 的地方是这条 Docker 命令的部分内容:discourse/discourse_dev:release /sbin/boot

目前我就卡在这里了……我真的不知道该尝试什么了。

3 个赞

很抱歉,由于我不熟悉 Windows,我只能提供到这里的帮助。

也许 @rishabh 能够确认这是否也适用于 Windows,并补充缺失的步骤(或者如果不适用的话,添加一条说明)。

3 个赞

@sam 我尝试的方法是否真的是运行 Discourse 进行开发的某种勉强可行的方式?在 Windows 开发线程 中,你提到过尝试过这种方法,但到目前为止进展并不顺利。基本的(非 Discourse 相关的)Ubuntu 容器在技术上可以运行,但该线程楼主提到的特定命令却无法使用。

你最初曾对我说过:

因此,除非你有建议能帮我解决目前在 Windows 上使用 Docker 遇到的问题,否则我最好及时止损,改用 VirtualBox 之类的工具在 Ubuntu 虚拟机中运行 Discourse。

2 个赞

我们中许多人都使用慢速环,没有任何问题。游戏能运行,Steam 能运行,一切都能正常运行。

我理解这里的担忧,但这真的不是什么大问题。无论如何,如果这让你感到过于不安,我建议使用传统的虚拟机,如 Hyper-V 或 VirtualBox。在其中安装 Docker,然后使用我们的 Docker 开发环境。

6 个赞

如果这不是我的主要(也是唯一)的电脑,我本会加入你们的慢速更新环,但我实在无法冒这个险。因此,我将采用传统的虚拟机搭配 Docker 的方案。再次感谢你的建议。希望很快就能搭建好并运行起来。

那么,为虚拟机分配多少磁盘空间和内存才够用呢?我原本打算分配 16GB 磁盘空间和 4GB 内存,但想先确认一下。

1 个赞

这应该就足够了,找到理想食谱的最佳方法就是多尝试。

6 个赞

好的,我快要真正让这个项目运行起来了。我在 Ubuntu 虚拟机中通过 Docker 运行了一个本地实例。我假设 d/boot_dev --init 应该只在第一次运行时执行,之后只需运行 d/boot_dev。这样理解对吗?我之所以这么问,是因为当我在 d/unicorn 中包含 --init 时它能正常工作,但如果在 d/boot_dev 中省略 --init 就无法运行。启动失败时,我收到以下错误:

无法在任何源中找到 aws-partitions-1.270.0。
请运行 bundle install 以安装缺失的 gems。

但随后运行 bundle install 又失败了,报错如下:

actionview_precompiler-0.2.2 要求 Ruby 版本 >= 2.6,这与当前版本 ruby 2.5.1p57 不兼容。

我是否需要升级到特定版本的 Ruby?还是说还有其他问题?


我的虚拟机是 Ubuntu 18.04 (LTS),这次我能够直接按照主贴中的说明操作,所以我不确定自己可能在哪里出错了(如果确实有问题的话)。

2 个赞

你不应该使用 bundle install,而应该使用 d/bundle install。你希望在容器内部安装 bundle,虚拟机本身不需要 Ruby。

4 个赞

是的,我早就猜到了,只是不知道在容器中运行它的正确方法。再次感谢你的帮助!

2 个赞

我拉取了仓库的最新版本,和往常一样,d/unicorn 提示我“运行 bundle install 以安装缺失的 gems”。但这次当我运行 d/bundle install 时,出现以下错误:

正在从 https://rubygems.org/ 获取源索引

因错误重试获取器(第 2/4 次):Bundler::HTTPError 无法从 https://rubygems.org/ 获取规范

因错误重试获取器(第 3/4 次):Bundler::HTTPError 无法从 https://rubygems.org/ 获取规范

因错误重试获取器(第 4/4 次):Bundler::HTTPError 无法从 https://rubygems.org/ 获取规范

无法从 https://rubygems.org/ 获取规范

因此,我目前无法运行 Discourse。我不确定这是否与拉取操作(直接或间接)有关,还是完全无关的其他问题。在 Google 上搜索该错误并未提供太多帮助,所以我不知道该如何继续。

1 个赞

您能访问 rubygems.org 吗?

1 个赞