How to install tzdata on a ubuntu docker image?

One line only:

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

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.


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

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