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: Because of Company Constraint, I Cannot Put...
Sorry I am a newbie to docker and docker-compose.
The "saved" container does not show correct timezone.
Background:
- because of company constraint, I cannot put the Dockerized SpringBoot Application to a Host B.
- My Boss tells me to Dockerized SpringBoot Application in Host A.
- Save the
docker save $imageName > application.tarin Host A - Load the Saved Image
docker load < application.tarin Host B - Run the Docker image in Host B...
- Host A and Host B are in same timezone (Hong Kong Time)
Result:
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 runordocker compose)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 runordocker 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)
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
################################################