首页 > 解决方案 > 使用 docker-compose 在添加/删除新 gem 后运行 bundle install

问题描述

我是 docker 新手,正在尝试将 docker 与我的 rails 应用程序集成。我在关注这个文档

我能够设置服务,但我遇到了问题。

每当我将 Gem 添加到我的 Gemfile 时,我都会docker-compose up出错

sidekiq_1  | bundler: failed to load command: sidekiq (/usr/local/bundle/bin/sidekiq)
sidekiq_1  | /usr/local/bundle/gems/bundler-2.2.3/lib/bundler/resolver.rb:309:in `block in verify_gemfile_dependencies_are_found!': Could not find gem 'awesome_print' in any of the gem sources listed in your Gemfile. (Bundler::GemNotFound)

Dockerfile

FROM ruby:3.0.0-alpine3.12

ENV BUNDLER_VERSION=2.2.3

RUN apk add --update --no-cache \
      binutils-gold \
      build-base \
      curl py-pip \
      curl \
      file \
      g++ \
      gcc \
      git \
      less \
      libstdc++ \
      libffi-dev \
      libc-dev \
      linux-headers \
      libxml2-dev \
      libxslt-dev \
      libgcrypt-dev \
      make \
      netcat-openbsd \
      nodejs \
      openssl \
      pkgconfig \
      python3 \
      tzdata \
      yarn

RUN gem install bundler -v 2.2.3

WORKDIR /app

COPY Gemfile Gemfile.lock ./

RUN bundle config build.nokogiri --use-system-libraries

RUN bundle check || bundle install

COPY package.json yarn.lock ./

RUN yarn install --check-files

COPY . ./

ENTRYPOINT ["./entrypoints/docker-entrypoint.sh"]

码头工人-compose.yml

version: '3.6'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: sna-main
    depends_on:
      - redis
    ports:
      - "3000:3000"
    volumes:
      - .:/app
      - gem_cache:/usr/local/bundle/gems
      - node_modules:/app/node_modules
    environment:
      RAILS_ENV: development

  redis:
    image: redis:5.0.7

  sidekiq:
    build:
      context: .
      dockerfile: Dockerfile
    depends_on:
      - app
      - redis
    volumes:
      - .:/app
      - gem_cache:/usr/local/bundle/gems
      - node_modules:/app/node_modules
    environment:
      RAILS_ENV: development
    entrypoint: ./entrypoints/sidekiq-entrypoint.sh

volumes:
  gem_cache:
  node_modules:

如果有人可以帮助我了解每次 Gemfile 发生变化时如何运行捆绑安装,那就太好了。

从文档中我了解到,因为我们正在缓存 Gems,所以如果我们添加/删除新的 gem,我们需要显式地删除卷。

我确实通过运行删除了卷docker-compose down -v。它删除了卷,但仍然出现相同的错误。

标签: ruby-on-railsrubydockerdocker-compose

解决方案


这有可能是因为 docker-compose 缓存了您的图像;您需要重新构建docker-compose up --build以反映 docker-compose 中的图像更改。


推荐阅读