Skip to content
Home » Using the TZ time zone specification in Docker: Instructions and available time zones

Using the TZ time zone specification in Docker: Instructions and available time zones

Advertisements

Some of the links shared in this post are affiliate links. If you click on the link and make a purchase, we will receive an affiliate commission at no additional cost to you.


Docker is a powerful tool for isolating applications in containers and running them platform-independently. When using Docker, it is often important to configure the time zone correctly, especially for time-dependent processes. In this article, you will learn how to use the TZ environment variable in Docker, what it does and which time zones are available.

What is the TZ environment variable?

The TZ environment variable allows you to set the time zone for a Docker container. By default, Docker containers adopt the time zone of the host system. However, if you want to ensure that your container uses a specific time zone, you can control this using the TZ variable.

Why is the time zone configuration important?

The correct time zone is crucial for:

  • Timestamps: Applications that generate timestamps (e.g. in logs or database entries) must generate them in the correct time zone.
  • Scheduled tasks: Cron jobs or other time-based tasks must be executed at the correct local time.
  • Global applications: If your application is used by users in different time zones, it is important that the time is localized correctly.

How to use the TZ environment variable in Docker

To specify the time zone of a Docker container, set the TZ environment variable when starting the container. This is done with the -e (or --env) switch, followed by the desired time zone specification.

Example: Start container with defined time zone

Assuming you want to start a container and set the time zone to “Europe/Berlin”, use the following command:

docker run -e TZ=Europe/Berlin your-docker-image

This command starts the container and ensures that the time zone is set to Berlin, regardless of which time zone the host system uses

Effects of the TC time zone specification

If you set the TZ variable, this has an effect:

  • System time within the container: All time-dependent operations in the container are based on the defined time zone.
  • Logs and timestamps: Each generated time entry (e.g. in logs) is output in the specified time zone.
  • Scheduled tasks: Cron jobs and other time-based tasks run at the correct time in the specified time zone.

List of available time zones

Docker supports all time zones that are maintained by the IANA (Internet Assigned Numbers Authority). These time zones are specified in the format “Region/City”. Here is a selection of the most frequently used time zones:

  • UTC (Universal Time Coordinated)
  • Europe/Berlin
  • Europe/London
  • America/New_York
  • America/Los_Angeles
  • Asia/Tokyo
  • Asia/Shanghai
  • Australia/Sydney
  • Africa/Johannesburg
  • Pacific/Auckland

You can display a complete list of available time zones on your Linux system with the following command:

timedatectl list-timezones

Alternatively, you can also find a list of IANA time zones online.

Leave a Reply

Your email address will not be published. Required fields are marked *

Mastodon