How to deal with time zone issues in Docker

How to deal with time zone issues in Docker

background

When I was using Docker these two days, I found that after my container was started, the output time zone of date -R was UTC, which was always 8 hours behind Beijing time.


Standard image


Time zone is UTC

Checking /etc/localtime, I found that the default time zone is Etc/UTC. And the TZ environment variable is not set.


How to deal with time zone issues in Linux

In fact, all of our time zone handling issues are time zone handling issues in glibc. The most authoritative documentation on this issue is the official glibc documentation, which describes the TZ environment variable and introduces the handling of time zone issues.

https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html#TZ-Variable

The parts that are relevant to us are as follows.

The core meaning is as follows: In glibc, the value of the TZ environment variable is the name of a file whose contents describe time zone related information.

If the TZ environment variable is not set, the system will choose a default value. In glibc, the default value is /etc/localtime. If the TZ environment variable has a value and the value starts with /, it is an absolute path file name, otherwise the file name is /usr/share/zoneinfo/$TZ. The /usr/share/zoneinfo directory contains local time information for various parts of the world, such as Asia/Shanghai. Generally, the files in this directory are installed by the tzdata package.

Following this idea, we force the TZ environment variable to be Asia/Shanghai, and the time zone is correct


Force modification of the /etc/localtime file, the time zone is also correct.

How to handle time zones in Docker containers

According to the above description, there are actually two main ways to set the time zone in a docker container. One is to set the TZ environment variable directly, and the other is to modify the content of /etc/localtime directly (either through a soft link or by directly copying the file) without setting the TZ environment variable.

Here we take setting the TZ environment variable as an example (I prefer to do this myself, it feels more convenient than modifying /etc/localtime).

First, we can add ENV TZ=Asia/Shanghai in the Dockerfile, so that the default TZ environment variable of the image built by Docker is the value we want.


Secondly, we can also use -e TZ=Asia/Shanghai to set the TZ environment variable when pulling up the container. This setting is dynamic. We can set different values ​​for the same image when pulling it up.


Summarize

The time zone processing in docker is actually the time zone processing in glibc. After understanding how glibc handles things, the core is the TZ environment variable and the /etc/localtime file, the time zone processing in docker is simple.

You may also be interested in:
  • Docker time zone issue and data migration issue
  • Docker container time zone adjustment operation
  • The created Docker container time display error/date error/time zone error
  • Detailed explanation of modifying docker time zone and common docker commands
  • How to solve the problem of Docker container time zone and time synchronization
  • The difference between two ways to create a docker image and start the container (summary)
  • Docker container time zone error issue

<<:  How InnoDB cleverly implements transaction isolation levels

>>:  WeChat applet uses canvas to draw clocks

Recommend

Web Design Tutorial (8): Web Page Hierarchy and Space Design

<br />Previous article: Web Design Tutorial ...

How to use CSS counters to beautify ordered lists of numbers

In web design, it is very important to use an org...

HTML blockquote tag usage and beautification

Blockquote Definition and Usage The <blockquot...

Detailed explanation of Linux copy and paste in VMware virtual machine

1. Linux under VMware Workstation: 1. Update sour...

The pitfalls encountered when learning Vue.js

Table of contents Class void pointing ES6 Arrow F...

Introduction to using Unicode characters in web pages (&#,\u, etc.)

The earliest computers could only use ASCII chara...

Analyze how to automatically generate Vue component documentation

Table of contents 1. Current situation 2. Communi...

Deploy Varnish cache proxy server based on Centos7

1. Varnish Overview 1. Introduction to Varnish Va...

MySQL 8.0 Window Function Introduction and Summary

Preface Before MySQL 8.0, it was quite painful to...

HTML input box optimization to improve user experience and ease of use

In order to improve user experience and ease of us...

Realize super cool water light effect based on canvas

This article example shares with you the specific...

MySQL 5.7 and above version download and installation graphic tutorial

1. Download 1. MySQL official website download ad...

How to use firewall iptables strategy to forward ports on Linux servers

Forwarding between two different servers Enable p...