本文发自 http://www.binss.me/blog/solve-the-connection-refused-problem-when-using-gem-install/,转载请注明出处。

最近查API有点频繁,导致Dash每次的查询时需要等待10秒的频率指数式增长,最后变成每查一次都要等10秒,真是急死人,免费版用户果然没人权。一怒之下换用开源的DevDocs。

为了方便,我使用了作者提供的Dockerfile构件镜像,结果构建到这一步挂了

Step 8 : RUN bundle install --system
 ---> Running in eaf91a8d8041
Don't run Bundler as root. Bundler can ask for sudo if it is needed, and
installing your bundle as root will break this application for all non-root
users on this machine.
Fetching gem metadata from https://rubygems.org/...........
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Using rake 10.4.2

Gem::RemoteFetcher::FetchError: Errno::ECONNREFUSED: Connection refused - connect(2) for "rubygems.global.ssl.fastly.net" port 443 (https://rubygems.org/gems/i18n-0.7.0.gem)
An error occurred while installing i18n (0.7.0), and Bundler cannot continue.
Make sure that `gem install i18n -v '0.7.0'` succeeds before bundling.
The command '/bin/sh -c bundle install --system' returned a non-zero code: 5

由于之前很少折腾Ruby,感觉不明觉历,于是手动构建了一个容器逐步执行Dockerfile内容,最后还是挂在这一句:

Gem::RemoteFetcher::FetchError: SSL_connect SYSCALL returned=5 errno=0 state=SSLv2/v3 read server hello A (https://rubygems.global.ssl.fastly.net/gems/i18n-0.7.0.gem)
An error occurred while installing i18n (0.7.0), and Bundler cannot continue.
Make sure that `gem install i18n -v '0.7.0'` succeeds before bundling.

按照提示,尝试gem install i18n -v '0.7.0':

root@4c1e62ed5408:/devdocs# gem install i18n -v '0.7.0' --source http://rubygems.org
ERROR:  While executing gem ... (Gem::RemoteFetcher::FetchError)
    SSL_connect SYSCALL returned=5 errno=0 state=SSLv2/v3 read server hello A (https://rubygems.global.ssl.fastly.net/quick/Marshal.4.8/i18n-0.7.0.gemspec.rz)

看上去像SSL出错的,于是开始了漫长的Google and Try,使用了各种姿势却依然无法解决,折腾了一个多小时。

最后灵光一闪,妈蛋该不会是被墙了吧(容器没装梯子)?

改个源:

bundle config mirror.https://rubygems.org https://ruby.taobao.org

然后一大串字符滚过:

root@4c1e62ed5408:/devdocs# bundle install --system
Don't run Bundler as root. Bundler can ask for sudo if it is needed, and installing your bundle as root will
break this application for all non-root users on this machine.
Fetching gem metadata from https://ruby.taobao.org/...........
Fetching version metadata from https://ruby.taobao.org/..
Using rake 10.4.2
Installing i18n 0.7.0
Installing json 1.8.3 with native extensions
Installing minitest 5.8.3
........
Bundle complete! 25 Gemfile dependencies, 46 gems now installed.

啊,啊,啊,西湖的水我的泪。中国的码农好艰难。