How to install tzdata on a ubuntu docker image?

Solution 1:

One line only:

RUN DEBIAN_FRONTEND="noninteractive" apt-get -y install tzdata

Solution 2:

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

Solution 3:

You can use ARG and ENV directives to your advantage:

ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Europe/Moscow
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.


Solution 4:

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

Solution 5:

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