0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Docker] コンテナ内で日本語を表示すると文字化けする時の対策

Posted at

症状

Docker container 内で git diff を実行したりして日本語を表示すると、以下のように文字化けが発生してしまいました。

<E3><82><92>...などと表示されているところが、元々は日本語の部分です。

# git diff
diff --git a/hello/src/samples/types.rs b/hello/src/samples/types.rs
index 8ff661c..df045af 100644
--- a/hello/src/samples/types.rs
+++ b/hello/src/samples/types.rs
@@ -1,3 +1,5 @@
+use std::io;
+
 use std::collections::HashMap;
 
 fn type_of<T>(_: &T) -> &'static str {
@@ -102,6 +104,13 @@ pub fn type_sample() {
     println!("text_length: {text_length:?}");
     println!("still can print text: {text:?}");
 
+    // https://doc.rust-lang.org/std/borrow/trait.ToOwned.html#tymethod.to_owned
+    // to_owned() <E3><81><AF> Option<&T> <E3><82><92> Option<T::Owned> <E3><81><AB>
<E5><A4><89><E6><8F>E3><81><99><E3><82><8B>
+    // Option<&T> to Option<T::Owned>
+
+    // https://zenn.dev/asamin/articles/6a9c29a89a8064
+    // Rust<E3><81><AE>unwrap<E3><81><A8>?<E3><81><AE><E9><81><95><E3><81><84>
+
     // https://doc.rust-lang.org/std/option/enum.Option.html#method.as_deref
     // Option<T> (or &Option<T>) to Option<&T::Target>
     println!("Some('hey'): {:?}", type_of(&Some("hey"))); // "core::option::Option<&str>"
@@ -187,6 +196,21 @@ pub fn type_sample() {
     let code = error_handling(result);
     println!("Result5-2: {:?}", code);
 
+    // ? <E3><82><92><E4><BD><BF><E3><81><A3><E3><81><A6><E7><B5><90><E6><9E><9C><E3>
<82><92><E5><8F><96><E3><82><8A><E5><87><BA><E3><81><99>
+    fn extract_from_result<T>(
+        opt: Result<T, io::Error>,
+    ) -> Result<T, io::Error> {
+        let content: T = opt?;
+        return Ok(content);
+    }
+
+    fn main() {
+        let res: Result<String, io::Error> = Ok("hey".to_owned());
+
+        let content: Result<String, io::Error> = extract_from_result(res);
+        println!("{:?}", content.unwrap() + "hoge");
+    }
+
     /* -------------------------
     Vec <E3><81><AE><E4><BD><BF><E3><81><84><E6><96><B9>
     ------------------------- */
@@ -231,6 +255,14 @@ pub fn type_sample() {
     // Box::new <E3><81><A7> Box<[u8]> <E5><9E><8B><E3><81><AB><E5><A4><89><E6><8F>�
E3><81><99><E3><82><8C><E3><81><B0><E8><A6><81><E7><B4><A0><E6><95><B0><E6><9C><AA>
<E5><AE><9A><E3><81><A7><E3><82><82>OK
     print(Box::new(byte_array));
 
+    // Box <E3><81><AE><E4><B8><AD><E8><BA><AB><E3><81><AE><E5><8F><96><E3><82><8A>
<E5><87><BA><E3><81><97><E3><80><81><E5><A4><89><E6>B4>
+    let x = Box::new(5);
+    let mut y = Box::new(3);
+    // * <E3><82><92><E4><BD><BF><E3><81><86><E3><81><A8><E3><80><81> Box <E3><81>
<AE><E4><B8><AD><E8><BA><AB><E3><82><92><E5><8F><96><E3><82><8A><E5><87><BA><E3><81>
<97><E3><81><9F><E3><82><8A><E3><80><81><E4><B8><AD><E8><BA><AB><E3><82><92><E4><B8>
<8A><E6>B8><E3><81><8D><E3><81><97><E3><81><9F><E3><82><8A><E3><81><A7><E3><81><8D>
<E3><82><8B>.
+    *y = 4;
+    assert_eq!(*x, 5);
+    println!("Success!");
+
     /* -------------------------
     Range
     ------------------------- */

対処法

具体的なコマンドや対処方針は以下の記事に書かれています。

が、 Dockerfile にそれを反映させる方法までは具体的には書かれていないので、本記事ではそこを記載します。

ソースコード (Dockerfile)

Dockerfile に、以下のように記述を追加してください。

Dockerfile
# locales を install
RUN apt-get update && \
    apt-get -y install locales

# 日本語 locale を生成
RUN echo "ja_JP UTF-8" > /etc/locale.gen && locale-gen

# 生成した locale をセット
ENV LANG=ja_JP.UTF-8

この状態で image をビルドし直すと、日本語の文字化けを防げます!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?