最近在使用Docker容器时,部署java程序发现时间输出不对,在修改问题时总结如下。

  #date [-R]                       #查看主机时间
  #timedatectl                           #查看主机时区
  #tzselect                         #选择时区,5 选择亚洲 > 9 选择中国时区 -> 1选择北京时间 -> 1 选择Yes

修改主机时区

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime             #上海时间
rm /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #上海时间

更新主机时间

date                                      #查看当前系统时间
yum install -y ntpdate #安装ntpdate程序
ntpdate cn.pool.ntp.org #更新系统时间
date #再次查看当前系统时间

网络时间服务器

ntp1.aliyun.com
ntp2.aliyun.com
ntp3.aliyun.com
ntp4.aliyun.com
ntp5.aliyun.com
ntp6.aliyun.com
ntp7.aliyun.com
.cn.pool.ntp.org
.cn.pool.ntp.org
.cn.pool.ntp.org
.cn.pool.ntp.org

同步BIOS时钟,强制把系统时间写入CMOS

clock --show                         #查看硬件时间
clock -w       #强制把系统时间写入CMOS
clock --show       #查看硬件时间
reboot       #重起机器

设置系统自动同步时间

vi /etc/crontab                            #设置定时任务

   * * ntpdate -s cn.pool.ntp.org      --每月一号同步
* */ * * * ntpdate -s cn.pool.ntp.org --每一个小时同步

Docker时间和宿主同步方法

  1.在run容器时添加参数挂载宿主时间配置:  -v /etc/localtime:/etc/localtime

  2.复制宿主localtime时间配置覆盖:  docker cp /etc/localtime container_id:/etc/localtime

  3.在启动jar包添加时区参数:  -Duser.timezone=GMT+08

Docker容器设置时区

  #docker exec -it container_id /bin/bash            #进入容器命令

  #修改时区,设置为上海时区
  ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  或者
  cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

Docker设置build参数

  ENV TZ=Asia/Shanghai
  RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

构建dockerfile镜像.

备注:
作者:Shengming Zeng
博客:http://www.cnblogs.com/zengming/
严正声明:
1.由于本博客部分资源来自互联网,版权均归原作者所有。转载的目的是用于学术交流与讨论学习,将不对任何资源负法律责任。
2.若无意中侵犯到您的版权利益,请来信联系我,我会在收到信息后会尽快给予处理!
3.所有资源内容仅供学习交流之用,请勿用作商业用途,谢谢。
4.如有转发请注明出处,来源于http://www.cnblogs.com/zengming/ ,谢谢合作。

主机、Docker时间与时区设置总结的更多相关文章

  1. Linux 时间及时区设置

    时间以及时区设置 by:授客 QQ:1033553122 1.首先确认使用utc还是local time. UTC(Universal Time Coordinated)=GMT(Greenwich ...

  2. Django 时间与时区设置问题

    django 时间与时区设置问题 在Django的配置文件settings.py中,有两个配置参数是跟时间与时区有关的,分别是TIME_ZONE和USE_TZ 如果USE_TZ设置为True时,Dja ...

  3. linux下的时间及时区设置

    一.时间设置及同步 1修改系统时间 #date -s 06/18/14 #date -s 14:20:50 2命令查看.设置硬件时间 #hwclock  --show 或者clock  --show ...

  4. Django时间与时区设置问题

    在Django的配置文件settings.py中,有两个配置参数是跟时间与时区有关的,分别是TIME_ZONE和USE_TZ 如果USE_TZ设置为True时,Django会使用系统默认设置的时区,即 ...

  5. linux 时间和时区设置

    在linux中与时间相关的文件有 /etc/localtime /etc/timezone 其中,/etc/localtime是用来描述本机时间,而 /etc/timezone是用来描述本机所属的时区 ...

  6. Android实践 -- 设置系统日期时间和时区

    设置系统日期时间和时区 设置系统的日期时间和时区,需要 系统权限和系统签名,android:sharedUserId="android.uid.system" 需要在manifes ...

  7. linux下jenkins的时区设置问题

    https://blog.csdn.net/king_wang10086/article/details/76178711 [root@jenkins ~]# yum install -y ntpda ...

  8. docker 时区设置

    今天查问题的时候发现 在对时间 格式化为 时间戳的时候,time.mktime(time.strptime('20170609-15:00:00','%Y%m%d-%H:%M:%S')) 发现测试环境 ...

  9. docker容器修改时区(java应用log信息与标准容器时间有八个小时时间差)

    在docker容器中运行的java应用打出的日志时间和通过date -R方式获取的容器标准时间有八个小时时间差- 因为docker容器的原生时区为0时区,为了和国内时区保持一致,需要把容器时区调为东八 ...

随机推荐

  1. Connecting Elixir Nodes with libcluster, locally and on Kubernetes

    转自:https://www.poeticoding.com/connecting-elixir-nodes-with-libcluster-locally-and-on-kubernetes/ Tr ...

  2. is_null, empty, isset的区别

    参考博客: http://www.jb51.net/article/24666.htm http://www.jb51.net/article/36223.htm is_null, empty, is ...

  3. Revit API 操作共享参数和项目参数

    1.获取共享参数 private string GetSharInfo(Autodesk.Revit.ApplicationServices.Application revitApp) { Strin ...

  4. 关联本地文件夹到 GitLab 项目

    关联本地文件夹到 GitLab 项目的 dev 分支: rm -rf .git git init git remote add origin git pull git checkout dev git ...

  5. 如何使用redis计数器防止并发请求

    需求描述 最近项目中有个需求,短信发送的并发请求问题:业务需求是需要限制一个号码一分钟内只能获取一次随机码,之前的实现是短信发送请求过来后,先去数据库查询发送记录,根据上一次的短信发送时间和当前时间比 ...

  6. Zookeeper命令行world

    world:anyone:cdrwa getAcl /imooc/abc 获得节点abc的权限 设置权限为crwa.  setAcl /nick/abc world:anyone:crwa 测试删除权 ...

  7. SyntaxError: Non-ASCII character '\xe4' in file t.py on line 3, but no encoding declared

    问题 报错代码 #!/usr/bin/python s = "你好" print s 执行报错: File "t.py", line 3 SyntaxError ...

  8. Java读写HDFS文件

    一.依赖包maven路径 <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client --> <d ...

  9. 使用PHPMAILER实现PHP发邮件功能

    第一步: 打开网址https://github.com/PHPMailer/PHPMailer/ 下载PHPMailer,PHPMailer 需要 PHP 的 sockets 扩展支持,而登录 QQ ...

  10. centos下redis的导出和导入(限set命令)

    #!/bin/bash REDIS_HOST=127.0.0.1 REDIS_PORT=6379 REDIS_DB=10 KEYNAME="*" KEYFILE=redis_key ...