我也遇到了这个问题,你需要做两件事:
- 将当前目录映射到 Docker 当前托管文件的位置。
- 将 config.file_watcher 更改为 ActiveSupport::FileUpdateChecker
Step 1:
在您的 Dockerfile 中,检查您在何处复制/添加文件。
查看我的 Dockerfile:
# https://docs.docker.com/compose/rails/#define-the-project
FROM ruby:2.5.0
# The qq is for silent output in the console
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs vim
# Sets the path where the app is going to be installed
ENV RAILS_ROOT /var/www/
# Creates the directory and all the parents (if they don't exist)
RUN mkdir -p $RAILS_ROOT
# This is given by the Ruby Image.
# This will be the de-facto directory that
# all the contents are going to be stored.
WORKDIR $RAILS_ROOT
# We are copying the Gemfile first, so we can install
# all the dependencies without any issues
# Rails will be installed once you load it from the Gemfile
# This will also ensure that gems are cached and onlu updated when
# they change.
COPY Gemfile ./
COPY Gemfile.lock ./
# Installs the Gem File.
RUN bundle install
# We copy all the files from the current directory to our
# /app directory
# Pay close attention to the dot (.)
# The first one will select ALL The files of the current directory,
# The second dot will copy it to the WORKDIR!
COPY . .
The /var/www
目录是这里的关键。这是图像的内部文件夹结构,也是您需要将当前目录映射到的位置。
然后,在 docker-compose 中定义一个名为volumes的索引,并将该路由放在那里(也适用于V2!):
version: '3'
services:
rails:
# Relative path to Dockerfile.
# Docker will read the Dockerfile automatically
build: .
# This is the one that makes the magic
volumes:
- "./:/var/www"
上图仅供参考。检查 docker-compose 和 Dockefile 是否位于同一目录中。他们not一定需要这样,您只需确保正确指定目录即可。
docker-compose 工作相对于文件. The ./
意味着它将把当前的 docker-compose 目录(在本例中是整个 ruby 应用程序)作为托管图像内容的位置。
The :
只是一种区分图像将要出现的位置和图像实际存在的位置的方法。
下一部分:/var/www/
与您在 Dockerfile 中指定的路径相同。
Step 2:
打开development.rb(可以在/config/environments中找到)
并寻找config.file_watcher
, 代替:
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
for:
config.file_watcher = ActiveSupport::FileUpdateChecker
这将采用轮询机制来代替。
如果这不起作用,请尝试在环境文件中添加以下行,在本例中为development.rb:
config.cache_classes = false
就是这样!
请记住,任何不是routes.rb 且不在应用程序文件夹内的内容,很可能需要手动重新加载Rails 应用程序。