【问题标题】:Run bundle install after adding/removing a new gem when using docker-compose使用 docker-compose 时添加/删除新 gem 后运行 bundle install
【发布时间】:2021-03-04 14:38:07
【问题描述】:

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

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

每当我在我的 Gemfile 中添加 Gem 时,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 删除了这些卷。 它删除了卷,但仍然出现相同的错误。

【问题讨论】:

  • 您应该删除所有volumes:,以便您使用图像中内置的代码。特别是在 /usr/local/bundle/gems 上的卷挂载将导致 Docker 使用该目录的旧版本并忽略您在服务的 Gemfile 中所做的任何更改。
  • @DavidMaze - 我正在使用 docker-compose down -v 删除所有卷,但在运行 docker-compose up 时仍然存在同样的问题。

标签: ruby-on-rails ruby docker docker-compose


【解决方案1】:

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

【讨论】:

  • 感谢您的回复,但不幸的是它没有成功。我确实找到了一种方法,在我的入口文件中添加bundle install
【解决方案2】:

我通过在我的入口点文件中添加 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    相关资源
    最近更新 更多