問題

Dockerで作ったRuby環境でbundle installしたときにinvalid byte sequence in US-ASCII (ArgumentError)というエラーが出た。

bundle install --without production --retry=3 --path vendor/bundle
/bin/bash: warning: setlocale: LC_ALL: cannot change locale (en_US.utf-8)
Your Gemfile lists the gem byebug (>= 0) more than once.
You should probably keep only one of them.
While it's not a problem now, it could cause errors if you change the version of one of them later.
Warning: the running version of Bundler (1.16.0) is older than the version that created the lockfile (1.16.1). We suggest you upgrade to the latest version of Bundler by running `gem install bundler`.
/usr/local/lib/ruby/site_ruby/2.4.0/bundler/dsl.rb:586:in `parse_line_number_from_description': invalid byte sequence in US-ASCII (ArgumentError)
	from /usr/local/lib/ruby/site_ruby/2.4.0/bundler/dsl.rb:552:in `to_s'
	from /usr/local/lib/ruby/site_ruby/2.4.0/bundler/friendly_errors.rb:17:in `to_s'
	from /usr/local/lib/ruby/site_ruby/2.4.0/bundler/friendly_errors.rb:17:in `message'
	from /usr/local/lib/ruby/site_ruby/2.4.0/bundler/friendly_errors.rb:17:in `log_error'
	from /usr/local/lib/ruby/site_ruby/2.4.0/bundler/friendly_errors.rb:126:in `rescue in with_friendly_errors'
	from /usr/local/lib/ruby/site_ruby/2.4.0/bundler/friendly_errors.rb:122:in `with_friendly_errors'
	from /usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.16.0/exe/bundle:22:in `<top (required)>'
	from /usr/local/bin/bundle:23:in `load'
	from /usr/local/bin/bundle:23:in `<main>'
Error: Exited with code 1
Step failed
Task failed

解決策

Dockerfile内に下記を記述する。

ENV RUBYOPT -EUTF-8

調べたこと

Dockerhub上のRubyのimageはデフォルトのエンコーディングがUS-ASCIIになっている。

$ docker run -it ruby:2.5.0 bin/bash
root@799ea38705df:/# ruby -e 'puts Encoding.default_external'
US-ASCII

なので、上記のエラーが出る場合があると。
ちなみにMacにrbenv経由で入れたRubyの場合、デフォルトのエンコーディングはUTF-8になっている。

$ ruby -e 'puts Encoding.default_external'
UTF-8

export RUBYOPT=-EUTF-8すればエンコーディングを変更できる。

$ docker run -it ruby:2.5.0 bin/bash
root@799ea38705df:/# export RUBYOPT=-EUTF-8
root@799ea38705df:/# ruby -e 'puts Encoding.default_external'
UTF-8

参考