Linux系统,Docker容器时区设置是一个很普遍的问题,本文记录下如何正确设置系统时区

Linux系统时区设置

时区修改

  • 执行 tzselect 命令, 按照提示完成设置
    [root@coder ~]# tzselect
    Please identify a location so that time zone rules can be set correctly.
    Please select a continent or ocean.
    1) Africa
    2) Americas
    3) Antarctica
    4) Arctic Ocean
    5) Asia
    6) Atlantic Ocean
    7) Australia
    8) Europe
    9) Indian Ocean
    1)  Pacific Ocean
    2)  none - I want to specify the time zone using the Posix TZ format.
    #? 5
    Please select a country.
    1) Afghanistan           18) Israel                35) Palestine
    2) Armenia               19) Japan                 36) Philippines
    3) Azerbaijan            20) Jordan                37) Qatar
    4) Bahrain               21) Kazakhstan            38) Russia
    5) Bangladesh            22) Korea (North)         39) Saudi Arabia
    6) Bhutan                23) Korea (South)         40) Singapore
    7) Brunei                24) Kuwait                41) Sri Lanka
    8) Cambodia              25) Kyrgyzstan            42) Syria
    9) China                 26) Laos                  43) Taiwan
    10) Cyprus                27) Lebanon               44) Tajikistan
    11) East Timor            28) Macau                 45) Thailand
    12) Georgia               29) Malaysia              46) Turkmenistan
    13) Hong Kong             30) Mongolia              47) United Arab Emirates
    14) India                 31) Myanmar (Burma)       48) Uzbekistan
    15) Indonesia             32) Nepal                 49) Vietnam
    16) Iran                  33) Oman                  50) Yemen
    17) Iraq                  34) Pakistan
    #? 9
    Please select one of the following time zone regions.
    1) Beijing Time
    2) Xinjiang Time
    #? 1
    
    The following information has been given:
    
            China
            Beijing Time
    
    Therefore TZ='Asia/Shanghai' will be used.
    Local time is now:      Fri Aug 19 10:43:52 CST 2022.
    Universal Time is now:  Fri Aug 19 02:43:52 UTC 2022.
    Is the above information OK?
    1) Yes
    2) No
    #? 1
    
    You can make this change permanent for yourself by appending the line
            TZ='Asia/Shanghai'; export TZ
    to the file '.profile' in your home directory; then log out and log in again.
    
    Here is that TZ value again, this time on standard output so that you
    can use the /bin/tzselect command in shell scripts:
    Asia/Shanghai
  • .bash_profile 文件中执行以下操作
    vim .bash_profile
    
    TZ='Asia/Shanghai';
    export TZ
    
    source ~/.bash_profile

chrony时间同步

centos7已默认使用chrony设置时间,默认服务为chronyd

chronyd 是一个 daemon 守护进程,chronyc 是用来监控 chronyd 性能和配置参数的命令行工具

  • chrony的主配置文件: /etc/chrony.conf
    #chrony.conf配置文件注释
    # 上游公共ntp服务器
    server 0.centos.pool.ntp.org iburst
    server 1.centos.pool.ntp.org iburst
    server 2.centos.pool.ntp.org iburst
    server 3.centos.pool.ntp.org iburst
    
    #-----如果此服务器为服务端,允许的客户端连接
    # Allow NTP client access from local network.
    #allow 192.168.0.0/16
    • 启动服务:
    systemctl enable chronyd #配置开机自启动
    systemctl start chronyd  #启动chrony服务
    
    # 如果客户端同步服务端时间,防火墙开启ntp服务即可, ntp使用123/udp端口协议
  • 查看时间同步命令
    timedatectl         # 查看当前系统时区
    timedatectl list-timezones  # 列出所有可用的时区
    timedatectl set-timezone Asia/Shanghai # 设置系统为Asia/Shanghai 上海时区 CST
    timedatectl status  # 查看时间同步状态
    timedatectl set-ntp true # 开启网络时间同步
  • chronyc用法
    ## 查看 ntp_servers 状态
    chronyc sources -v
    
    ## 查看 ntp_sync 状态
    chronyc sourcestats -v
    
    ## 查看 ntp_servers 是否在线
    chronyc activity -v
    
    ## 查看 ntp 详细信息
    chronyc tracking -v
    
    ## 校准时间服务器
    chronyc tracking

Docker容器时区设置

前提: os本地时区已设置好

容器未启动

启动时加上挂在本地时区目录 /etc/localtime

docker run -it -v /etc/localtime:/etc/localtime ... bash

docker-compose.yaml volumes项添加 /etc/localtime

...

  eagle_celery:
    build: .
    command: "celery -A celery_worker worker -l info -E -c 10 -Q eagle"
    user: nobody
    links:
      - eagle_db
      - eagle_redis
    depends_on:
      - eagle_db
      - eagle_redis
    volumes:
      - .:/code:ro
      - /etc/localtime:/etc/localtime
    container_name: eagle_celery

...

容器已启动

将本地时区文件拷贝到容器中

docker cp /etc/localtime container_id:/etc/localtime