欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Timezone and Time on All Servers and Docker Containers

程序员文章站 2022-03-30 19:23:22
...
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://*.com/questions/24551592/how-to-make-sure-dockers-time-syncs-with-that-of-the-host
https://docs.docker.com/engine/reference/commandline/run/