ひだまりソケットは壊れない

ソフトウェア開発に関する話を書きます。 最近は主に Android アプリ、Windows アプリ (UWP アプリ)、Java 関係です。

まじめなことを書くつもりでやっています。 適当なことは 「一角獣は夜に啼く」 に書いています。

Docker コンテナなどの Linux 環境でタイムゾーンを指定する (TZ 環境変数)

やりたいこと

調べてよく引っ掛かるのは /etc/localtime (などのファイル) を変更する方法

Linux timezone」 みたいな検索ワードで検索すると、/etc/localtime (などのファイル) を書き換える方法が多く見つかる。

これらの方法は、システムのタイムゾーン設定を変更するもので、ディストリビューションごとに詳細な設定方法は違っていたりする。

Docker コンテナ内のプロセスのための設定などは TZ 環境変数を使うのが良さそう

別の方法として TZ 環境変数を設定するというものもある。 これは POSIX 標準の方法っぽい。

You should not normally need to set TZ. If the system is configured properly, the default time zone will be correct. You might set TZ if you are using a computer over a network from a different time zone, and would like times reported to you in the time zone local to you, rather than what is local to the computer.

とのことで、おそらく Docker コンテナ内のプロセスのためのタイムゾーン設定などは TZ を設定する方法が適していると思われる。

ちなみに設定する値については以下のように書かれている。

If characters begins with a slash, it is an absolute file name; otherwise the library looks for the file /usr/share/zoneinfo/characters. The zoneinfo directory contains data files describing local time zones in many different parts of the world.

つまり、TZ=Asia/Tokyo みたいに設定するか、TZ=/usr/share/zoneinfo/Asia/Tokyo みたいに設定するかのどちらかを選べる。

docker のコマンドオプションで設定する場合は、docker -e TZ=Asia/Tokyo -it --rm --name xxx image/xxx みたいな感じ。

CircleCI 2.0 のジョブ定義であれば、以下のようにして設定できる。

jobs:
  <job name>:
    environment:
      TZ: "/usr/share/zoneinfo/America/Los_Angeles"

参考