高木のブログ

【Docker】Rails Console で日本語を入力すると落ちる

2023/04/13

問題

Docker コンテナ内で、Rails Consoleを起動して、日本語を入力しようとしたら落ちた

[1] pry(main)> '���'
/usr/local/lib/ruby/gems/2.0.0/gems/pry-0.10.1/lib/pry/history.rb:106:in `write': "\xE3" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError)
        from /usr/local/lib/ruby/gems/2.0.0/gems/pry-0.10.1/lib/pry/history.rb:106:in `puts'
        from /usr/local/lib/ruby/gems/2.0.0/gems/pry-0.10.1/lib/pry/history.rb:106:in `save_to_file'
        from /usr/local/lib/ruby/gems/2.0.0/gems/pry-0.10.1/lib/pry/history.rb:50:in `call'
        from /usr/local/lib/ruby/gems/2.0.0/gems/pry-0.10.1/lib/pry/history.rb:50:in `push'
        from /usr/local/lib/ruby/gems/2.0.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:267:in `handle_line'
        from /usr/local/lib/ruby/gems/2.0.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval'
        from /usr/local/lib/ruby/gems/2.0.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:242:in `catch'
        from /usr/local/lib/ruby/gems/2.0.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:242:in `block in eval'
        from /usr/local/lib/ruby/gems/2.0.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:241:in `catch'
        from /usr/local/lib/ruby/gems/2.0.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:241:in `eval'
        from /usr/local/lib/ruby/gems/2.0.0/gems/pry-0.10.1/lib/pry/repl.rb:77:in `block in repl'
        from /usr/local/lib/ruby/gems/2.0.0/gems/pry-0.10.1/lib/pry/repl.rb:67:in `loop'
        from /usr/local/lib/ruby/gems/2.0.0/gems/pry-0.10.1/lib/pry/repl.rb:67:in `repl'
        from /usr/local/lib/ruby/gems/2.0.0/gems/pry-0.10.1/lib/pry/repl.rb:38:in `block in start'
        from /usr/local/lib/ruby/gems/2.0.0/gems/pry-0.10.1/lib/pry/input_lock.rb:61:in `call'
        from /usr/local/lib/ruby/gems/2.0.0/gems/pry-0.10.1/lib/pry/input_lock.rb:61:in `__with_ownership'
        from /usr/local/lib/ruby/gems/2.0.0/gems/pry-0.10.1/lib/pry/input_lock.rb:79:in `with_ownership'
        from /usr/local/lib/ruby/gems/2.0.0/gems/pry-0.10.1/lib/pry/repl.rb:38:in `start'
        from /usr/local/lib/ruby/gems/2.0.0/gems/pry-0.10.1/lib/pry/repl.rb:15:in `start'
        from /usr/local/lib/ruby/gems/2.0.0/gems/pry-0.10.1/lib/pry/pry_class.rb:169:in `start'
        from /usr/local/lib/ruby/gems/2.0.0/gems/pry-nav-0.2.4/lib/pry-nav/pry_ext.rb:17:in `start_with_pry_nav'
        from /usr/local/lib/ruby/gems/2.0.0/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
        from /usr/local/lib/ruby/gems/2.0.0/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
        from /usr/local/lib/ruby/gems/2.0.0/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'

解決方法

環境変数でマルチバイト文字列を使うようにしたら良い

Dockerfile に以下を追加して解決

Dockerfile
ENV LANG ja_JP.UTF-8
[1] pry(main)> 'あ'
=> "あ"

参考

dockerのrailsで、rails console から日本語を入力すると落ちます


SNS でシェアする


ytkg

Written by ytkg, Twitter, GitHub