How to Fix Timezone in Container Generated by Docker Build

Sorry I am a newbie to docker and docker-compose.

The "saved" container does not show correct timezone.

Background:

  1. because of company constraint, I cannot put the Dockerized SpringBoot Application to a Host B.
  2. My Boss tells me to Dockerized SpringBoot Application in Host A.
  3. Save the docker save $imageName > application.tar in Host A
  4. Load the Saved Image docker load < application.tar in Host B
  5. Run the Docker image in Host B...
  6. Host A and Host B are in same timezone (Hong Kong Time)

Result:

  1. The timezone inside container (check log result) find the log time is correct (Hong Kong timezone, UTC+8) in Host A (no matter it is triggered by docker run or docker compose)

  2. The timezone inside the container (check log result) find the log time is WRONG (UTC+0) in Host B (no matter it is triggered by docker run or docker compose)

docker version Client: Version: 1.13.1 API version: 1.26 Package version: docker-1.13.1-88.git07f3374.el7.x86_64 Go version: go1.10.2 Git commit: 07f3374/1.13.1 Built: Thu Dec 6 07:01:49 2018 OS/Arch: linux/amd64

docker-compose version docker-compose version 1.23.2, build 1110ad01 docker-py version: 3.6.0 CPython version: 3.6.7 OpenSSL version: OpenSSL 1.1.0f 25 May 2017

Host A Ubuntu Version 18.04.3

FROM java:8-jdk-alpine
WORKDIR /root/flexi/
COPY ./target/foo.jar /root/flexi/

### an alpine based image you have to install the tzdata first #### 
RUN apk add --no-cache tzdata


### ENV TZ=Asia/Hong_Kong
### RUN echo "Europe/Stockholm" > /etc/timezone
### RUN dpkg-reconfigure -f noninteractive tzdata
### Not work for ubuntu to dpkg-reconfigure


VOLUME /log

ADD db.properties /root/flexi
EXPOSE 9988
RUN sh -c 'touch foo.jar'
ENTRYPOINT ["java", "-jar", "foo.jar"]
version: '2.2'
services:
  foos:
    build:
      context: ./
      dockerfile: Dockerfile
    image: foos
    ports:
      - "9555:9988"
    environment:
      - TZ=Asia/Hong_Kong
    networks:
      - network1
    volumes:
      - /log:/log
networks:
  network1:
docker build -t foos .
docker inspect -f '{{ .Created }}'  foos
--> Shows UTC time (in both Host A and Host B)
0

2 Answers

Set Timezone Using environment variables

  • The timezone of a container can be set using an environment variable

docker run -e TZ=America/New_York ubuntu date

  • the time zone data package tzdata needs to be installed in the container
  • configure an NTP server to ensure that the time zones are synced in containers

Solution:

# install tzdata ref. 
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install tzdata

RUN cp -r -f /usr/share/zoneinfo/Asia/Hong_Kong /etc/localtime

if not run this command , the build image does not contain /etc/timezone, therefore it use UTC time.


Details

### COPY /etc/timezone      /ect/timezone
### fail. It cannot copy the file to the container internally

################################################
## No luck to execute dpkg-reconfigure for UBUNTU 18.0.4.3
################################################
## ENV DEBIAN_FRONTEND=noninteractive
### RUN dpkg-reconfigure --frontend noninteractive tzdata
OR
### RUN sudo dpkg-reconfigure --frontend noninteractive tzdata
################################################

################################################
## no use
################################################
ENV TZ=Asia/Hong_Kong
### RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
################################################

################################################
## no use
################################################
## RUN echo "Asia/Hong_Kong" > /config/etc/timezone
################################################

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest