Docker: configure tzdata and timezone during build

By | 05/17/2020
 

During a Docker image build – it stops asking to configure the tzdata.

Dockerfile at this moment is the next:

FROM ubuntu:18.04
RUN apt update && apt install -y python-pip python-dev ssh python-boto3
RUN pip install ansible==2.4.3.0

Let’s reproduce – run the build:

admin@jenkins-production:~$ docker build -t proj/proj-ansible:1.1 .
Sending build context to Docker daemon  29.62MB
Step 1/3 : FROM ubuntu:18.04
---> 113a43faa138
Step 2/3 : RUN apt update && apt install -y python-pip python-dev ssh python-boto3
...
Setting up tzdata (2018d-1) ...
...
Configuring tzdata
------------------
Please select the geographic area in which you live. Subsequent configuration
questions will narrow this down by presenting a list of cities, representing
the time zones in which they are located.
1. Africa      4. Australia  7. Atlantic  10. Pacific  13. Etc
2. America     5. Arctic     8. Europe    11. SystemV
3. Antarctica  6. Asia       9. Indian    12. US
Geographic area:

And here it hangs waiting for us enter data, and even after you’ll enter a region – the process will not resume.

To avoid this data-enter request, especially when such build is running on CI like Jenkins – you can configure tzdata directly in the Dockerfile so it will be set during the docker build.

To do so, create a variable called $TZ which will hold our timezone, and the create a /etc/timezone file:

FROM ubuntu:18.04
ENV TZ=Europe/Kiev
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt update && apt install -y python-pip python-dev ssh python-boto3
RUN pip  install ansible==2.4.3.0

Build again:

admin@jenkins-production:~$ docker build -t proj/proj-ansible:1.1 .
Sending build context to Docker daemon  29.62MB
Step 1/5 : FROM ubuntu:18.04
---> 113a43faa138
Step 2/5 : ENV TZ=Europe/Kiev
---> d2debbbd9f86
Step 3/5 : RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
...
---> 0d2422a01f56
Successfully built 0d2422a01f56
Successfully tagged proj/proj-ansible:1.1

Done.