docker-compose run命令在运行bundle install后找不到gem

问题描述 投票:0回答:2

我正在使用docker-compose为我的rails应用程序。

我最近更新了我的主分支,将rails版本更新到5.2.3 - 我通过docker-compose运行bundle install:

docker-compose run web bundle install

似乎它运行正常,但是当我尝试运行rspec时,我收到此错误:

Could not find activesupport-5.2.3 in any of the sources
Run `bundle install` to install missing gems.

我尝试运行bundle update activesupport - 并得到这个:

Bundler attempted to update activesupport but its version stayed the same
Bundle updated!

所以我尝试手动安装gem:

docker-compose run web gem install activesupport
Fetching activesupport-5.2.3.gem
Successfully installed activesupport-5.2.3
1 gem installed

然后我尝试再次运行rspec,同样的事情:

$ docker-compose run web bin/rspec ./spec/some_spec.rb 
Could not find activesupport-5.2.3 in any of the sources
Run `bundle install` to install missing gems.

docker-compose没有接受gem / bundler的更改?我在这里错过了什么吗?

ruby-on-rails docker rspec docker-compose bundler
2个回答
1
投票

docker-compose run每次调用时都会创建一个新容器,您的更改不会持久存在。

如果您希望更改保持不变,请使用docker-compose exec,它在正在运行的容器中运行您的命令。


3
投票

每个docker-compose run都在开始一个新容器。运行它两次,然后运行docker ps -a,你会看到两个已退出的容器。

您需要在bundle install中运行Dockerfile作为图像构建过程的一部分。

作为旁注提示,通常的做法是首先复制GemfileGemfile.lock文件,运行bundle install,然后才复制整个应用程序。这样,您可以创建两个单独的图层,并避免在应用程序文件更改时重新安装所有gem。

这是一个Dockerfile供参考。

FROM ruby:2.5.3

WORKDIR $RAILS_ROOT

# ... more custom stuff here ...

# Pre-install gems
COPY Gemfile* ./
RUN gem install bundler && bundle install --jobs=3 --retry=3 

# Copy app files
COPY . .
RUN chmod -R 755 $RAILS_ROOT/bin

# Run server
EXPOSE 3000
CMD bundle exec rails s -b 0.0.0.0 -p 3000
© www.soinside.com 2019 - 2024. All rights reserved.