# 使用 Docker 为开发环境安装 Discourse

**URL:** https://meta.discourse.org/t/install-discourse-for-development-using-docker/102009
**Category:** Developer Guides
**Tags:** docker, dev-install, how-to
**Created:** [2018年十一月16日 09:40 UTC](https://meta.discourse.org/t/install-discourse-for-development-using-docker/102009 "2018-11-16T09:40:04Z")
**Posts on this page:** 20
**Page:** 5

<div class="post-metadata">

### Author: ![max-elia](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/max-elia/32/225853_2.png) [@max-elia](https://meta.discourse.org/u/max-elia)
#### Post date: [2021年七月2日 12:45 UTC](https://meta.discourse.org/t/install-discourse-for-development-using-docker/102009/168 "2021-07-02T12:45:55Z")

</div>

感谢您提供的指南。

不过，我在从管理后台创建备份时遇到了问题。我收到的错误信息是：  
`pg_dump: error: connection to database "discourse development" failed: FATAL: Peer authentication failed for user "postgres".`

我已检查 pg\_hba.conf 文件，并将所有选项都设置为 trust。

如果能得到一些协助以解决此问题，将不胜感激。

我在 Ubuntu 和 macOS 上都进行了尝试。除了备份功能外，其他一切（如创建帖子、API 调用等）在两个系统上均运行正常。

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [2021年八月30日 06:39 UTC](https://meta.discourse.org/t/install-discourse-for-development-using-docker/102009/169 "2021-08-30T06:39:59Z")

</div>

感谢这个出色的 Docker 解决方案。

在运行插件规格测试时，以下命令效果很好：

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

是否有一种方法可以像非 Docker 开发环境那样，针对特定的插件测试？例如：

`LOAD_PLUGINS=1 RAILS_ENV=test rspec plugins/discourse-follow/spec/requests/follow_controller_spec.rb:32`

我尝试过类似以下的方式：

`d/rspec plugins/discourse-follow/spec/lib/updater_spec.rb:10 LOAD_PLUGINS=1 RAILS_ENV=test`

但遇到了错误。

---

<div class="post-metadata">

### Author: ![Simon\_Manning](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon_manning/32/198596_2.png) [@Simon\_Manning](https://meta.discourse.org/u/Simon_Manning)
#### Post date: [2021年八月30日 14:26 UTC](https://meta.discourse.org/t/install-discourse-for-development-using-docker/102009/170 "2021-08-30T14:26:06Z")

</div>

`LOAD_PLUGINS` 和 `RAILS_ENV` 必须放在命令之前以设置环境变量。如果放在命令之后，它们会被当作 `rspec` 的参数，而 `rspec` 无法识别这些参数。

`LOAD_PLUGINS=1 RAILS_ENV=test d/rspec plugins/discourse-follow/spec/lib/updater_spec.rb:10`

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [2021年八月30日 14:30 UTC](https://meta.discourse.org/t/install-discourse-for-development-using-docker/102009/171 "2021-08-30T14:30:25Z")

</div>

不，我还没被说服这行得通——你实际试过吗？

我得到：

```plaintext
NameError:
  uninitialized constant Follow

```

我怀疑这是因为环境变量没有传递到 Docker 进程中？

这正是我将其作为参数传递的原因。

---

<div class="post-metadata">

### Author: ![Simon\_Manning](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon_manning/32/198596_2.png) [@Simon\_Manning](https://meta.discourse.org/u/Simon_Manning)
#### Post date: [2021年八月30日 14:56 UTC](https://meta.discourse.org/t/install-discourse-for-development-using-docker/102009/172 "2021-08-30T14:56:39Z")

</div>

抱歉，我误读了您的两条指令，以为“第一条命令对该规范有效，第二条无效”。我目前没有搭建开发环境来进行测试。

查看 GitHub 上的 rspec 文件后，我认为您说得对，环境变量确实没有被传入。不过，您应该可以通过运行 `d/shell` 进入容器内的 shell，然后在那里执行您的 rspec 命令。

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [2021年八月30日 15:01 UTC](https://meta.discourse.org/t/install-discourse-for-development-using-docker/102009/173 "2021-08-30T15:01:52Z")

</div>

Simon，这不仅仅是一个出色的可行变通方案，非常感谢！

刚试了一下，确实有效，例如：

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

```

已将此方法和整个插件版本添加到 Wiki 中。

---

<div class="post-metadata">

### Author: ![Simon\_Manning](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon_manning/32/198596_2.png) [@Simon\_Manning](https://meta.discourse.org/u/Simon_Manning)
#### Post date: [2021年八月30日 15:35 UTC](https://meta.discourse.org/t/install-discourse-for-development-using-docker/102009/174 "2021-08-30T15:35:44Z")

</div>

仔细看看 `d/exec`，`d/shell` 和 `d/rspec` 都在使用它，我认为它也可以这样简化：

```plaintext
RAILS_ENV=test d/exec LOAD_PLUGINS=1 rspec plugins/discourse-follow/spec/lib/updater_spec.rb:37

```

`d/exec` 会传入 `RAILS_ENV`，但不会传入 `LOAD_PLUGINS`，因此它们需要放在 `d/exec` 的两侧。

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [2021年八月30日 16:13 UTC](https://meta.discourse.org/t/install-discourse-for-development-using-docker/102009/175 "2021-08-30T16:13:09Z")

</div>

这给了我一个错误：

```plaintext
OCI 运行时 exec 失败：exec 失败：container_linux.go:380：启动容器进程导致：exec: "LOAD_PLUGINS=1"：在 $PATH 中未找到可执行文件：未知

```

---

<div class="post-metadata">

### Author: ![Simon\_Manning](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon_manning/32/198596_2.png) [@Simon\_Manning](https://meta.discourse.org/u/Simon_Manning)
#### Post date: [2021年八月30日 16:39 UTC](https://meta.discourse.org/t/install-discourse-for-development-using-docker/102009/176 "2021-08-30T16:39:44Z")

</div>

啊，看来不能那样使用 `docker exec` 来传递环境变量。不过没关系，至少先打开 shell 是可行的。

---

<div class="post-metadata">

### Author: ![dmc](https://avatars.discourse-cdn.com/v4/letter/d/839c29/32.png) [@dmc](https://meta.discourse.org/u/dmc)
#### Post date: [2021年九月5日 03:28 UTC](https://meta.discourse.org/t/install-discourse-for-development-using-docker/102009/177 "2021-09-05T03:28:15Z")

</div>

我和 Max 遇到了完全相同的问题。每当我尝试在本地开发的 Docker 安装环境中进行备份或恢复时，都会收到相同的错误：`Peer authentication failed for user "postgres"`（用户 “postgres” 的同行身份验证失败）。

经过一番排查，我发现开发环境中的数据库配置显示如下：

```plaintext
BackupRestore.database_configuration
=> #<struct BackupRestore::DatabaseConfiguration host=nil, port=nil, username="postgres", password=nil, database="discourse_development">

```

不知何故，开发环境没有在环境变量中设置用户名，导致 `BackupRestore` 模块将用户名默认设置为 `postgres`。

```ruby
# lib/backup_restore.rb:122

    DatabaseConfiguration.new(
      config["backup_host"] || config["host"],
      config["backup_port"] || config["port"],
      config["username"] || username || ENV["USER"] || "postgres",
      config["password"] || password,
      config["database"]
    )

```

我们可以在哪里设置正确的数据库用户名？

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [2021年九月6日 10:58 UTC](https://meta.discourse.org/t/install-discourse-for-development-using-docker/102009/178 "2021-09-06T10:58:01Z")

</div>

我们该如何在此处使用 [https://meta.discourse.org/t/discourse-theme-cli-console-app-to-help-you-build-themes/82950？](https://meta.discourse.org/t/discourse-theme-cli-console-app-to-help-you-build-themes/82950%EF%BC%9F)

我们已经成功安装了 gem：`d/exec sudo gem install discourse_theme`……现在的挑战是如何将其符号链接到本地主题……

@Simon_Manning 请注意使用 sudo 来解决权限问题（感谢 @fzngagan 的提醒）。

---

<div class="post-metadata">

### Author: ![Naresh.Modhipalli](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/naresh.modhipalli/32/234273_2.png) [@Naresh.Modhipalli](https://meta.discourse.org/u/Naresh.Modhipalli)
#### Post date: [2021年九月16日 18:34 UTC](https://meta.discourse.org/t/install-discourse-for-development-using-docker/102009/179 "2021-09-16T18:34:52Z")

</div>

我正在尝试使用 Docker 设置测试一个插件。应用程序会随机停止加载，此时我只看到一个空白页面，直到我删除数据文件夹并重新构建所有内容。有什么调试该问题的建议吗？

---

<div class="post-metadata">

### Author: ![helmi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/helmi/32/114772_2.png) [@helmi](https://meta.discourse.org/u/helmi)
#### Post date: [2021年十月13日 16:55 UTC](https://meta.discourse.org/t/install-discourse-for-development-using-docker/102009/180 "2021-10-13T16:55:56Z")

</div>

关于那个问题有什么发现吗？似乎仍然存在问题。

---

<div class="post-metadata">

### Author: ![dmc](https://avatars.discourse-cdn.com/v4/letter/d/839c29/32.png) [@dmc](https://meta.discourse.org/u/dmc)
#### Post date: [2021年十月14日 10:54 UTC](https://meta.discourse.org/t/install-discourse-for-development-using-docker/102009/181 "2021-10-14T10:54:52Z")

</div>

我的“权宜之计”是将以下代码块中的用户名从 **postgres** 替换为 **discourse** ：

```ruby
# lib/backup_restore.rb:122

    DatabaseConfiguration.new(
      config["backup_host"] || config["host"],
      config["backup_port"] || config["port"],
      config["username"] || username || ENV["USER"] || "postgres",
      config["password"] || password,
      config["database"]
    )

```

---

<div class="post-metadata">

### Author: ![helmi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/helmi/32/114772_2.png) [@helmi](https://meta.discourse.org/u/helmi)
#### Post date: [2021年十月15日 07:22 UTC](https://meta.discourse.org/t/install-discourse-for-development-using-docker/102009/182 "2021-10-15T07:22:27Z")

</div>

我从本地 Mac 切换到 Ubuntu 虚拟机，本以为这样能更容易运行，但遗憾的是目前行不通。

在早期阶段已经遇到了一些奇怪的权限问题。`d/bundle install` 报告需要 sudo 权限才能安装某些内容，`d/rails s` 也返回了权限问题。

```plaintext
Traceback (most recent call last):
        8: from /src/bin/unicorn:70:in `<main>'
        7: from /src/bin/unicorn:38:in `ensure_cache_clean!'
        6: from /usr/local/lib/ruby/2.7.0/fileutils.rb:211:in `mkdir_p'
        5: from /usr/local/lib/ruby/2.7.0/fileutils.rb:211:in `each'
        4: from /usr/local/lib/ruby/2.7.0/fileutils.rb:226:in `block in mkdir_p'
        3: from /usr/local/lib/ruby/2.7.0/fileutils.rb:226:in `reverse_each'
        2: from /usr/local/lib/ruby/2.7.0/fileutils.rb:228:in `block (2 levels) in mkdir_p'
        1: from /usr/local/lib/ruby/2.7.0/fileutils.rb:250:in `fu_mkdir'
/usr/local/lib/ruby/2.7.0/fileutils.rb:250:in `mkdir': Permission denied @ dir_s_mkdir - /src/tmp (Errno::EACCES)

```

有什么想法吗？这台机器之前曾无问题地运行过生产环境的 Discourse。我只是停止了并移除了那些容器，然后将开发用的 git 仓库克隆到了另一个目录。目前正通过 tmux 来运行所有操作以处理不同的 shell 实例。

---

<div class="post-metadata">

### Author: ![dfabulich](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dfabulich/32/108716_2.png) [@dfabulich](https://meta.discourse.org/u/dfabulich)
#### Post date: [2021年十月21日 06:14 UTC](https://meta.discourse.org/t/install-discourse-for-development-using-docker/102009/183 "2021-10-21T06:14:25Z")

</div>

我还没有使用 M1，但我预计很快就会切换，而且我真的很喜欢 Docker 设置的便利性。

> [@rishabh](#):
>
> ⚠ Discourse 开发 Docker 镜像仅适用于 x86\_64 架构。M1 Mac 可以通过架构模拟启动该镜像，但由于 QEMU 中 [缺乏 inotify 支持](https://github.com/discourse/discourse/pull/13117)，Discourse 很可能无法启动。

该 PR 链接指向 [https://github.com/docker/for-mac/issues/5321](https://github.com/docker/for-mac/issues/5321)，其中他们表示：

> [唯一的解决方案是切换到支持 arm64 的多架构镜像。](https://github.com/docker/for-mac/issues/5321#issuecomment-824283216) 这些镜像也会快得多，并且通常更可靠。我建议调查您正在使用的基础镜像，并在可能的情况下切换到多架构镜像。您可以在 Docker Hub 上查看每个镜像支持哪些架构：[…]
> 
> 若要自行构建多架构镜像，我推荐使用 `docker buildx`，请查看这篇博客文章：[https://www.docker.com/blog/multi-arch-build-and-images-the-simple-way/](https://www.docker.com/blog/multi-arch-build-and-images-the-simple-way/)

Discourse 团队是否愿意支持多架构镜像？看起来 Discourse 的基础镜像基于 `debian:buster-slim`，而该镜像 _确实_ 支持多架构，因此将 Discourse 基础镜像改为多架构似乎不应过于困难。但这可能会使你们处于必须在生产环境中支持 ARM 的境地。需要有人（也许是 Discourse 团队？）在 x86\_64 和 ARM 上运行 Discourse 测试，修复失败时的问题等。

在这里提交 PR 会受到欢迎吗？

（依我之见，即使在云托管环境中，ARM 也是未来的架构。）

---

<div class="post-metadata">

### Author: ![Alexander\_Wright](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alexander_wright/32/119518_2.png) [@Alexander\_Wright](https://meta.discourse.org/u/Alexander_Wright)
#### Post date: [2021年十月28日 17:58 UTC](https://meta.discourse.org/t/install-discourse-for-development-using-docker/102009/184 "2021-10-28T17:58:15Z")

</div>

我尝试在 WSL2 上设置此环境。

我已经成功启动 ember\_cli，但 Chrome 显示了以下错误：

> [@](#):
>
> # 此页面无法工作
> 
> **localhost** 未发送任何数据。
> 
> ERR\_EMPTY\_RESPONSE

终端或开发日志中均未显示任何错误。欢迎提供任何建议，谢谢？

---

<div class="post-metadata">

### Author: ![pgrand](https://avatars.discourse-cdn.com/v4/letter/p/c0e974/32.png) [@pgrand](https://meta.discourse.org/u/pgrand)
#### Post date: [2021年十月28日 20:46 UTC](https://meta.discourse.org/t/install-discourse-for-development-using-docker/102009/185 "2021-10-28T20:46:21Z")

</div>

这真的很有帮助

---

<div class="post-metadata">

### Author: ![koen360](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/koen360/32/194302_2.png) [@koen360](https://meta.discourse.org/u/koen360)
#### Post date: [2021年十一月10日 17:57 UTC](https://meta.discourse.org/t/install-discourse-for-development-using-docker/102009/186 "2021-11-10T17:57:56Z")

</div>

大家好，

我遇到了同样的问题，所以我想把它作为一个 bug 提出来。请看下面。

[在干净的开发 Docker 环境中备份恢复失败：用户“postgres”的对等身份验证失败](https://meta.discourse.org/t/backup-restore-failing-on-clean-dev-docker-env-fatal-peer-authentication-failed-for-user-postgres/208170)

如果我能提供更多信息或提供任何帮助，请告诉我。

Koen

---

<div class="post-metadata">

### Author: ![dcook](https://avatars.discourse-cdn.com/v4/letter/d/ba9def/32.png) [@dcook](https://meta.discourse.org/u/dcook)
#### Post date: [2021年十一月16日 04:43 UTC](https://meta.discourse.org/t/install-discourse-for-development-using-docker/102009/187 "2021-11-16T04:43:57Z")

</div>

无法在 openSUSE Leap 15 上运行。我猜它不受支持，尽管既然我们正在使用 Docker，这应该无关紧要……

```plaintext
Migrating database...
rake aborted!
Errno::EACCES: Permission denied @ dir_s_mkdir - /src/app/assets/javascripts/plugins
/src/lib/plugin/instance.rb:441:in `ensure_directory'
/src/lib/plugin/instance.rb:713:in `activate!'
lib/discourse.rb:283:in `block in activate_plugins!'
lib/discourse.rb:280:in `each'
lib/discourse.rb:280:in `activate_plugins!'
/src/config/application.rb:318:in `block in <class:Application>'
/src/lib/plugin_initialization_guard.rb:5:in `plugin_initialization_guard'
/src/config/application.rb:317:in `<class:Application>'
/src/config/application.rb:73:in `<module:Discourse>'
/src/config/application.rb:72:in `<main>'
/src/Rakefile:7:in `<main>'
(See full trace by running task with --trace)

```

我尝试手动创建“app/assets/javascripts/plugins”，但这导致了：

```plaintext
Migrating database...
rake aborted!
Errno::EACCES: Permission denied @ dir_s_mkdir - /src/tmp
lib/discourse.rb:94:in `atomic_write_file'
/src/lib/plugin/instance.rb:726:in `activate!'
lib/discourse.rb:283:in `block in activate_plugins!'
lib/discourse.rb:280:in `each'
lib/discourse.rb:280:in `activate_plugins!'
/src/config/application.rb:318:in `block in <class:Application>'
/src/lib/plugin_initialization_guard.rb:5:in `plugin_initialization_guard'
/src/config/application.rb:317:in `<class:Application>'
/src/config/application.rb:73:in `<module:Discourse>'
/src/config/application.rb:72:in `<main>'
/src/Rakefile:7:in `<main>'
(See full trace by running task with --trace)

```

所以我将 mkdir tmp 放在源文件夹中……

但这又导致了：

```plaintext
Migrating database...
rake aborted!
Errno::EACCES: Permission denied @ rb_sysopen - /src/tmp/5ad4443faf817dc922116f8df65ae5c3
lib/discourse.rb:97:in `initialize'
lib/discourse.rb:97:in `open'
lib/discourse.rb:97:in `atomic_write_file'
/src/lib/plugin/instance.rb:726:in `activate!'
lib/discourse.rb:283:in `block in activate_plugins!'
lib/discourse.rb:280:in `each'
lib/discourse.rb:280:in `activate_plugins!'
/src/config/application.rb:318:in `block in <class:Application>'
/src/lib/plugin_initialization_guard.rb:5:in `plugin_initialization_guard'
/src/config/application.rb:317:in `<class:Application>'
/src/config/application.rb:73:in `<module:Discourse>'
/src/config/application.rb:72:in `<main>'
/src/Rakefile:7:in `<main>'
(See full trace by running task with --trace)

```

看起来 boot\_dev 是以 discourse 用户身份运行 docker exec……但目录归用户 1016（我的主机用户的 uid）所有。

我想很多开发者在他们的个人笔记本电脑上不会遇到这个问题，因为他们的用户 uid 是 1000，这恰好吻合……

[上一頁](https://meta.discourse.org/t/install-discourse-for-development-using-docker/102009.md?page=4)

[下一頁](https://meta.discourse.org/t/install-discourse-for-development-using-docker/102009.md?page=6)
