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 setTZ
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"
参考
- Migrating from 1.0 to 2.0 - CircleCI
- Best way to set the timezone · Issue #12084 · moby/moby · GitHub
- glibcを更新しても大丈夫な「正しい」タイムゾーンの設定方法 (2/3追記あり) - めもおきば
- タイムゾーンの設定方法をメモ(RHEL6, RHEL7, Ubuntu編) | Siguniang's Blog
- dockerコンテナのタイムゾーンを簡単に設定する - 隙あらば寝る
- Setting timezone from terminal - Ask Ubuntu
- DockerのTime zoneをJSTに設定する |