我正在尝试“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

最佳答案

我假设您正在对本地文件系统进行更改,并在容器内触发预期的守卫。

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

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

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

关于ruby-on-rails - 为什么在dockerizing Rails 应用程序后Guard 没有检测到文件更改?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54026795/

10-14 23:52
查看更多