lostmarine
lostmarine

Reputation: 39

Docker container has wrong localtime and timezone

My docker timezone did not work fine in my server ;

first , here is my host config:

cat /etc/timezone #Asia/Shanghai
ls /etc/localtime -l #/etc/localtime -> /usr/share/zoneinfo/Asia/Shanghai
date #Thu Nov 11 01:07:40 AM CST 2021

I think it looks fine. then , I try start docker container and show the date

docker run --rm -it alpine /bin/sh
#/ date 
Wed Nov 10 17:01:51 UTC 2021
#/ exit

## append time sync code

docker run --rm -it -v /etc/localtime:/etc/localtime -v /etc/timezone:/etc/timezone alpine /bin/sh
#/ date
Wed Nov 10 17:03:25 UTC 2021

I have do same thing in other linux server . It show the time correctly. so, I know something wrong with my server but I don't know where.

Is there anybody help?

Upvotes: 1

Views: 3484

Answers (1)

emadelbieh
emadelbieh

Reputation: 356

In your Dockerfile include these 2 lines:

ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

Upvotes: 2

Related Questions