当 运行 Docker 时,git 源尚未签出

The git source is not yet checked out when running Docker

我目前在尝试使用 GitHub 中的 gem 时遇到错误。我的 Gemfile 中有以下内容:

# Gemfile
source 'https://rubygems.org'
ruby '2.3.1'

gem 'sinatra'
gem 'rack'
gem 'puma'

group :development do
  gem 'byebug'
  gem 'rack-test'
  gem 'rerun', github: 'alexch/rerun', branch: 'master'
end

当我从 Dockerfile 运行 bundle install 时,它会处理如下消息:

...
Installing sinatra 1.4.6
Installing listen 3.1.5 (was 3.0.6)
Using rerun 0.11.0 from git://github.com/alexch/rerun.git (at master@3e4c486)
Bundle complete! 6 Gemfile dependencies, 14 gems now installed.
...

但是,当我启动容器时,我得到:

The git source git://github.com/alexch/rerun.git is not yet checked out. Please run `bundle install` before trying to start your application

我见过类似的问题,但与 Docker 无关。

原来我找到了问题所在。问题与我的 Gemfile 用于构建 Docker 图像有关。

我没有本地 运行 bundle install 命令导致 Gemfile.lock 没有被更新。一旦我 运行 将以下命令添加到我的 Gemfile.lock:

@@ -1,14 +1,20 @@
+GIT
+  remote: git://github.com/alexch/rerun.git
+  revision: 3e4c486304be406cb86180ef70ec24e9ae055ce4
+  branch: master
+  specs:
+    rerun (0.11.0)
+      listen (~> 3.0)

原来这就是我所需要的。一旦我有了更新的文件,就重建图像和 运行 容器,一切都按预期工作 bundle exec rerun。所以要注意的是,我正在使用 Gemfile.lock 来帮助缓存/版本锁定,但未能通过我的 Gemfile 保持更新。

对我有用:

docker-compose run web bundle install

万一有人感兴趣,还有其他选择。

在映像创建期间,如果您的 docker 文件运行 bundle install,它将在容器中生成一个 Gemfile.lock。您可以使用 docker 运行 获取内容,例如

docker run web cat Gemfile.lock

您可以复制内容并保存一个Gemfile.lock到您的项目目录。