繁体   English   中英

使用 docker-compose 时添加/删除新 gem 后运行 bundle install

[英]Run bundle install after adding/removing a new gem when using docker-compose

我是 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"]

docker-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删除了这些卷。 它删除了卷,但仍然出现相同的错误。

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

我通过在我的入口点文件中添加bundle install来实现它

----------------./entrypoints/docker-entrypoint.sh-----------------

#!/bin/sh

set -e

if [ -f tmp/pids/server.pid ]; then
  rm tmp/pids/server.pid
fi

bundle install
bundle exec rails s -b 0.0.0.0

----------------./entrypoints/sidekiq-entrypoint.sh-----------------

#!/bin/sh

set -e

if [ -f tmp/pids/server.pid ]; then
  rm tmp/pids/server.pid
fi

bundle install && bundle exec sidekiq -C config/sidekiq.yml

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM