我希望使用 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
并將 bundler 設定為在專案目錄中安裝 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.lockand./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?
uj5u.com熱心網友回復:
發生這種情況的原因很少:
- gem安裝目錄,在打包鏡像的bundler的配置中配置
BUNDLE_APP_CONFIGdocker 鏡像中定義的環境變數
查看 bundler 檔案,我們必須注意以下幾點:
- 可用密鑰串列中,
BUNDLE_PATH是不存在的,但是path是...
安裝指定 gem 的位置。
- 執行
config
執行
bundle config set --local <name> <value>將在本地應用程式的目錄中設定該配置。配置將存盤在<project_root>/.bundle/config. 如果BUNDLE_APP_CONFIG設定,配置將存盤在$BUNDLE_APP_CONFIG/config
- Bundler 按此順序加載配置設定
本地配置 (
<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
如你看到的:
- 捆綁器的配置存盤在您的本地目錄中
- 捆綁程式將 gems 安裝到您的本地目錄中
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/383905.html
