227

I have the following line in the Dockerfile.

RUN apt-get install -y tzdata

When I run it, it asks for my input. After I provided my input, it hung there. Does anybody know how to solve this problem?

Step 25/25 : RUN apt-get install -y tzdata
 ---> Running in ee47a1beff84
Reading package lists...
Building dependency tree...
Reading state information...
The following NEW packages will be installed:
  tzdata
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 189 kB of archives.
After this operation, 3104 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 tzdata all 2018i-0ubuntu0.18.04 [189 kB]
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (This frontend requires a controlling tty.)
debconf: falling back to frontend: Teletype
dpkg-preconfigure: unable to re-open stdin: 
Fetched 189 kB in 1s (219 kB/s)
Selecting previously unselected package tzdata.
(Reading database ... 25194 files and directories currently installed.)
Preparing to unpack .../tzdata_2018i-0ubuntu0.18.04_all.deb ...
Unpacking tzdata (2018i-0ubuntu0.18.04) ...
Setting up tzdata (2018i-0ubuntu0.18.04) ...
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
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:
``

10 Answers 10

236

One line only:

RUN DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC apt-get -y install tzdata
9
  • 16
    This is the simplest solution. You can additionally add TZ="America/New_York". It is a cool trick of adding environment variable to the run command. Commented Apr 30, 2020 at 18:27
  • 6
    unfortunately, this didn't work for me, the answer from @SnakE did Commented Aug 11, 2020 at 8:31
  • 24
    Can I suggest putting these into ENV statements before the run? ENV DEBIAN_FRONTEND="noninteractive" TZ="Europe/London" Commented Oct 3, 2020 at 11:24
  • 7
    @DannyStaple That will leave the environment variable in the resulting docker container, which might be undesired. By giving the env var to the RUN command, you can be sure that the environment variable affects only the tzdata install.
    – Ignatius
    Commented Nov 13, 2020 at 9:06
  • 5
    If you are using sudo to run apt-get (because the user is not root), make sure to pass the -E flag, otherwise sudo will discard all environment variables (including DEBIAN_FRONTEND). Commented Jan 23, 2021 at 17:13
131

You can use ARG and ENV directives to your advantage:

ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Etc/UTC
RUN apt-get install -y tzdata

This way DEBIAN_FRONTEND will be defined only while you build your image while TZ will persist at runtime.

2
  • The usage of ARG and ENV is well done here. I would add an apt-get update and apt-get clean in the last step.
    – user8162
    Commented Sep 14, 2023 at 9:56
  • 2
    I wouldn't want this answer to turn into a tutorial on how to use apt-get in your Dockerfiles. It answers a very specific question, and keeping it short helps emphasize the point.
    – SnakE
    Commented Sep 18, 2023 at 23:21
54

You need to execute serie of commands:

    # set noninteractive installation
    export DEBIAN_FRONTEND=noninteractive
    # install tzdata package
    apt-get install -y tzdata
    # set your timezone
    ln -fs /usr/share/zoneinfo/America/New_York /etc/localtime
    dpkg-reconfigure --frontend noninteractive tzdata

(commands which start with # are comments and you can ignore them)

The best way is to create script, copy the script to container and execute it In Dockerfile:

ADD yourscript.sh /yourscript.sh
RUN /yourscript.sh
2
  • Good answer, but might I ask why this is necessary? Is this a Docker issue or an Ubuntu issue? Commented Nov 19, 2019 at 22:09
  • @JeremyHajek, this is the way docker (and containers) work. Some things need to be done on weird way. Commented Nov 20, 2019 at 5:14
17

Set two environment variables in a docker-compose file. One disables the prompt, and the other sets the timezone.

docker-compose.yml

version: '3.7'
services:
  timezone:
    build: .
    environment:
      - TZ=America/New_York
      - DEBIAN_FRONTEND=noninteractive

Then simply install tzdata in your image.

Dockerfile

FROM ubuntu:18.04
RUN apt-get update && apt-get install -y tzdata
# Testing command: Print the date.  It will be in the timezone set from the compose file.
CMD date

To test:

docker-compose build timezone
15

Make sure if you're using @petertc's solution and are doing apt-get update && apt-get install on the same line that the DEBIAN_FRONTEND statement is after the &&:

Right:

RUN apt-get update && DEBIAN_FRONTEND="noninteractive" TZ="America/New_York" apt-get install -y tzdata

Wrong:

RUN DEBIAN_FRONTEND="noninteractive" TZ="America/New_York" apt-get update && apt-get install -y tzdata
2

From a simple Dockerfile it works but it might require further tweaking (tz is 19:25 but 16:25 inside docker, idc now because it is for automation purpose on a ARM64 jetson nano)

RUN export TZ=Europe/Paris
RUN pip3 install -U Cython contextlib2 pillow lxml jupyter matplotlib
RUN DEBIAN_FRONTEND=noninteractive apt-get install protobuf-compiler python-pil python-lxml python-tk -y
Unpacking protobuf-compiler (3.0.0-9.1ubuntu1) ...
Setting up python-chardet (3.0.4-1) ...
Setting up tzdata (2019c-0ubuntu0.18.04) ...

Current default time zone: 'Etc/UTC'
Local time is now:      Wed Apr 22 16:25:17 UTC 2020.
Universal Time is now:  Wed Apr 22 16:25:17 UTC 2020.
Run 'dpkg-reconfigure tzdata' if you wish to change it.

Setting up libxss1:arm64 (1:1.2.2-1) ...
2

On focal 20.04 the solutions using TZ=... and DEBIAN_FRONTEND=... does not work anymore. It used to work till bionic 18.04. The docker file snippet that works for focal look like:

   ## preesed tzdata, update package index, upgrade packages and install needed software
   RUN truncate -s0 /tmp/preseed.cfg && \
       (echo "tzdata tzdata/Areas select America" >> /tmp/preseed.cfg) && \
       (echo "tzdata tzdata/Zones/America select Los_Angeles" >> /tmp/preseed.cfg) && \
       debconf-set-selections /tmp/preseed.cfg && \
       rm -f /etc/timezone /etc/localtime && \
       apt-get update && \
       DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
       apt-get install -y tzdata
   ## cleanup of files from setup
   RUN rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

The solution is mostly derived from another stackoverflow topic.

2
  • What is the advantage of using a subshell for the preseed file echos? I see mostly a disadvantage: You introduce a potential race condition. Commented Dec 2, 2020 at 19:42
  • Thanks for pointing out the race condition.
    – minghua
    Commented Dec 4, 2020 at 20:10
2

For me, it worked and I preferred this way (this way you don't need to set a noninteractive mode):

Set an environment variable with your timezone, for instance: ENV TZ=Europe/Madrid

Then, print this variable into a file and then link that file to the file which the the configuration process will read when installing tzdata: RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

Finally, install tzdata normally: RUN apt-get update && apt-get install -y tzdata

Extracted from: https://dev.to/setevoy/docker-configure-tzdata-and-timezone-during-build-20bk

2

On ubuntu22:04 image, I got:

Setting up locales (2.34-0ubuntu3) ...
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
Generating locales (this might take a while)...
  de_AT.UTF-8... done
  ...
  sv_SE.UTF-8... done
Generation complete.
Setting up libjansson4:amd64 (2.13.1-1.1build2) ...
Setting up libsasl2-modules-db:amd64 (2.1.27+dfsg2-2build1) ...
Setting up tzdata (2021e-1ubuntu1) ...
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
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   3. Antarctica  5. Arctic  7. Atlantic  9. Indian    11. US
  2. America  4. Australia   6. Asia    8. Europe    10. Pacific  12. Etc
Geographic area:

so that the Dockerfile would not build.

Taken from Getting tons of debconf messages unless TERM is set to linux #58, I needed to run:

RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections
# And afterwards whatever you like, for example:
RUN DEBIAN_FRONTEND=noninteractive apt-get update && apt-get -y \
--no-install-recommends install ...

which stopped debconf reporting on the missing terminal and also stopped the tzdata installer from opening the locations menu during the Dockerfile build.

0

This is what worked for me:

FROM ubuntu
RUN apt-get update
ARG DEBIAN_FRONTEND=noninteractive
RUN ln -s /usr/share/zoneinfo/America/New_York /etc/localtime && apt-get install tzdata -y

Output:

Sending build context to Docker daemon  6.144kB
Step 1/6 : FROM ubuntu
 ---> 08d22c0ceb15
Step 2/6 : RUN apt-get update
 ---> Using cache
 ---> 5800f3ac7c48
Step 3/6 : ARG DEBIAN_FRONTEND=noninteractive
 ---> Using cache
 ---> 9adeac79d3a8
Step 4/6 : RUN ln -s /usr/share/zoneinfo/America/New_York /etc/localtime && apt-get install tzdata -y
 ---> Running in d6bbe6e72e23
Reading package lists...
Building dependency tree...
Reading state information...
The following NEW packages will be installed:
  tzdata
0 upgraded, 1 newly installed, 0 to remove and 2 not upgraded.
Need to get 335 kB of archives.
After this operation, 3867 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 tzdata all 2023c-0ubuntu0.22.04.0 [335 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 335 kB in 0s (3517 kB/s)
Selecting previously unselected package tzdata.
(Reading database ... 4395 files and directories currently installed.)
Preparing to unpack .../tzdata_2023c-0ubuntu0.22.04.0_all.deb ...
Unpacking tzdata (2023c-0ubuntu0.22.04.0) ...
Setting up tzdata (2023c-0ubuntu0.22.04.0) ...

Current default time zone: 'America/New_York'
Local time is now:      Thu Apr 13 17:39:33 EDT 2023.
Universal Time is now:  Thu Apr 13 21:39:33 UTC 2023.
Run 'dpkg-reconfigure tzdata' if you wish to change it.

Removing intermediate container d6bbe6e72e23
 ---> 9e4fbb9adcd1
2
  • are you saying that DEBIAN_FRONTEND=noninteractive alone is not sufficient and that the symlink is required? Commented Jun 21, 2023 at 8:30
  • @FrederickNord the symlink was required in my environment
    – Val Kornea
    Commented Jun 23, 2023 at 13:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .