我正在使用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的更改?我在这里错过了什么吗?
docker-compose run
每次调用时都会创建一个新容器,您的更改不会持久存在。
如果您希望更改保持不变,请使用docker-compose exec
,它在正在运行的容器中运行您的命令。
每个docker-compose run
都在开始一个新容器。运行它两次,然后运行docker ps -a
,你会看到两个已退出的容器。
您需要在bundle install
中运行Dockerfile
作为图像构建过程的一部分。
作为旁注提示,通常的做法是首先复制Gemfile
和Gemfile.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