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

时间:2017-02-25 23:31:17

标签: ruby-on-rails docker-compose

我有一个非常标准的rails(5.0.1)项目,使用docker-compose(Docker版本1.12.5,构建7392c3b / docker-compose版本1.9.0,版本2585387)来管理开发环境。不幸的是,通常的开发模式,Rails在每次重新加载时重新加载类似乎都不起作用。相反,我的编码周期变为:

  1. ^ C docker-compose up
  2. docker-compose build
  3. docker-compose up
  4. 泡沫,冲洗,重复
  5. 因为我有几个服务正在运行,每次我被迫从docker-compose up中删除,重建世界然后重新启动,这需要时间,我担心踢我的Postgres(数据库)实例太多次了。

    很明显,安装应用程序正在运行,因为我可以更改视图文件并立即查看更改。但是如果我改变任何,我就会被迫做一个cancel-compile-restart舞蹈。

      

    搬运工-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

    # 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
    

2 个答案:

答案 0 :(得分:0)

迟到的答案,但我发现在development.rb中设置此问题已解决此问题。

config.reload_classes_only_on_change = false

它可能与https://github.com/rails/rails/issues/16678有关,这表明它是VirtualBox文件时间戳和Rails重新加载器之间的问题。

答案 1 :(得分:-1)

您需要将restart: always添加到Docker撰写文件中,然后通过docker compose up -d将其作为守护程序运行。这将作为守护进程运行它,不再在构建后将其运行到前台。如果重新启动计算机,它也将重新启动容器。请注意,您还应该在依赖项上设置restart=always,以便在重新启动时启动它们。最后,您应该将应用程序挂载为卷,以便修改代码并通过docker restart app进行循环。将文件复制到Docker容器中时,您正在创建快照,这就是每次都必须重建它的原因。解决方案是将您的代码目录作为卷安装并将编译命令设置为启动CMD,以便在重新启动时激活构建。