How to Set Timezone in Linux Docker Images

In Docker on 2~4 minutes
How to Set Timezone in Linux Docker Images

Most of the time, popular base Docker images like Ubuntu and Alpine are pre-configured to the UTC timezone. For example, if you run the date command in a Docker image, you will see the current time in the UTC timezone.

Ubuntu date command

However, we can easily change or re-configure the system-wide timezone in Docker images. Even though the following instructions are given specifically for Docker-based images, you can use the same commands to configure the system-wide timezone of normal Linux desktop distributions like Ubuntu, Fedora and other similar distributions as well. If you do so, make sure to omit the Docker instructions.

Change The Timezone In Debian Docker Images

Here we are executing two separate commands. The first ln link command makes a symbolic link of the /usr/share/zoneinfo/America/Phoenix file to /etc/localtime. You need to change the America/Phoenix to your required timezone.

The second echo command puts the name of the timezone, in our case America/Phoenix into the /etc/timezone file. Make sure to replace it with your timezone.

ln command flags:

  • s - symbolic link
  • n - treat as a normal file
  • f - overwrite if already existing
FROM debian:latest

RUN ln -snf /usr/share/zoneinfo/America/Phoenix /etc/localtime && echo America/Phoenix > /etc/timezone

If you feel curious about the /etc/localtime file, it is a symbolic link to a binary file which contains information about the related timezone. If you need, you can view more information about the currently selected timezone using the zdump command.

zdump /etc/localtime

zdump -v /etc/localtime

Debian tzdump command

Visit https://www.ibm.com/docs/en/aix/7.2?topic=z-zdump-command for more information about zdump command.

Change The Timezone In Ubuntu Docker Images

We need to install tzdata to configure the timezone in Ubuntu. Then set the TZ environment variable.

FROM ubuntu:latest

RUN apt-get update && apt-get install -yq tzdata && \
    ln -nfs /usr/share/zoneinfo/America/Phoenix /etc/localtime && \
    dpkg-reconfigure -f noninteractive tzdata

ENV TZ="America/Phoenix"

Change The Timezone In Alpine Docker Images

FROM alpine:latest

RUN apk update && apk add tzdata

ENV TZ="America/Phoenix"

Bonus TIP: Change The Timezone With timedatectl

This is a bonus tip if you ever wanted to change the timezone of a Linux distribution which uses Systemd. You can use its timedatectl package to configure the timezone more easily rather than the methods mentioned earlier in this post.

View current timezone information

timedatectl

List all the available timezones

timedatectl list-timezones

Search for a timezone

timedatectl list-timezones | grep -i phoenix

Change the timezone

timedatectl set-timezone America/Phoenix

Furthermore, refer to the tz database time zones list for more available time zones.