【问题标题】:Rails app in docker container doesn't reload in developmentDocker容器中的Rails应用程序不会在开发中重新加载
【发布时间】:2016-06-08 10:12:41
【问题描述】:

我关注了这个docker-compose tutorial 如何启动一个 Rails 应用程序。 它运行完美,但当我更改控制器时应用程序没有重新加载。

它可能缺少什么?

【问题讨论】:

  • 在文件系统事件和你的容器之间有很多东西可以进入。您能否添加指向教程的链接或有关您到目前为止设置的更多信息。
  • 这里是教程docs.docker.com/compose/rails,更准确地说我使用docker for mac
  • 你找到解决办法了吗?

标签: ruby-on-rails docker docker-compose


【解决方案1】:

我也在为此苦苦挣扎,您需要做两件事:

  1. 将当前目录映射到 Docker 当前托管文件的位置。
  2. 将 config.file_watcher 更改为 ActiveSupport::FileUpdateChecker

第 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 . .

/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 是否在同一目录中。它们不一定必须是这样的,您只需确保正确指定了目录即可。

docker-compose 相对于文件工作。 ./ 表示它将以当前的 docker-compose 目录(在本例中为整个 ruby​​ 应用程序)作为托管图像内容的位置。

: 只是一种区分它的位置和图像的位置的方法。

下一部分:/var/www/ 与您在 Dockerfile 中指定的路径相同。

第 2 步:

打开 development.rb(可以在 /config/environments 中找到)

并寻找config.file_watcher,替换:

config.file_watcher = ActiveSupport::EventedFileUpdateChecker

为:

config.file_watcher = ActiveSupport::FileUpdateChecker

这将改为使用轮询机制。

如果这不起作用,请尝试在环境文件中添加以下行,在本例中为 development.rb:

config.cache_classes = false

就是这样!

请记住,任何不是 routes.rb 且不在 a​​pp 文件夹中的内容,很有可能需要手动重新加载 Rails 应用程序。

【讨论】:

  • 我已经完成了第 1 步,第 2 步为我解决了这个问题。这应该是答案。
【解决方案2】:

尝试使用下一条命令重建映像,以将更改添加到 dockerized 应用程序:

docker-compose build

之后,您需要使用 docker-compose up 重新启动应用程序,以便为您的应用程序重新创建 docker 容器。

【讨论】:

  • 一些答案​​告诉我“增加了数量”。但我已经在 docker-compose 中有音量了。感谢您的回答,我已经解决了
【解决方案3】:

您应该创建一个卷,将您的本地/主机源代码与位于 docker 内的相同代码映射,以便处理代码并启用此类功能。

这是一个 (docker-compose) 映射文件的示例,我正在编辑器中更新它,而无需通过构建过程来查看我的更新:

  volumes:
    - ./lib/radius_auth.py:/etc/freeradius/radius_auth.py

如果没有映射主机 来宾,来宾将简单地运行它在构建过程中收到的任何代码。

【讨论】:

  • 是的,在 Rails 中你需要添加:config.file_watcher = ActiveSupport::FileUpdateChecker 到你的 environment/development.rb
【解决方案4】:

增加@Jose A 的答案,我将 development.rb 中的属性config.cache_classes 更改为false 并解决了问题。以下是其解释:

  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

【讨论】:

  • 谢谢你的回答,救了我。用您的输入编辑了上面的答案:D:D
【解决方案5】:

我遇到了同样的问题,对于某人,我不知道你的问题和我的一样,但我希望你能解决,对我来说,当我这样做时我可以解决。

1, Restart mac
2, Delete application.yml
3, make
4, make up

【讨论】:

  • 或 "docker-sync" 请尝试停止一次。如果换了分支,很多时候不会同步。
猜你喜欢
  • 1970-01-01
  • 2023-02-10
  • 1970-01-01
  • 2018-05-11
  • 1970-01-01
  • 2023-03-26
  • 2012-05-19
  • 1970-01-01
  • 2017-04-19
相关资源
最近更新 更多