dockertimezonedevops

How do I change timezone in a docker container?


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.


Solution

  • You can override as suggest by LinPy during the run stage, but if you want to set it in your Dockerfile you can use 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 && \