Sometimes you may want to set the correct timezone in a docker image. It could be needed for some simple things, or even it can change how your application inside the container behaves. Anyway, the task is pretty simple. Let me show you.
So, long story short, to set the timezone in your docker image, add the following line to your Dockerfile.
ENV TZ="Europe/London"
This method will work if you have the package tzdata installed. For example, Debian has tzdata, but Ubuntu doesn't. You need to install tzdata to use the 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 that line to 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 a 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 the 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 similar to another distributive.
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, check out my github.
I hope this article helps you. Feel free to ask any questions in the comments.
Top comments (0)