`
sillycat
  • 浏览: 2486612 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Timezone and Time on All Servers and Docker Containers

 
阅读更多
Timezone and Time on All Servers and Docker Containers

First Of All - timezone
For the operation system CentOS
List the timezone in America
> sudo timedatectl list-timezones | grep America

Austin is the same as Chicago
> sudo timedatectl set-timezone America/Chicago
> date
date
Wed Oct  9 21:24:36 CDT 2019


For PHP
>  cat /etc/php.ini
date.timezone=America/Chicago
Support Timezone in America
https://www.php.net/manual/en/timezones.america.php
https://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone

For JAVA
-Duser.timezone=America/Chicago

Sync the clock
> sudo yum install ntp ntpdate
> sudo systemctl start ntpd
> sudo systemctl enable ntpd
> sudo systemctl status ntpd

If we do all these to the host machine, we should be good. Then we need to make all the Docker Container use the host machine timezone and time.
I start my Redis Docker Container for example
> docker ps | grep redis
e3b5adc41220        sillycat/public:sillycat-redisservice     "./start.sh"             11 seconds ago      Up 10 seconds       0.0.0.0:6379->6379/tcp                     sillycat-redisservice

Enter the docker container machine
> docker exec -ti e3b5adc41220 /bin/bash
> date
Thu Oct 10 02:59:51 UTC 2019

It is different from the host machine.
In the command line, do something like this
run:
        docker run -d -p 6379:6379 -v /etc/timezone:/etc/timezone:ro -v /etc/localtime:/etc/localtime:ro --name $(NAME) $(IMAGE):$(TAG)

Or configure in the yml file in docker-compose or something
volumes:
  - "/etc/timezone:/etc/timezone:ro"
  - "/etc/localtime:/etc/localtime:ro"

It will be exactly the same as the host machine
> date
Wed Oct  9 22:05:23 CDT 2019

References:
https://stackoverflow.com/questions/24551592/how-to-make-sure-dockers-time-syncs-with-that-of-the-host
https://docs.docker.com/engine/reference/commandline/run/
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics