Docker 安装
使用 Docker 进行开发
由于 Discourse 运行在 Docker 中,你应该可以使用 Discourse 开发容器直接从源码目录运行 Discourse。
:white_check_mark: 优点:无需安装任何系统依赖,快速搭建开发环境完全不需要任何配置。
:x: 缺点:会比 Ubuntu 上的原生开发环境稍慢,比 MacOS 上的原生安装慢得多。
另请参见 https://meta.discourse.org/t/developing-discourse-using-a-dev-container/336366
第 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 大概率无法启动。
作为替代,你应该使用 https://meta.discourse.org/t/beginners-guide-to-install-discourse-on-macos-for-development/15772
选项 1: 从 Docker store 下载打包好的 .dmg
选项 2: brew install docker
第 2 步:启动容器
将 Discourse 仓库克隆到你的本地设备。
git clone https://github.com/discourse/discourse.git
cd discourse
(在源码根目录下)
d/boot_dev --init
# wait while:
# - dependencies are installed,
# - the database is migrated, and
# - an admin user is created (you'll need to interact with this)
# In one terminal:
d/rails s
# And in a separate terminal
d/dev --only ember
……然后在浏览器中打开 http://localhost:3000,_瞧!_你就应该能看到 Discourse 了。
插件符号链接
Docker 开发流程支持 plugins/ 目录下的符号链接,但有以下注意事项:
每当创建新的插件符号链接时,都必须使用以下命令重启 Docker 容器:
d/shutdown_dev; d/boot_dev
注意:
-
要测试电子邮件,请运行 MailHog:
d/mailhog -
如果缺少 gems,请运行:
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
谢谢你的反馈!