32

I am running docker container for my development stack which I pulled from docker-hub, the image is created for a different timezone than where my application is supposed to be deployed.

How do I change timezone in a docker container?

I tried to change the timezone config within the container by running

echo "Africa/Lusaka" > /etc/timezone

and restarted the container but I still get the same timezone.

peterh
  • 1
  • 15
  • 76
  • 99
mekbib.awoke
  • 704
  • 1
  • 7
  • 14

6 Answers6

48

You can override as suggest by @LinPy during the run stage, but if you want to set at your Dockerfile you can set using ENV as tzdata is already there in your base image.

FROM postgres:10
ENV TZ="Africa/Lusaka"
RUN date

Build

docker build -t dbtest .

RUN

docker run -it dbtest -c "date"

Now you can verify on DB side by running

show timezone;

You will see Central Africa Time in both container and Postgres

in the alpine base image, the environment variable will not work. You will need to run

 RUN ls /usr/share/zoneinfo && \
cp /usr/share/zoneinfo/Europe/Brussels /etc/localtime && \
echo "Africa/Lusaka" >  /etc/timezone && \
Adiii
  • 44,267
  • 6
  • 120
  • 114
  • 3
    Note that `tzdata` package has to be installed. Or `/usr/share/zoneinfo` may not even exist. This is the case for me for a Ubuntu docker container. I have to install `tzdata` first. – jdhao Jun 10 '20 at 05:00
  • Yup agree @jdhao, but its already exist in the Postgres image base on alpine https://github.com/docker-library/postgres/blob/f1e039c4ebd8e4691af65dfd6cf280df126039aa/10/alpine/Dockerfile – Adiii Jun 10 '20 at 06:45
  • 1
    To get a list of valid values to use for TZ, refer to this site: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones – Rono Jan 06 '22 at 15:34
27

There's a few ways to do it .

  1. You can declare the time zone directly as an environment variable in the docker compose file
   environment:
      - TZ=Asia/Singapore
      - DEBIAN_FRONTEND=noninteractive
  1. You can map the container's time zone and local time files to use that of the host machine in the docker compose file
volumes:
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro

I personally prefer to use the second method, in this way , all of my containers will have the same time configuration as my host machine

rugby2312
  • 528
  • 5
  • 14
19

Simply change the /etc/localtime to the time zone in the /usr/share/zoneinfo directory.

follow these steps:

first log into bash of your container:

docker exec -u 0 -it mycontainer bash

then remove the symbolic link file (/etc/localtime):

sudo rm -rf /etc/localtime

Identify the timezone you want to configure and create the symbolic link for it:

For instance, I would like to set Asia/Tehran timezone:

ln -s /usr/share/zoneinfo/Asia/Tehran /etc/localtime

Now verify it by:

date

and the output would be your timezone:

Sat Jan 30 14:22:17 +0330 2021
16

the best way is to use ENV in your run stage

-e TZ=Africa/Lusaka

and make sure that the package tzdata is present in the Container

LinPy
  • 14,991
  • 3
  • 31
  • 47
4

A simpler method would be to add an env var to your deployment:

env:
  - name: TZ
    value: "Europe/London"

(kubernetes deployment yaml)

Al Brain
  • 41
  • 1
0

If you have TZ env set correctly and you still get the wrong time, make sure the tzdata system dependency is installed.

Blaise
  • 12,372
  • 9
  • 66
  • 95