Skip to main content

Resolving Time Zone Inconsistency Issues

Problem Background

The container system time defaults to UTC (Universal Time Coordinated), which differs by 8 hours from the local time zone CST (Shanghai Time). When it is necessary to obtain the system time for operations such as logging, database storage, etc., the time zone inconsistency issue within the container will cause a series of problems.

Operation Steps

When building a base image or creating a custom image based on it, creating a timezone file in the Dockerfile resolves the time zone inconsistency issue within a single container, and subsequent use of this image will not be affected by time zone problems.

  1. Open the Dockerfile file.

  2. Write the following content to configure the time zone file.

    FROM centos
    RUN rm -f /etc/localtime \
    && ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo "Asia/Shanghai" > /etc/timezone
  3. Rebuild the container image and redeploy using the new image. Alternatively, directly upload the code package containing the new Dockerfile and redeploy.