ruby-on-rails - 与docker-compose bundle 在一起可从头开始安装gem

标签 ruby-on-rails docker docker-compose bundle bundler

我正在尝试在Docker构建上缓存 bundle 安装。
我已经尝试了几篇博客文章中的建议,但最终却遇到了这样的情况:

Dockerfile:

FROM ruby:2.5.0-alpine

RUN apk update && apk add build-base postgresql-dev nodejs git tzdata

ENV APP_HOME /panabus-api
RUN mkdir $APP_HOME
WORKDIR $APP_HOME

COPY Gemfile Gemfile.lock ./
ENV BUNDLE_PATH /gems
ENV GEM_PATH /gems
ENV GEM_HOME /gems

RUN bundle check || bundle install

COPY . $APP_HOME

ENV RAILS_ENV=production

RUN bundle exec rake RAILS_ENV=production assets:precompile apipie:cache

LABEL maintainer="Hari Carreras Pérez <hc@abtion.com>"

CMD bundle exec puma -C config/puma.rb

docker-compose.yml
version: '3'
services:
  box:
    image: busybox
    volumes:
      - gem_cache:/gems

  postgres:
    image: postgres:10.3-alpine
    ports:
      - 5432:5432
    volumes:
      - postgres:/var/lib/postgresql/data
    restart: always

  web:
    build: .
    image: panabus-api-web
    command: bundle exec puma -C config/puma.rb -p 3000
    depends_on:
      - postgres
      - box
    volumes:
      - .:/panabus-api
      - gem_cache:/gems
    ports:
      - "3000:3000"
    restart: always
    environment:
      - RAILS_ENV=development
    tty: true
    stdin_open: true

  setup:
    build: .
    depends_on:
      - postgres
      - box
    environment:
      - RAILS_ENV=development
    volumes:
      - .:/panabus-api
      - gem_cache:/gems
    command: "bin/rails db:create db:migrate"
volumes:
  postgres:
  gem_cache:

如果Gemfile没有更改,这不会更新gem。不幸的是,如果更改了,它将从头开始安装所有gem,而不使用缓存。

有什么办法可以在构建时缓存gem?
我想将 bundle 包保留在Dockerfile中,因为Heroku使用相同的Dockerfile。

最佳答案

您需要在 bundle 包中添加标签,以检查已安装 gem 的缓存位置

bundle check || bundle install --binstubs="$BUNDLE_BIN"

并使用特定的路径缓存包,如下所示
ENV BUNDLE_PATH=/bundle \
  BUNDLE_BIN=/bundle/bin \
  GEM_HOME=/bundle
ENV PATH="${BUNDLE_BIN}:${PATH}"

关于ruby-on-rails - 与docker-compose bundle 在一起可从头开始安装gem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49599562/

相关文章:

ruby-on-rails - 在不输入源代码的情况下在rails中设置测试环境变量

c# - 在 docker 容器外公开日志文件

docker - env_file在docker-compose中被忽略并导致 “The … variable is not set”警告

linux - 奇异并行查询

docker - 如何在 docker-compose 文件中使用等待?

ruby-on-rails - Rails 301 重定向

ruby-on-rails - Rails 抓取 - 提交表单

ruby-on-rails - 如何获取 foreign_key 列的名称

docker - 如何通过已设置管理员凭据的docker-compose启动gitlab-ce容器?

docker - docker-compose:为什么代理和应用服务由于主机名而失败?