简体   繁体   English

容器需要重建并重新启动。 无法从浏览器重新加载

[英]Containers need to be rebuilt and restarted. Can't reload from browser

I have a pretty standard rails (5.0.1) project, using docker-compose (Docker version 1.12.5, build 7392c3b/docker-compose version 1.9.0, build 2585387) to manage the development environment. 我有一个非常标准的Rails(5.0.1)项目,使用docker-compose(Docker版本1.12.5,内部版本7392c3b / docker-compose版本1.9.0,内部2585387)来管理开发环境。 Unfortunately the usual development mode, where Rails reloads classes on every reload doesn't seem to work. 不幸的是,通常的开发模式(Rails在每次重新加载时都重新加载类)似乎不起作用。 Instead, my coding cycle has become: 相反,我的编码周期变成了:

  1. ^C out of docker-compose up docker-compose up ^ C docker-compose up
  2. docker-compose build
  3. docker-compose up
  4. Lather, rinse, repeat 泡沫,冲洗,重复

Because I have several services running, every time I am forced to ^C out of docker-compose up , rebuild the world and then restart, it costs time and I'm worried about kicking my Postgres (database) instance too many times in the head. 因为我有多个服务在运行,所以每当我被迫从docker-compose up退出^ C,重建世界,然后重新启动时,这会花费时间,而且我担心将Postgres(数据库)实例踢进去的次数太多了。头。

It's clear that mounting the app is working, because I can change view files and see the changes instantly. 很明显,安装该应用程序是可以的,因为我可以更改视图文件并立即查看更改。 But if I change any class , I'm forced to do the cancel-compile-restart dance. 但是,如果我更改任何课程 ,就不得不做“取消-编译-重新开始”舞蹈。

docker-compose.yml docker-compose.yml

services:
  app:
    env_file:
    - .env
    depends_on:
    - database
    - redis
    build:
      context: .
      args:
      - environment
    command: bin/rails server --port 3000 --binding 0.0.0.0
    environment:
      REDIS_URL: "redis://redis:6379/0"
    links:
    - database
    - redis
    network_mode: bridge
    ports:
    - "3000:3000"
    expose:
    - 3000
    volumes:
    - .:/app:rw

Dockerfile Docker文件

# https://hub.docker.com/_/ruby/
FROM ruby:2.3-slim

# Install apt based dependencies required to run Rails as
# well as RubyGems. As the Ruby image itself is based on a 
# Debian image, we use apt-get to install those.
# nodejs for JavaScript runtime
RUN apt-get update -qq && \
    apt-get install -y --no-install-recommends build-essential libpq-dev nodejs git && \
    rm -rf /var/lib/apt/lists/* && \
    echo "linux up-to-date"

# Configure the main working directory. This is the base
# directory used in any further RUN, COPY, and ENTRYPOINT 
# commands.
RUN mkdir /app
WORKDIR /app

# build args, provided --build-arg
ARG environment=production
ARG GIT_COMMIT=unknown
ARG VERSION=unknown

# May be overridden by docker-compose.yml
ENV RAILS_ENV=$environment RACK_ENV=$environment NODE_ENV=$environment

# Copy the Gemfile as well as the Gemfile.lock and install
# the RubyGems. This is a separate step so the dependencies 
# will be cached unless changes to one of those two files 
# are made.
COPY Gemfile Gemfile.lock ./
RUN gem install bundler && bundle install --jobs 20 --retry 5

# Copy the main application.
COPY . ./

# Precompile Rails assets
RUN bundle exec rake assets:precompile

# Start console (starting puma creates a .pid file that we don't want)
CMD bundle exec rails console

Late answer, but I found that setting this in development.rb fixed the issue. 较晚的答案,但是我发现在development.rb中设置此选项可以解决此问题。

config.reload_classes_only_on_change = false

It might have something to do with https://github.com/rails/rails/issues/16678 , which suggests it's an issue between VirtualBox file timestamps and the Rails reloader. 这可能与https://github.com/rails/rails/issues/16678有关 ,这表明VirtualBox文件时间戳和Rails重新加载器之间存在问题。

You will want to add restart: always to your Docker compose file and run it as a daemon by going docker compose up -d . 您将需要添加restart: always添加到Docker compose文件中,并通过使docker compose up -d作为守护程序运行它。 This will run it as a daemon no longer running it the foreground after build. 这将使其作为守护程序运行,而不再在构建后在前台运行。 It will also restart the container after if you restart the machine. 如果重新启动计算机,它还将重新启动容器。 Be aware that you should set restart=always on the dependencies as well to have them start up on reboot. 请注意,您还应该在依赖项上设置restart=always ,以使其在重新启动时启动。 Lastly, you should mount your application as a volume so you can modify the code and cycle it via docker restart app . 最后,您应该将应用程序作为卷挂载,以便您可以修改代码并通过docker restart app对其进行循环。 When you cop files into a Docker container you are creating a snapshot which is why you have to rebuild it every time. 当您将文件复制到Docker容器中时,您将创建一个快照,这就是为什么每次都要重建它的原因。 The solution to this is mount your code directory as a volume and set the compile command as The start CMD so it excites the build on restart. 解决方案是将代码目录安装为卷,并将compile命令设置为The start CMD,以便在重新启动时激发构建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM