Docker容器中的Rails应用程序不会在开发中重新加载

sam*_*uel 17 ruby-on-rails docker docker-compose

我按照这个docker-compose教程介绍了如何启动rails应用程序.它运行完美但我更换控制器时不会重新加载应用程序.

有什么可以遗漏的?

Jos*_*e A 9

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

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 . .
Run Code Online (Sandbox Code Playgroud)

/var/www目录在这里是关键。那就是图像的内部文件夹结构,以及您需要将当前目录映射到的位置。

然后,在您的docker-compose中,定义一个名为volume的索引,并将该路由放置在那里(也适用于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"
Run Code Online (Sandbox Code Playgroud)

当前项目目录

上图仅供参考。检查docker-compose和Dockefile是否在同一目录中。它们不必一定是这样,您只需要确保正确指定了目录即可。

docker-compose 相对于file起作用。这./意味着它将以当前的docker-compose目录(在本例中为整个ruby应用程序)作为托管图像内容的位置。

: 只是一种之间鸿沟的地方将是VS在图像有它。

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

第2步:

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

并查找config.file_watcher,替换:

config.file_watcher = ActiveSupport :: EventedFileUpdateChecker用于:

  config.file_watcher = ActiveSupport::FileUpdateChecker
Run Code Online (Sandbox Code Playgroud)

这将改为采用轮询机制。

而已!

请记住,任何不是routes.rb的东西,也不是在应用程序文件夹中,很可能需要手动重新加载Rails应用程序。

  • 我已经介绍了步骤1,步骤2为我解决了此问题。这应该是答案。 (2认同)

Evg*_*kin 1

尝试使用以下命令重建映像以将更改添加到 Docker 化应用程序:

docker-compose build
Run Code Online (Sandbox Code Playgroud)

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