64

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
  • 11,875
  • 18
  • 85
  • 108
mekbib.awoke
  • 1,094
  • 1
  • 9
  • 16

10 Answers10

70

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
  • 54,482
  • 7
  • 145
  • 148
  • 5
    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
58

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
  • 1,056
  • 1
  • 10
  • 15
  • And if you want to do this at run time (adding it here just for completeness) `docker run -it --name=my-ubuntu --rm -v /etc/timezone:/etc/timezone:ro -v /etc/localtime:/etc/localtime:ro my-ubuntu:1` – vkt Sep 21 '22 at 12:41
  • Is there a way to change it in an existing container? – tolache Oct 24 '22 at 16:46
41

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
  • fantastic, helpful answer. Also, interesting use of -u 0 which I hadn't seen before and made it so I could rm the original /etc/localtime. If you don't use that option then you don't have rights to do that. Thanks again. – raddevus Aug 13 '23 at 22:14
26

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
  • 16,987
  • 4
  • 43
  • 57
5

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

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

(kubernetes deployment yaml)

Al Brain
  • 51
  • 1
  • 1
4

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

Blaise
  • 13,139
  • 9
  • 69
  • 97
1

This question was about a postgres base, mine was about an Alpine base, but based on the Alpine Wiki, what I can glean of best practice means my Dockerfile looks like:

FROM alpine:3.14
RUN apk add --no-cache alpine-conf && \
    setup-timezone -z Europe/London

https://wiki.alpinelinux.org/wiki/Alpine_setup_scripts#setup-timezone

DaveJenni
  • 1,901
  • 1
  • 12
  • 4
1

For anyone who are using --env-file. add

# .env
TZ=Asia/Shanghai

To .env file, and it will get the time zone you want.

Andy Su
  • 131
  • 7
0

use

ls /usr/share/zoneinfo/

to show all zone

Kevin wang
  • 45
  • 2
  • 8
0

For France, for a docker container, in the docker-compose.yml, i tested successfully:

In the volume section i added these 2 entries:

volumes:
- /etc/timezone:/etc/timezone:fr
- /etc/localtime:/etc/localtime:fr