使用 Docker 图像进行 运行 捆绑安装会忽略捆绑配置设置

Using a Docker image to run bundle install ignores bundle config setting

我希望使用 Docker 映像 (ruby:3.0) 构建映像,而不必(最终)在本地安装 Ruby。

出于测试目的,我在 Windows 10 WSL2 环境中安装了 Ruby 2.7.0:

$ ruby -v
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux-gnu]

我有一个 Gemfile:

来源'https://rubygems.org'

gem 'sinatra'

group :development, :test do
    gem 'thin'
    gem 'sqlite3'
end

并且捆绑器设置为在项目目录中安装 Gems:

$ cat .bundle/config
---
BUNDLE_PATH: "vendor/bundle"

如果我在本地 运行 bundle install:

$ bundle install
Fetching gem metadata from https://rubygems.org/...
Resolving dependencies...
Using bundler 2.2.17
Fetching rack 2.2.3
Fetching tilt 2.0.10
Fetching daemons 1.4.1
Fetching sqlite3 1.4.2
Fetching eventmachine 1.2.7
Fetching ruby2_keywords 0.0.5
Installing rack 2.2.3
Installing tilt 2.0.10
Installing sqlite3 1.4.2 with native extensions
Installing ruby2_keywords 0.0.5
Installing daemons 1.4.1
Installing eventmachine 1.2.7 with native extensions
Fetching mustermann 1.1.1
Installing mustermann 1.1.1
Fetching rack-protection 2.1.0
Installing rack-protection 2.1.0
Fetching sinatra 2.1.0
Installing sinatra 2.1.0
Fetching thin 1.8.1
Installing thin 1.8.1 with native extensions
Bundle complete! 3 Gemfile dependencies, 11 gems now installed.
Bundled gems are installed into `./vendor/bundle`

Gems 安装到 ./vendor/bundle 目录:

确认:

$ bundle info thin
  * thin (1.8.1)
        Summary: A thin and fast web server
        Homepage: https://github.com/macournoyer/thin
        Source Code: https://github.com/macournoyer/thin
        Changelog: https://github.com/macournoyer/thin/blob/master/CHANGELOG
        Path: /home/craig/ruby/dev/vendor/bundle/ruby/2.7.0/gems/thin-1.8.1

接下来,我使用 ruby:3.0 图像将 Gems 打包到项目目录中。

我删除了 Gemfile.lock./vendor/bundle 目录,然后 运行 bundle install 使用图像:

$ docker run --rm -v "$(pwd)":/src -w /src ruby:3.0 bundle install
Fetching gem metadata from https://rubygems.org/...
Resolving dependencies...
Using bundler 2.2.32
Fetching sqlite3 1.4.2
Fetching eventmachine 1.2.7
Fetching rack 2.2.3
Fetching tilt 2.0.10
Fetching ruby2_keywords 0.0.5
Fetching daemons 1.4.1
Installing ruby2_keywords 0.0.5
Installing tilt 2.0.10
Installing daemons 1.4.1
Installing sqlite3 1.4.2 with native extensions
Installing rack 2.2.3
Fetching mustermann 1.1.1
Installing eventmachine 1.2.7 with native extensions
Installing mustermann 1.1.1
Fetching rack-protection 2.1.0
Installing rack-protection 2.1.0
Fetching sinatra 2.1.0
Installing sinatra 2.1.0
Fetching thin 1.8.1
Installing thin 1.8.1 with native extensions
Bundle complete! 3 Gemfile dependencies, 11 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

这个过程似乎安装了 Gems,但没有安装到 ./vendor/bundle。尝试识别位置会产生错误:

$ bundle info thin
Could not find daemons-1.4.1 in any of the sources

似乎 .bundle/config 被忽略了。我猜 Gems 实际上被安装到为 运行 bundle install.

创建的容器中

有没有办法让它使用 .bundle/config

发生这种情况的原因有以下几点:

  1. gem安装目录,在bundler的configuration中配置,与image一起打包
  2. BUNDLE_APP_CONFIG docker 图像中定义的环境变量

查看打包器文档,我们必须注意以下几点:

  1. list of available keysBUNDLE_PATH 不存在,但是 path 是...

The location to install the specified gems to.

  1. executing config

Executing bundle config set --local <name> <value> will set that configuration in the directory for the local application. The configuration will be stored in <project_root>/.bundle/config. If BUNDLE_APP_CONFIG is set, the configuration will be stored in $BUNDLE_APP_CONFIG/config

  1. Bundler 按此顺序加载配置设置

Local config (<project_root>/.bundle/config ...)

让我们试一试...

$ docker run --rm -it -v $PWD:/src --workdir /src --entrypoint /bin/sh ruby:alpine

/src # ls -l
total 0

/src # echo $BUNDLE_APP_CONFIG
/usr/local/bundle

/src # export BUNDLE_APP_CONFIG=$PWD/.bundler

$ bundle config set --local path 'vendor/bundle'
/src # ls -la
total 4
drwxr-xr-x    3 root     root            96 Dec 16 21:10 .
drwxr-xr-x    1 root     root          4096 Dec 16 21:10 ..
drwxr-xr-x    3 root     root            96 Dec 16 21:10 .bundler

/src # bundle config
Settings are listed in order of priority. The top value will be used.
app_config
Set via BUNDLE_APP_CONFIG: "/src/.bundler"

path
Set for your local app (/src/.bundler/config): "vendor/bundle"

silence_root_warning
Set via BUNDLE_SILENCE_ROOT_WARNING: true

/src # bundle init
Writing new Gemfile to /src/Gemfile

/src # echo 'gem "sinatra"' >> Gemfile

/src # bundle install --quiet

/src # ls -l
total 8
-rw-r--r--    1 root     root           161 Dec 16 21:12 Gemfile
-rw-r--r--    1 root     root           398 Dec 16 21:13 Gemfile.lock
drwxr-xr-x    3 root     root            96 Dec 16 21:18 vendor

/src # du -sh vendor/bundle/
3.1M    vendor/bundle/

/src # bundle exec rackup --version
Rack 1.3 (Release: 2.2.3)

/src # exit

$ ls -ax1
.
..
.bundler
Gemfile
Gemfile.lock
vendor

如你所见:

  1. bundler 的配置存储在您的本地目录中
  2. bundler 安装 gems 到您的本地目录