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

MySQL transaction concepts and usage in-depth explanation

Table of contents The concept of affairs The stat...

Two ways to build a private GitLab using Docker

The first method: docker installation 1. Pull the...

mysql command line script execution example

This article uses an example to illustrate the ex...

Implementing long shadow of text in less in CSS3

This article mainly introduces how to implement l...

MySQL 5.7.27 winx64 installation and configuration method graphic tutorial

This article shares the installation and configur...

MySQL solution for creating horizontal histogram

Preface Histogram is a basic statistical informat...

jQuery realizes the full function of shopping cart

This article shares the specific code of jQuery t...

Detailed steps for running springboot project in Linux Docker

Introduction: The configuration of Docker running...

Theory: The two years of user experience

<br />It has been no more than two years sin...

HTML tag meta summary, HTML5 head meta attribute summary

Preface meta is an auxiliary tag in the head area...

Step by step guide to build a calendar component with React

Table of contents Business Background Using Techn...

Vue basic instructions example graphic explanation

Table of contents 1. v-on directive 1. Basic usag...

Solve the problem of using linuxdeployqt to package Qt programs in Ubuntu

I wrote some Qt interface programs, but found it ...

How to access MySql through IP address

1. Log in to mysql: mysql -u root -h 127.0.0.1 -p...

Detailed explanation of Vue3 sandbox mechanism

Table of contents Preface Browser compiled versio...