How do I change timezone in a docker container? How do I change timezone in a docker container? docker docker

How do I change timezone in a docker container?


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:10ENV 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 && \


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


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