DockerでrbenvによるRubyのインストールが失敗する話

Dockerで立ち上げたコンテナでrbenvによるRubyのインストールが失敗することがあり、その原因を調べた。

概要

以下のように、インストール以前にソースのダウンロードに失敗していた。

$ rbenv install 2.3.0
Downloading ruby-2.3.0.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.0.tar.bz2
error: failed to download ruby-2.3.0.tar.bz2
...

試しにcurlでアクセスしてみたところ、以下のように証明書関係でエラーが出た。

$ curl https://cache.ruby-lang.org
curl: (60) SSL certificate problem: certificate is not yet valid
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle"
 of Certificate Authority (CA) public keys (CA certs). If the default
 bundle file isn't adequate, you can specify an alternate file
 using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
 the bundle, the certificate verification probably failed due to a
 problem with the certificate (it might be expired, or the name might
 not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
 the -k (or --insecure) option.
$ 

原因

エラーの原因は、コンテナ内の時刻がずれていたことだった。
ntpdでも入れればよいのかと思ったが、Dockerの場合そもそもホスト側の時刻に合わせてコンテナが起動していたような気がしたので、ホスト側の時刻を合わせることにした。

Docker for Macを用いていたため、単純に再起動することでコンテナ内の時刻も正常に戻った。
どうやらDocker for Macは起動させたままにしておくと、少しずつ時刻がずれていくようだった。

まとめ

Docker for Macは定期的に再起動したほうが良さそう