【问题标题】:Why is Guard not detecting file changes after dockerizing Rails app?为什么 Guard 在 dockerizing Rails 应用程序后没有检测到文件更改?
【发布时间】:2019-01-15 20:45:21
【问题描述】:

我正在尝试“dockerize”现有的 Rails 开发应用程序。这是我第一次尝试 Docker。

我想设置 Guard 以侦听文件更改并运行相关规范。

Guard 服务似乎运行正常,并且日志显示:

guard_1     | 16:35:12 - INFO - Guard is now watching at '/app'

但是当我编辑/保存规范文件时,Guard 没有运行任何测试。

这是我正在迁移到 Docker 中的现有应用程序。它有一个在 Docker 之外工作的保护文件。

我已经搜索并阅读了许多帖子(例如this one),但我不确定从哪里开始调试它。谁能指出我正确的方向并让 Guard 监听文件更改。

我的 docker-compose.yml 看起来像这样:

version: '3'

services:
  postgres:
    ports:
      - "5432:5432"
    volumes:
      - $HOME/postgres-data:/var/lib/postgresql
    image: postgres:9.6.9

  redis:
    ports:
      - "6379:6379"
    depends_on:
      - postgres
    image: redis:5.0-rc

  web:
    build: .
    ports:
      - "3000:3000"
    command: /bin/sh -c "rails s -b 0.0.0.0 -p 3000"
    depends_on:
      - postgres
      - redis
    env_file:
      - .env

  guard:
    build: .
    env_file:
      - .env
    command: bundle exec guard --no-bundler-warning --no-interactions

  sidekiq:
    build: .
    command: bundle exec sidekiq -C config/sidekiq.yml
    depends_on:
      - postgres
      - redis
    env_file:
      - .env

volumes:
  redis:
  postgres:
  sidekiq:
  guard:

保护文件

guard 'spring', bundler: true do
  watch('Gemfile.lock')
  watch(%r{^config/})
  watch(%r{^spec/(support|factories)/})
  watch(%r{^spec/factory.rb})
end

guard :rspec, cmd: "bundle exec rspec" do
  require "guard/rspec/dsl"
  dsl = Guard::RSpec::Dsl.new(self)

  # RSpec files
  rspec = dsl.rspec
  watch(rspec.spec_files)

  # Ruby files
  ruby = dsl.ruby
  dsl.watch_spec_files_for(ruby.lib_files)

  # Rails files
  rails = dsl.rails(view_extensions: %w(erb haml slim))
  dsl.watch_spec_files_for(rails.app_files)
  dsl.watch_spec_files_for(rails.views)

  watch(rails.controllers) do |m|
    [
      rspec.spec.call("routing/#{m[1]}_routing"),
      rspec.spec.call("controllers/#{m[1]}_controller"),
      rspec.spec.call("acceptance/#{m[1]}")
    ]
  end

  # Rails config changes
  watch(rails.spec_helper)     { rspec.spec_dir }
  watch(rails.routes)          { "#{rspec.spec_dir}/routing" }
  watch(rails.app_controller)  { "#{rspec.spec_dir}/controllers" }

  # Capybara features specs
  watch(rails.view_dirs)     { |m| rspec.spec.call("features/#{m[1]}") }
  watch(rails.layouts)       { |m| rspec.spec.call("features/#{m[1]}") }

  # Turnip features and steps
  watch(%r{^spec/acceptance/(.+)\.feature$})
  watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
    Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
  end
  ignore %r{^spec/support/concerns/}
end

guard 'brakeman', :run_on_start => true do
  watch(%r{^app/.+\.(erb|haml|rhtml|rb)$})
  watch(%r{^config/.+\.rb$})
  watch(%r{^lib/.+\.rb$})
  watch('Gemfile')
end

【问题讨论】:

  • 你能发布你的 Guardfile 吗?并尝试在您的guard 服务下添加tty: true?另外,您是否尝试过在 docker 之外运行 guard 命令以确保其配置正确?我会尝试独立运行它以确保它看到更改以确保您已正确配置它,然后将其移动到容器中。另外,您使用的是 minitest 还是 rspec?
  • 谢谢@JayDorsey,我会在上面添加保护文件。我正在使用 Rspec。如果我从命令提示符运行bundle exec guard,它会正确运行。我刚刚离开我的开发计算机,但会尝试从 docker bash 提示符运行它并报告回来。与tty: true 配置相同。谢谢!

标签: ruby-on-rails docker guard


【解决方案1】:

我假设您正在对本地文件系统和容器内的预期保护进行更改以触发。

如果是这样,则缺少的链接是您的 docker-compose.yml 文件。

guard:
  build: .
  env_file:
    - .env
  command: bundle exec guard --no-bundler-warning --no-interactions
  volumes:
    - .:/app

您需要在容器内挂载根(Rails 根)目录的卷,以便反映更改。如果没有这一行,您的容器只会看到构建时可用的内容,而不是更改。

【讨论】:

  • 感谢@Jay,我认为这是可行的。我花了一些时间来确认这一点,因为:1. Guard 从 Docker 容器中响应非常缓慢,2. 当 Guard 启动时,我的所有 gem 似乎都没有自动加载(例如,NameError:未初始化的常量 FactoryBot)。知道为什么会这样吗?
  • 缓慢可能是由于 Docker 在 Docker 中挂载文件系统的方式。在我的系统(macOS)上也是如此。这是一个突出的已知问题。你可以看看docker-sync.io。我还没有测试过它,但它看起来可以解决这个问题。也可以看看这个:docs.docker.com/docker-for-mac/osxfs-caching 自动加载问题是否只存在于您的 Guard 容器中?您能否确认它不仅仅是显示 factorybot 错误的旧(er)日志文件?我似乎记得日志是持久的并显示以前迭代的错误
  • 鉴于这个缓慢的问题,您(或人们)是否真的以这种方式使用 Docker 进行文件监控和测试?它在我的机器上几乎无法使用。
  • 宝石的东西很奇怪。例如,我通过将 require 'factory_bot' 添加到 spec/support/factory_bot.rb 来修复 FactoryBot 问题。但随后我开始看到接下来调用的任何 gem 都出现相同的错误。所以宝石似乎可用,只是没有启用。该应用程序可以在所有其他环境中完美运行,无需 require gems。不知道从哪里开始!
  • 因为这个问题,我不将它用于本地开发。我从未尝试过 docker-sync ,但我怀疑它可能是一个可行的解决方案。我已经使用它来加速支持 Docker 的管道的测试和部署。宝石问题听起来很奇怪。您是否尝试在 Guardfile 中传递 bundler: true ?我认为您正在运行 bundle exec guard,但我认为还有另一个标志。
【解决方案2】:

我遇到了完全相同的问题,并且一直想知道现有的解决方案并没有真正奏效。您的问题的关键解决方案(当然,如果您使用的是 OSX)是了解“Docker Toolbox”和“Docker for Mac”之间的区别。

这篇文章给出了很多见解:https://docs.docker.com/docker-for-mac/docker-toolbox/

TL;DR

如果您使用的是 Mac,则需要使用 Docker for Mac 才能享受 osxfs 的好处。 如果你这样做,你将不需要 docker-sync!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-30
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 2013-12-03
    相关资源
    最近更新 更多