How to set timezone in your docker image?

Sometimes you may want to set correct timezone in docker image. It could be needed by some simple things or even it can change how your application inside container behave. Anyway, the task is pretty simple. Let me show you.

So, long story short, to set timezone in your docker image just add following line to your Dockerfile.

ENV TZ="Europe/London"

This method will work if you have package tzdata installed. For example, Debian has tzdata, but Ubuntu doesn’t. Uou need to install tzdata to use same Dockerfile command in Ubuntu.

What to do if you got something different than Debian? Don’t worry, I collected instructions for different distributives. If you use some distro not mentioned let me know, and I’ll try to add instructions for it too.

Debian

As I said before it’s pretty simple. Just add those line in your Dockerfile.

ENV TZ="Europe/London"

Ubuntu

Also simple, but one more command required, because we need to install tzdata.

RUN apt-get update && apt-get install tzdata -y
ENV TZ="Europe/London"

Alpine

Very similar to Debian and Ubuntu, but Alpine uses different package manager (apk instead of apt).

RUN apk add tzdata
ENV TZ="Europe/London"

CentOS

For CentOS the situation is a bit more complex. CentOS uses yum package manager. By default CentOS docker image doesn’t have any URL in sources lists, so you need to add them first. After that everything is very same to another distributives.

RUN sed -i -e "s|mirrorlist=|#mirrorlist=|g" /etc/yum.repos.d/CentOS-*
RUN sed -i -e "s|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g" /etc/yum.repos.d/CentOS-*

RUN yum -y install tzdata
ENV TZ="Europe/London"

If you want to have Dockefile examples, please, checkout my github.

I hope this article helped you. Feel free to ask any question in comments.

Leave a Reply

Your email address will not be published.