From 0e963bd7cfbb47c4fa7b2d91753c86f3f7721f3b Mon Sep 17 00:00:00 2001 From: Eric Chang Date: Tue, 21 May 2019 17:44:34 +0800 Subject: [PATCH] add change-timezone-in-docker.md --- content/post/change-timezone-in-docker.md | 149 +++++ public/404.html | 10 +- public/author/eric-chang/index.html | 164 +++--- public/author/eric-chang/index.xml | 13 +- public/author/eric-chang/page/2/index.html | 167 +++--- public/author/eric-chang/page/3/index.html | 88 ++- public/author/index.html | 12 +- public/author/index.xml | 2 +- public/categories/index.html | 12 +- public/categories/index.xml | 2 +- public/categories/linux/index.html | 10 +- public/categories/ps/index.html | 10 +- public/categories/碎念/index.html | 10 +- public/categories/筆記/index.html | 178 +++--- public/categories/筆記/index.xml | 13 +- public/categories/筆記/page/2/index.html | 103 +++- public/categories/群暉/index.html | 10 +- public/gallery/index.html | 10 +- public/gallery/sammy93/index.html | 10 +- public/index.html | 168 +++--- public/index.json | 7 + public/index.xml | 13 +- public/page/2/index.html | 171 +++--- public/page/3/index.html | 90 ++- .../index.html | 10 +- .../post/ansible-selectattr-filter/index.html | 10 +- public/post/ansible-selectattr/index.html | 10 +- public/post/bookstack-docker/index.html | 10 +- .../index.html | 10 +- .../post/change-timezone-in-docker/index.html | 520 ++++++++++++++++++ .../post/command_to_test_main_ssl/index.html | 10 +- public/post/copy_role_in_pgsql/index.html | 10 +- .../index.html | 10 +- .../enable-synology-public-ssh/index.html | 10 +- .../index.html | 10 +- .../incredibly-slow-mdadm-rebuild/index.html | 10 +- public/post/index.html | 164 +++--- public/post/index.xml | 13 +- .../index.html | 10 +- .../index.html | 10 +- .../index.html | 10 +- public/post/log-all-bash-commands/index.html | 10 +- .../post/nice-du-report-tool-durep/index.html | 10 +- public/post/page/2/index.html | 167 +++--- public/post/page/3/index.html | 88 ++- .../index.html | 10 +- .../synology-ds415-repair-cost/index.html | 10 +- .../transfer-cent62-using-rsync/index.html | 10 +- .../index.html | 10 +- .../index.html | 10 +- .../weird-client-server-connection/index.html | 10 +- public/sitemap.xml | 2 +- public/tags/10g/index.html | 10 +- public/tags/ansible/index.html | 10 +- public/tags/backup/index.html | 10 +- public/tags/bookstack/index.html | 10 +- public/tags/bsd/index.html | 10 +- public/tags/centos/index.html | 10 +- public/tags/docker/index.html | 85 ++- public/tags/docker/index.xml | 13 +- public/tags/du/index.html | 10 +- public/tags/firefox/index.html | 10 +- public/tags/freenas/index.html | 10 +- public/tags/index.html | 14 +- public/tags/index.xml | 11 +- public/tags/inventory/index.html | 10 +- public/tags/linux/index.html | 10 +- public/tags/log/index.html | 10 +- public/tags/mdadm/index.html | 10 +- public/tags/mellanox/index.html | 10 +- public/tags/metabase/index.html | 10 +- public/tags/nas/index.html | 10 +- public/tags/ps/index.html | 10 +- public/tags/psql/index.html | 10 +- public/tags/raid/index.html | 10 +- public/tags/ssh/index.html | 10 +- public/tags/synology/index.html | 10 +- public/tags/timezone/index.html | 386 +++++++++++++ public/tags/timezone/index.xml | 25 + public/tags/timezone/page/1/index.html | 1 + public/tags/ubuntu/index.html | 10 +- public/tags/vim/index.html | 10 +- public/tags/zfs/index.html | 10 +- public/tags/短今/index.html | 10 +- public/tags/筆記/index.html | 10 +- public/tags/群暉/index.html | 10 +- public/tw/sitemap.xml | 21 +- 87 files changed, 2482 insertions(+), 940 deletions(-) create mode 100644 content/post/change-timezone-in-docker.md create mode 100644 public/post/change-timezone-in-docker/index.html create mode 100644 public/tags/timezone/index.html create mode 100644 public/tags/timezone/index.xml create mode 100644 public/tags/timezone/page/1/index.html diff --git a/content/post/change-timezone-in-docker.md b/content/post/change-timezone-in-docker.md new file mode 100644 index 00000000..d437ca91 --- /dev/null +++ b/content/post/change-timezone-in-docker.md @@ -0,0 +1,149 @@ +--- +title: "[筆記] 修改 docker 容器內的時區 - Change Timezone in Docker" +date: 2019-05-21T17:25:15+08:00 + +noSummary: false +featuredImage: "https://h.cowbay.org/images/post-default-3.jpg" +categories: ['筆記'] +tags: ['docker','timezone'] +author: "Eric Chang" +--- + +最近一直在玩一些docker,不過老是會碰到歪果扔寫的東西,時區都不一致 + +有的用 UTC,有的用localtime,就是沒碰到用 Asia/Taipei 的.... + + +之前因為沒有要上線,所以這個問題可以當烏龜忽略過去 + +不過測試了一陣子的librenms 打算正式上線了 + +又不想重作一次,導致累積滿久的圖表資料都消失 + +所以開始找尋方法來面對這個問題 + +本來看了這篇 https://www.arthurtoday.com/2016/07/how-to-setup-docker-container-timezone-host.html + +想說來試試看好了 + +不過呢,一開始依照這篇的說明,在docker-compose.yml 內加入 +``` +web: + image: jarischaefer/docker-librenms + hostname: librenms + ports: + - "8001:80" + - "44301:443" + volumes: + - /etc/hosts:/etc/hosts + - /etc/localtime:/etc/localtime:ro + environment: + - TZ="Asia/Taipei" +``` + +重起之後沒作用 0rz + +於是我又加入了 + +``` + volumes: + - /etc/hosts:/etc/hosts + - /etc/localtime:/etc/localtime:ro + - /etc/timezone:/etc/timezone:ro +``` + +結果啟動直接報錯誤了.. + +看一下 log + +``` +May 21 09:09:12 bbs012 syslog-ng[12]: syslog-ng starting up; version='3.13.2' +*** Running /etc/my_init.d/librenms_100_cron... +*** Running /etc/my_init.d/librenms_101_ssl... +*** Running /etc/my_init.d/librenms_102_ipv6... +*** Running /etc/my_init.d/librenms_103_timezone... +/etc/my_init.d/librenms_103_timezone: line 4: /etc/timezone: Read-only file system +*** /etc/my_init.d/librenms_103_timezone failed with status 1 + +*** Killing all processes... +``` + +所以這個檔案不能改成 ro ,不過如果不能唯獨,那啟動docker的時候應該會被蓋掉吧? + +先來看一下那個 librenms_103_timezone 在幹什麼好了 + +``` +docker exec -it librenms_web_1 cat /etc/my_init.d/librenms_103_timezone +#!/bin/bash -e + +if [ -n "$TZ" ]; then + ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone + + if [ ! -f /etc/php/7.3/cli/conf.d/100-timezone.ini ]; then + echo "date.timezone=$TZ" > /etc/php/7.3/cli/conf.d/100-timezone.ini + fi + + if [ ! -f /etc/php/7.3/fpm/conf.d/100-timezone.ini ]; then + echo "date.timezone=$TZ" > /etc/php/7.3/fpm/conf.d/100-timezone.ini + fi +fi +2019-05-21 17:33:46 [mini@s013 librenms]$ +``` + +OK ,這裡的確是用傳進來的 $TZ 在設定timezone 沒錯 + +可是我前面已經改過 docker-compose.yml + +有把 TZ帶進來了啊?再來確認一下 + +``` +docker exec -it librenms_web_1 cat /etc/timezone +"Asia/Taipei" + +``` + +咦,沒錯啊? 欸斗,等等,那兩個 "" 有點刺眼... + +跟本機的比對一下看看 + +``` +2019-05-21 17:36:20 [mini@s013 librenms]$ docker exec -it librenms_web_1 cat /etc/timezone +"Asia/Taipei" +2019-05-21 17:36:23 [mini@s013 librenms]$ cat /etc/timezone +Asia/Taipei +2019-05-21 17:37:10 [mini@s013 librenms]$ +``` + +嗯,的確,本機的格式的確不包含那兩個 "" + +那就改掉再來試試看吧...改成底下這樣 + +``` + environment: + - TZ=Asia/Taipei + +``` + +重起 docker ,然後確認時間看看 + +``` +2019-05-21 17:39:00 [mini@s013 librenms]$ docker-compose down;docker-compose up -d +Stopping librenms_web_1 ... done +Stopping librenms_mysql_1 ... done +Removing librenms_web_1 ... done +Removing librenms_mysql_1 ... done +Removing network librenms_default +Creating network "librenms_default" with the default driver +Creating librenms_mysql_1 ... done +Creating librenms_web_1 ... done +2019-05-21 17:39:22 [mini@s013 librenms]$ docker exec -it librenms_web_1 cat /etc/timezone +Asia/Taipei +2019-05-21 17:39:42 [mini@s013 librenms]$ docker exec -it librenms_web_1 date +Tue May 21 17:39:48 CST 2019 +2019-05-21 17:39:48 [mini@s013 librenms]$ +``` + +OK ,果然沒有問題了! + +雖然是小小的"" ,還是要特別注意啊! + diff --git a/public/404.html b/public/404.html index 20276832..b9593594 100644 --- a/public/404.html +++ b/public/404.html @@ -115,6 +115,10 @@ @@ -153,7 +153,7 @@