Help Center/ Cloud Container Engine/ Best Practices/ Container/ Setting the Container Time Zone
Updated on 2025-09-11 GMT+08:00

Setting the Container Time Zone

If you need to obtain the system time to record logs and store data in a database when using containers, a time zone inconsistency within the containers may cause a series of problems. This section provides multiple solutions to address container time zone mismatches. You can choose the most suitable approach based on your specific requirements.

Scenario 1: Setting Time Zone Synchronization Between Pods and Nodes

  1. Log in to the CCE console.
  2. In the Basic Info area of the Create Workload page, enable Time Zone Synchronization so that the same time zone will be used for both the pod and the node. After completing other settings, create the workload.

    Figure 1 Enabling the time zone synchronization

  3. Log in to the node and obtain the time zone.

    date -R

    Information similar to the following is displayed:

    Sat, 12 Apr 2025 16:58:47 +0800

  4. Check whether the time zone of the pod is the same as that of the node.

    Run the following kubectl command to obtain the pod name:

    kubectl get pod

    Run the kubectl command to access the pod. In the command, <pod_name> specifies the pod name obtained in the previous step.

    kubectl exec -it <pod_name> -- /bin/bash

    Obtain the time zone of the pod.

    date -R

    Information similar to the following is displayed:

    Sat, 12 Apr 2025 16:59:23 +0800

Scenario 2: Specifying a Time Zone When Creating a Container Image

When building a container image, you can set the time zone file in the Dockerfile to solve the time zone inconsistency within a single container. You will not be bothered by the time zone problem when using the image.

In this scenario, a CentOS container image is used as an example to describe how to specify a time zone when creating a container image.

  1. Log in to a VM that has Docker installed and can access the Internet.
  2. Create a Dockerfile.

    vim Dockerfile

  3. Press i to switch to the editing mode and enter 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

    Press Esc and enter :wq to save the file and exit.

  4. Build the container image.

    docker build -t centos:v1 -f Dockerfile .

  5. Obtain the time zone of the host node.

    date -R

    Information similar to the following is displayed:

    Sat, 12 Apr 2025 17:23:44 +0800

  6. Check whether the time zone of the pod is the same as that of the node.

    Start the container image and check the container time zone. centos:v1 specifies the created container image.

    docker run -it centos:v1 /bin/sh

    Obtain the time zone of the pod.

    date -R

    Information similar to the following is displayed:

    Sat, 12 Apr 2025 17:25:13 +0800

Scenario 3: Setting Time Zone Synchronization Between Java Container Logs, Pods, and Nodes

After time zone synchronization between a pod and the host node is enabled, the standard time inside the pod (obtained by running date -R) aligns with the node time. However, a Java application uses the default time zone, leading to discrepancies between the time recorded in logs and the pod time. The following uses a Java application that runs in a Tomcat container as an example to describe how to synchronize the time zone in the Java container logs with the node time zone.

  1. Log in to the CCE console.
  2. In the Basic Info area of the Create Workload page, enable Time Zone Synchronization so that the same time zone will be used for both the pod and the node.

    Figure 2 Enabling the time zone synchronization

  3. In the Container Settings area, click Environment Variables and add the following environment variable:

    • Environment variable name: CATALINA_OPTS
    • Environment variable value: -Duser.timezone=GMT+08

  4. After completing the configuration, create the workload.
  5. In the workload list, locate the row containing the workload, click View Log in the Operation column, and check whether the time zone in the logs is the same as that of the pod and node. For details about how to check the time zone of a node and pod, see Scenario 1: Setting Time Zone Synchronization Between Pods and Nodes.