How to set timezone in your docker image?
Tags: docker
Published: 2022-11-02 20:04:57

<!-- wp:paragraph -->
<p>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. </p>
<!-- /wp:paragraph -->

<!-- wp:more -->
<!--more-->
<!-- /wp:more -->

<!-- wp:paragraph -->
<p>So, long story short, to set the timezone in your docker image, add the following line to your Dockerfile.  </p>
<!-- /wp:paragraph -->

<!-- wp:enlighter/codeblock -->
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">ENV TZ="Europe/London"</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:paragraph -->
<p>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. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Debian</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>As I said before, it's pretty simple. Just add that line to your Dockerfile.</p>
<!-- /wp:paragraph -->

<!-- wp:enlighter/codeblock -->
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">ENV TZ="Europe/London"</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:heading -->
<h2>Ubuntu</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Also simple, but one more command required because we need to install tzdata.</p>
<!-- /wp:paragraph -->

<!-- wp:enlighter/codeblock -->
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">RUN apt-get update &amp;&amp; apt-get install tzdata -y
ENV TZ="Europe/London"</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:heading -->
<h2>Alpine</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Very similar to Debian and Ubuntu, but Alpine uses a different package manager (apk instead of apt).</p>
<!-- /wp:paragraph -->

<!-- wp:enlighter/codeblock -->
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">RUN apk add tzdata
ENV TZ="Europe/London"</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:heading -->
<h2>CentOS</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:enlighter/codeblock -->
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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"</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:paragraph -->
<p>If you want to have Dockefile examples, please, check out my <a href="https://github.com/Jakeroid/docker-image-timezone">github</a>. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I hope this article helps you. Feel free to ask any questions in the comments. </p>
<!-- /wp:paragraph -->