我正在尝试在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/

10-16 05:23