我在Docker容器中使用Rails,偶尔我会遇到一个我不知道如何解决的问题。当向Gemfile中添加一个新的gem时,在重新构建Docker +容器时,使用常见的绑定错误Could not find [GEM_NAME] in any of the sources; Run 'bundle install' to install missing gems,构建将失败。只有当我试图在Docker中构建映像时,如果我在本地计算机上运行一个常规的bundle install,Gemfile就会被正确安装,并且一切都按预期工作,我才会想到这一点。
我有一个相当标准的Dockerfile & docker-compose文件。
Dockerfile:
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 -a、docker builder prune -a、重新安装Docker、行多个重新构建、重新启动我的机器等等,但都没有效果。奇怪的是,并不是每一个新的宝石,我决定添加,只有一些特定的宝石。例如,当我试图将gem 'sendgrid-ruby'添加到我的Gemfile时,再次遇到了这个问题。这是gem的参考回购,我在sendgrid-ruby中得到的具体错误是Could not find ruby_http_client-3.5.1 in any of the sources。我尝试在我的Gemfile中指定ruby_http_client,我也尝试在Docker容器中指定ssh并运行gem install ruby_http_client,但是我得到了相同的错误。
这里可能会发生什么事?
发布于 2020-11-15 12:22:50
您将在容器的/usr/local/bundle目录上挂载一个命名卷。命名卷将从图像中填充,但只有在第一次运行容器时才会填充。之后,命名卷的旧内容将优先于图像的内容:通过这种方式使用卷将导致Docker完全忽略您在Gemfile中所做的任何更改。
您应该能够从volumes:文件中删除该docker-compose.yml行。我不清楚将已安装的宝石保存在指定的卷中会带来什么好处。
https://stackoverflow.com/questions/64843732
复制相似问题