How do I change timezone in a docker container?

mekbib.awoke picture mekbib.awoke · Aug 22, 2019 · Viewed 14.8k times · Source

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.

Answer

Adiii picture Adiii · Aug 22, 2019

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 && \