Solve the problem of 8 hours difference between docker container and host machine

Solve the problem of 8 hours difference between docker container and host machine

When using docker-compose for deployment, the output logs and related events are verified and output, resulting in an 8-hour difference between the events and reality.

Troubleshooting:

1. Check the host time:

# View the time date
Wed Sep 29 11:12:44 CST 2021
# Check the time zone date -R
Wed, 29 Sep 2021 11:13:34 +0800

2. Enter the container to view the time

# View all containers docker ps -a
# Enter the container docker eec -it [container ID] /bin/bash
# View the time date

At this time, the host time is the same as the docker container time. After searching Baidu, I finally realized that it was a timezone problem.

Linux time:

Container time:

Cause: The host machine has a time zone set, but the Docker container has not, resulting in an 8-hour time difference between the two.

CST should refer to (China Shanghai Time, Eastern Time Zone 8)
UTC should refer to (Coordinated Universal Time, standard time)
Therefore, the difference between these two times should actually be 8 hours.

Therefore, the time zones of the two must be unified

Solution:

1. Docker-compose configuration volume mount

volumes:
      - /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime

2.Dockerfile runs the command and outputs the corresponding time zone to timezone

RUN echo "Asia/shanghai" > /etc/timezone
 
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

3. Delete the container and rerun

#Delete docker rm [container ID]
# Start docker-compose -f [file name] up -d

Summary: The 2-hour time difference is caused by the /etc/timezone problem in the container. You can set the corresponding time zone.

This is the end of this article about the 8-hour difference between docker container and host machine. For more relevant content about docker container and host machine, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Docker enables seamless calling of shell commands between container and host
  • Solution to the Docker container not having permission to write to the host directory
  • Solution to the Docker container being unable to access the host port
  • Execute the shell or program inside the Docker container on the host
  • Call and execute host docker operations in docker container
  • Detailed explanation of how to solve the problem that the docker container cannot access the host machine through IP
  • How to use Docker container to access host network

<<:  Introduction to new ECMAscript object features

>>:  Remove the a label and button and add the background image dotted line/shadow perfect solution

Recommend

4 principles for clean and beautiful web design

This article will discuss these 4 principles as t...

Things to note when writing self-closing XHTML tags

The img tag in XHTML is so-called self-closing, w...

Detailed explanation of script debugging mechanism in bash

Run the script in debug mode You can run the enti...

Use elasticsearch to delete index data regularly

1. Sometimes we use ES Due to limited resources o...

Summarize the commonly used nth-child selectors

Preface In front-end programming, we often use th...

Element sample code to implement dynamic table

Table of contents 【Code background】 【Code Impleme...

MySQL statement arrangement and summary introduction

SQL (Structured Query Language) statement, that i...

mysql8.0.11 winx64 installation and configuration tutorial

The installation tutorial of mysql 8.0.11 winx64 ...

Javascript basics about built-in objects

Table of contents 1. Introduction to built-in obj...

Detailed explanation of how to find the location of the nginx configuration file

How can you find the location of the configuratio...

Brief Analysis of MySQL B-Tree Index

B-Tree Index Different storage engines may also u...

MySQL 8.0.16 winx64 installation and configuration method graphic tutorial

I just started learning about databases recently....

Vue implements horizontal scrolling of marquee style text

This article shares the specific code for Vue to ...