Rails 仅在 Docker 容器中捆绑 "Can't find GEM in any of the sources"

Rails bundle "Can't find GEM in any of the sources" in Docker Container only

我在 Docker 容器中使用 Rails,每隔一段时间我就会 运行 遇到这个我不知道如何解决的问题。将新的 gem 添加到 Gem 文件时,在重建 Docker 图像 + 容器时,构建将失败并出现常见的捆绑程序错误 Could not find [GEM_NAME] in any of the sources; Run 'bundle install' to install missing gems。当我尝试在 Docker 中构建图像时,我只 想到 ,如果我 运行 在我的本地机器上使用常规 bundle install,Gem文件安装正确,一切正常。

我有一个相当标准的 Docker 文件和 docker-compose 文件。

Docker文件:

FROM ruby:2.6.3

ARG PG_MAJOR
ARG BUNDLER_VERSION
ARG UID
ARG MODE

# Add POSTGRESQL to the source list using the right version
RUN curl -sSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
  && echo 'deb http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main' $PG_MAJOR > /etc/apt/sources.list.d/pgdg.list

ENV RAILS_ENV $MODE

RUN apt-get update -qq && apt-get install -y postgresql-client-$PG_MAJOR vim
RUN apt-get -y install sudo

RUN mkdir /usr/src/app
WORKDIR /usr/src/app
COPY Gemfile /usr/src/app/Gemfile
COPY Gemfile.lock /usr/src/app/Gemfile.lock
ENV BUNDLER_VERSION $BUNDLER_VERSION
RUN gem install bundler:$BUNDLER_VERSION

RUN bundle install

COPY . /usr/src/app

# Add a script to be executed every time the container starts.

COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]

docker-compose.yml:

version: '3'
services:
  backend:
    build:
      dockerfile: Dockerfile
      args:
        UID: ${UID:-1001}
        BUNDLER_VERSION: 2.0.2
        PG_MAJOR: 10
        mode: development
    tty: true
    stdin_open: true
    volumes:
      - ./[REDACTED]:/usr/src/app
      - gem_data_api:/usr/local/bundle:cached
    ports:
      - "3000:3000"
    user: root

我试过docker system prune -adocker builder prune -a、重新安装Docker、连续多次重建、重启我的机器等等,但都无济于事。奇怪的是,它不会发生在我决定添加的每个新 Gem 中,只会发生在某些特定的 gem 中。例如,我在尝试将 gem 'sendgrid-ruby' 添加到我的 Gem 文件时再次遇到此问题。 This 是 gem 的 repo 以供参考,我用 sendgrid-ruby 得到的具体错误是 Could not find ruby_http_client-3.5.1 in any of the sources。我尝试在我的 Gem 文件中指定 ruby_http_client,我也尝试 sshing 到 Docker 容器和 运行ning gem install ruby_http_client,但是我得到相同的错误。

这里可能发生了什么?

您正在容器的 /usr/local/bundle 目录上安装命名卷。命名卷将从图像中填充,但只有在您第一次 运行 容器时。之后,指定卷的旧内容将优先于图像的内容:以这种方式使用卷将导致 Docker 完全忽略您在 Gemfile.[=14= 中所做的任何更改]

您应该能够从 docker-compose.yml 文件中删除 volumes: 行。我不清楚将已安装的 gem 保存在命名卷中会有什么好处。