designetwork

ネットワークを軸としたIT技術メモ

Proxy環境での設定誤りでGem, Bundleインストールが失敗するケース

Proxy環境でのパッケージインストールは環境変数などでProxyサーバの指定が必要となる。Gem, Bundlerにおいても同様、http_proxy環境変数の設定が必要となる。

環境構築の中で、誤設定によりProxyが期待通りに動作しないケースがあったため、設定内容による動作の違いを確認した。

検証環境

今回の検証では2018/4/28時点のlatestのRuby Dockerイメージを使用した。

root@8271fa172878:/test# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
NAME="Debian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

root@8271fa172878:/test# ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]

root@8271fa172878:/test# gem -v
2.7.6

root@8271fa172878:/test# bundle -v
Bundler version 1.16.1

root@8271fa172878:/test# cat ./Gemfile
source 'https://rubygems.org'
gem 'haml'

(失敗) Proxy未設定

Proxy環境で、インターネット向けのDNSによる名前解決ができないケースでは、Proxyの環境変数が未設定の場合は、当然ながら以下のようにGemインストール、Bundleインストールに失敗する。

root@8271fa172878:/test# env | grep proxy

root@8271fa172878:/test# gem install haml
ERROR:  Could not find a valid gem 'haml' (>= 0), here is why:
          Unable to download data from https://rubygems.org/ - no such name (https://rubygems.org/specs.4.8.gz)

root@8271fa172878:/test# bundle install
Fetching source index from https://rubygems.org/

Retrying fetcher due to error (2/4): Bundler::HTTPError Could not fetch specs from https://rubygems.org/
Retrying fetcher due to error (3/4): Bundler::HTTPError Could not fetch specs from https://rubygems.org/
Retrying fetcher due to error (4/4): Bundler::HTTPError Could not fetch specs from https://rubygems.org/
Could not fetch specs from https://rubygems.org/

(成功) http_proxy環境変数設定

http_proxy環境変数が適切に設定されている場合は、以下のように成功する。

root@8271fa172878:/test# export http_proxy=http://192.168.1.76:8080

root@8271fa172878:/test# env | grep proxy
http_proxy=http://192.168.1.76:8080

root@8271fa172878:/test# bundle install
Fetching gem metadata from https://rubygems.org/....
Using bundler 1.16.1
Using temple 0.8.0
Using tilt 2.0.8
Fetching haml 5.0.4
Installing haml 5.0.4
Bundle complete! 1 Gemfile dependency, 4 gems now installed.
Bundled gems are installed into `/usr/local/bundle`

(失敗) no_proxyの誤設定

Proxy環境でもイントラは直接アクセスする場合などはno_proxy環境変数により、ドメインIPアドレス指定でProxy経由しないようにすることが多い。

今回失敗したケースでは、対象ドメインの追加を想定してexport no_proxy=$no_proxy,localhost,127.0.0.1として再帰的にno_proxyを設定した。

root@8271fa172878:/test# gem uninstall haml

root@8271fa172878:/test# export no_proxy=$no_proxy,localhost,127.0.0.1

root@8271fa172878:/test# env | grep proxy
http_proxy=http://192.168.1.76:8080
no_proxy=,localhost,127.0.0.1

root@8271fa172878:/test# bundle install
Fetching gem metadata from https://rubygems.org/....
Using bundler 1.16.1
Using temple 0.8.0
Using tilt 2.0.8
Fetching haml 5.0.4

Retrying download gem from https://rubygems.org/ due to error (2/4): Gem::RemoteFetcher::UnknownHostError no such name (https://rubygems.org/gems/haml-5.0.4.gem)
Retrying download gem from https://rubygems.org/ due to error (3/4): Gem::RemoteFetcher::UnknownHostError no such name (https://rubygems.org/gems/haml-5.0.4.gem)
Retrying download gem from https://rubygems.org/ due to error (4/4): Gem::RemoteFetcher::UnknownHostError no such name (https://rubygems.org/gems/haml-5.0.4.gem)
Gem::RemoteFetcher::UnknownHostError: no such name (https://rubygems.org/gems/haml-5.0.4.gem)
An error occurred while installing haml (5.0.4), and Bundler cannot continue.
Make sure that `gem install haml -v '5.0.4'` succeeds before bundling.

In Gemfile:
  haml

root@8271fa172878:/test# gem install haml
ERROR:  Could not find a valid gem 'haml' (>= 0), here is why:
          Unable to download data from https://rubygems.org/ - no such name (https://rubygems.org/specs.4.8.gz)

Bundlerによる依存関係の確認は成功しているが、Gemパッケージの取得時にGem::RemoteFetcher::UnknownHostError: no such nameのエラーで失敗している。個別にGemインストールを試行しても同様に名前解決のエラーで失敗する。

厄介なのは、BundlerはProxy設定を読み込んで動作できているのに、GemでProxyが動作しないこと。

no_proxy構文エラーによりProxyがセットされない

上記の原因はこちらでもやり取りされている通り、no_proxyの構文エラーによるもので、その場合はGem Proxyがセットされず、直接アクセスになりGemインストールに失敗する。

github.com

(NG) no_proxy=,localhost,127.0.0.1
(OK) no_proxy=localhost,127.0.0.1

(成功) no_proxyを適切に設定

no_proxyを適切に設定することで、以下の通り期待通りにGemインストールが可能となる。

root@8271fa172878:/test# export no_proxy=localhost,127.0.0.1

root@8271fa172878:/test# env | grep proxy
http_proxy=http://192.168.1.76:8080
no_proxy=localhost,127.0.0.1

root@8271fa172878:/test# bundle install
Fetching gem metadata from https://rubygems.org/....
Using bundler 1.16.1
Using temple 0.8.0
Using tilt 2.0.8
Fetching haml 5.0.4
Installing haml 5.0.4
Bundle complete! 1 Gemfile dependency, 4 gems now installed.
Bundled gems are installed into `/usr/local/bundle`

まとめ - Proxy環境での設定誤りでGem, Bundleインストールが失敗するケース

no_proxyの誤設定によりProxy環境でのGemインストールに失敗するケースが確認できた。Proxy環境変数は適切に設定することが重要だと改めて実感した。