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

时间:2021-09-28 23:41:30

I followed this docker-compose tutorial on howto start a rails app. It runs perfectly but the app isn't reloaded when I change a controller.

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

What can it be missing?

它可以丢失什么?

3 个解决方案

#1


2  

You should create a volume that maps your local/host source code with the same code located inside docker in order to work on the code and enable such features.

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

Here's an example of a (docker-compose) mapped file that I'm updating in my editor without having to go through the build process just to see my updates:

这是一个(docker-compose)映射文件的示例,我在编辑器中更新,而不必通过构建过程只是为了查看我的更新:

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

Without mapping host <--> guest, the guest will simply run whatever code it has received during the build process.

如果没有映射主机< - > guest,guest将只运行它在构建过程中收到的任何代码。

#2


0  

Try to rebuild the image with the next command to add the changes to the dockerized app:

尝试使用下一个命令重建映像以将更改添加到dockerized app:

docker-compose build

And after it you need to restart the app with docker-compose up to recreate the docker container for your app.

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

#3


0  

I was struggling with this as well, there are 2 things that you need to do:

我也在努力解决这个问题,你需要做两件事:

1) Map the current directory to the place where Docker is currently hosting the files.

1)将当前目录映射到Docker当前托管文件的位置。

2) Change the config.file_watcher to ActiveSupport::FileUpdateChecker

2)将config.file_watcher更改为ActiveSupport :: FileUpdateChecker

Step 1:

In your Dockerfile, check where are you copying/adding the files.

在Dockerfile中,检查复制/添加文件的位置。

See my 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 directory is key here. That's the inner folder structure of the image, and where you need to map the current directory to.

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

Then, in your docker-compose, define an index called volumes, and place that route there (Works for V2 as well!):

然后,在您的docker-compose中,定义一个名为volumes的索引,并将该路由放在那里(Works for 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容器中的Rails应用程序不会在开发中重新加载

The image above is for reference. Check that the docker-compose and Dockefile are in the same directory. They not necessarily need to be like this, you just have to be sure that the directories are specified correctly.

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

docker-compose works relative to the file. The ./means that it will take the current docker-compose directory (In this case the entire ruby app) as the place where it's going to host the image's content.

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

The : just a way to divide between the where it's going to be vs where the image has it.

:只是一种方法,可以在它与图像所在的位置之间进行划分。

The next part: /var/www/ is the same path you specified in the Dockerfile.

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

Step 2:

Open development.rb (Can be found in /config/environments)

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

and look for config.file_watcher, replace:

并查找config.file_watcher,替换:

config.file_watcher = ActiveSupport::EventedFileUpdateChecker for:

config.file_watcher = ActiveSupport :: EventedFileUpdateChecker:

  config.file_watcher = ActiveSupport::FileUpdateChecker

This would do a polling mechanism instead.

这将改为采用轮询机制。

That's it!

而已!

Remember, that anything that is not routes.rb, and it's not inside the app folder, it's highly probable that the Rails app is going to need to be reloaded manually.

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

#1


2  

You should create a volume that maps your local/host source code with the same code located inside docker in order to work on the code and enable such features.

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

Here's an example of a (docker-compose) mapped file that I'm updating in my editor without having to go through the build process just to see my updates:

这是一个(docker-compose)映射文件的示例,我在编辑器中更新,而不必通过构建过程只是为了查看我的更新:

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

Without mapping host <--> guest, the guest will simply run whatever code it has received during the build process.

如果没有映射主机< - > guest,guest将只运行它在构建过程中收到的任何代码。

#2


0  

Try to rebuild the image with the next command to add the changes to the dockerized app:

尝试使用下一个命令重建映像以将更改添加到dockerized app:

docker-compose build

And after it you need to restart the app with docker-compose up to recreate the docker container for your app.

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

#3


0  

I was struggling with this as well, there are 2 things that you need to do:

我也在努力解决这个问题,你需要做两件事:

1) Map the current directory to the place where Docker is currently hosting the files.

1)将当前目录映射到Docker当前托管文件的位置。

2) Change the config.file_watcher to ActiveSupport::FileUpdateChecker

2)将config.file_watcher更改为ActiveSupport :: FileUpdateChecker

Step 1:

In your Dockerfile, check where are you copying/adding the files.

在Dockerfile中,检查复制/添加文件的位置。

See my 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 directory is key here. That's the inner folder structure of the image, and where you need to map the current directory to.

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

Then, in your docker-compose, define an index called volumes, and place that route there (Works for V2 as well!):

然后,在您的docker-compose中,定义一个名为volumes的索引,并将该路由放在那里(Works for 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容器中的Rails应用程序不会在开发中重新加载

The image above is for reference. Check that the docker-compose and Dockefile are in the same directory. They not necessarily need to be like this, you just have to be sure that the directories are specified correctly.

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

docker-compose works relative to the file. The ./means that it will take the current docker-compose directory (In this case the entire ruby app) as the place where it's going to host the image's content.

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

The : just a way to divide between the where it's going to be vs where the image has it.

:只是一种方法,可以在它与图像所在的位置之间进行划分。

The next part: /var/www/ is the same path you specified in the Dockerfile.

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

Step 2:

Open development.rb (Can be found in /config/environments)

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

and look for config.file_watcher, replace:

并查找config.file_watcher,替换:

config.file_watcher = ActiveSupport::EventedFileUpdateChecker for:

config.file_watcher = ActiveSupport :: EventedFileUpdateChecker:

  config.file_watcher = ActiveSupport::FileUpdateChecker

This would do a polling mechanism instead.

这将改为采用轮询机制。

That's it!

而已!

Remember, that anything that is not routes.rb, and it's not inside the app folder, it's highly probable that the Rails app is going to need to be reloaded manually.

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