对现有 Rails 应用程序进行 Docker 化

Dockerizing Existing Rails Application

我一直在尝试将我的 rails 应用程序移动到 docker 容器中,因为它曾经在开发环境的 virtualbox 中进行 ansible 管理。我查看了一些 docker 教程和一些特定于迁移 rails 应用程序的教程,但我 运行 遇到以下错误:

 => ERROR [10/10] RUN rails c                                                                                          0.9s
------
 > [10/10] RUN rails c:
#15 0.878 /usr/local/bundle/gems/bundler-1.17.3/lib/bundler/spec_set.rb:91:in `block in materialize': Could not find rake-13.0.1 in any of the sources (Bundler::GemNotFound)
#15 0.879   from /usr/local/bundle/gems/bundler-1.17.3/lib/bundler/spec_set.rb:85:in `map!'
#15 0.879   from /usr/local/bundle/gems/bundler-1.17.3/lib/bundler/spec_set.rb:85:in `materialize'
#15 0.879   from /usr/local/bundle/gems/bundler-1.17.3/lib/bundler/definition.rb:170:in `specs'
#15 0.879   from /usr/local/bundle/gems/bundler-1.17.3/lib/bundler/definition.rb:237:in `specs_for'
#15 0.879   from /usr/local/bundle/gems/bundler-1.17.3/lib/bundler/definition.rb:226:in `requested_specs'
#15 0.879   from /usr/local/bundle/gems/bundler-1.17.3/lib/bundler/runtime.rb:108:in `block in definition_method'
#15 0.879   from /usr/local/bundle/gems/bundler-1.17.3/lib/bundler/runtime.rb:20:in `setup'
#15 0.879   from /usr/local/bundle/gems/bundler-1.17.3/lib/bundler.rb:107:in `setup'
#15 0.879   from /usr/local/bundle/gems/bundler-1.17.3/lib/bundler/setup.rb:20:in `<top (required)>'
#15 0.879   from /usr/local/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
#15 0.879   from /usr/local/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
#15 0.879   from /usr/local/bundle/bin/rails:27:in `<main>'

这是我尝试通过 docker-compose 运行 编写的简化 docker 文件。我最初将它连接到我的 MySQL 图像,但已将其删除以帮助调试,但错误仍然存​​在。

Docker 文件

FROM ruby:2.4.0


RUN apt-get update -yqq \
    && apt-get install -yqq --no-install-recommends \
    build-essential \
    mysql-client \
    nodejs \
    supervisor \
    && apt-get clean -q clean \
    && rm -rf /var/lib/apt/lists


RUN mkdir -p /my_api
ENV RAILS_ROOT /my_api
WORKDIR $RAILS_ROOT

COPY ../../Gemfile ./
RUN gem install bundler --no-ri --no-rdoc --version 1.17.3
RUN bundle install 
COPY . ./

RUN gem list
RUN rails c

DockerCompose

version: "3.7"

services:
  api:
    image: my_api
    build:
      context: ./
      dockerfile: ./docker/app/dockerfile
    ports: 
      - 3000:3000
    working_dir: /api

我在以下 SO 线程中尝试了一些解决方案,但似乎无法消除错误:Specifying path to vendor/cache and installing rake before running bundle install

将我的解决方案放在这里以防万一有人遇到这个问题:

首先我更新了我的 docker 文件以执行以下操作

FROM ruby:2.4.0


RUN apt-get update -yqq \
    && apt-get install -yqq --no-install-recommends \
    build-essential \
    mysql-client \
    nodejs \
    supervisor \
    vim \
    lsof \
    htop \
    && apt-get clean -q clean \
    && rm -rf /var/lib/apt/lists


RUN mkdir -p /my_api
ENV RAILS_ROOT /my_api
WORKDIR $RAILS_ROOT

COPY Gemfile ./
COPY . ./
COPY ./railsbox/ansible/group_vars/all/.env ./.env
RUN gem install bundler  --no-ri --no-rdoc --version 1.17.3
RUN gem install rake --version 13.0.1 && bundle install --path vendor/cache --no-deployment

RUN gem list
CMD rm -f ./tmp/pids/server.pid && bundle exec rails s -b 0.0.0.0

注意 COPY 的变化。 ./.在 运行ning 捆绑包安装之前。这将避免在安装 运行(我相信)后覆盖 /vendor/cache。

我还更新了我的 docker-compose 设置,如下所示

api:
    image: my_api
    build:
      context: .
      dockerfile: ./docker/app/dockerfile
    depends_on:
      - mysql
      - mysql_test
    ports: 
      - "3000:3000"
    working_dir: /my_api
    volumes:
      - ./:/my_api
      - /my_api/vendor/cache
    links: 
      - mysql
      - mysql_test

请注意 /vendor/cache 的空白卷映射,如 中所建议的那样,它解释了关于 node_modules 的这一点。我相信通过此更改,您无需执行上述移动 COPY 的建议。 ./ 在捆绑包安装之前,但我将其保留原样,因为我认为这并不重要。