How to manage time zones in docker containers – Cloud Save IT

  • Home / Tech Today / How to manage…

How to manage time zones in docker containers – Cloud Save IT

Time zones are a common source of confusion when handling applications. Will your cronies work at the right time? Docker containers do not inherit the host’s time zone, so you can run into unexpected scheduling issues that wreak havoc with your request.

it is date Command to run locally on Ubuntu 20.04 host in British Summer Time Zone:

And here the same command in the container is based on unmodification ubuntu:20.04 Image:

The container is using the UTC time zone, which creates an hour difference between the two times.

How do Linux time zones work?

Most use Linux distributions tzdata Package for providing time zone information. When tzdata Installed, you can read and inspect the current time zone /etc/timezone File:

You will also have one /etc/localtime This is a SIM link to the correct time zone database for the selected file location.

If you use while changing the time zone dpkg-reconfigure tzdata, /etc/localtime The sylink is updated to point to the new database. Such as command output date The active time zone will be adjusted to include the offset.

Problem in containers

The challenge with containers comes from setting the time zone in the first place. If you think back when setting up your host, you will need to configure the time zone as part of the operating system installation. When you run a new container, it starts immediately, though without any installation steps. There is no opportunity to select the appropriate time zone.

Theoretically, container runtime can automatically inherit the host’s time zone. This does not happen because when setting in a remote environment, unpredictable results can occur. It’s easy to overlook that your cron schedule works on your local machine but unexpectedly hangs in an organized Kubernetes cluster using UTC time.

Container images ship instead of baked in time zone. For most popular images, this will be UTC. Many basic images, especially minimal images, will not be included tzdata You will not have the package /etc/timezone Or /etc/localtime Files.

Add time zones to your containers

The first part is to ensure proper time zone setting tzdata Is installed. If your image is not included, you will need to manually add the package as your part Dockerfile.

FROM ubuntu:latest
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y tzdata

When tzdata Once installed, you usually get an interactive prompt that allows you to select the correct time zone from the menu. This is not helpful when you are programming a docker container. Layout DEBIAN_FRONTEND Climate variable presses immediately and defaults to UTC time zone.

Once you get it tzdata In your icon, you are ready to create the correct time zone for your application. The easiest way is to establish a point of view TZ Environmental variables for the time zone you want to use:

FROM ubuntu:latest
ENV TZ=Europe/London
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y tzdata

If you prefer, you can set TZ Variable when you start the container. Move it as an environment variable docker run. It allows you to override an image default time zone provided it tzdata Package

docker run -e TZ=Europe/London -it ubuntu:latest

An alternative to environmental variables /etc/timezone File You can enter the desired time zone as part of your file Dockerfile. If you use this method, you will need to recreate it tzdata Using your package manager. Remember to use non-interactive mode or you will be prompted for a graphical time zone again.

FROM ubuntu:latest
RUN echo "Europe/London" > /etc/timezone
RUN dpkg-reconfigure -f noninteractive tzdata

Other techniques

If you want to guarantee time zone synchronization with the host, you can mount your local tzdata Files in your container. You will still need it tzdata Inside the container for it to work properly.

docker run -v /etc/timezone:/etc/timezone -v /etc/localtime:/etc/localtime -it ubuntu:latest

Although Docker does not provide any built-in support for time zones, this is not true of all container engines. Podman is a dedicated --tz Flag that lets you set a time zone when creating a new container:

podman run --tz=Europe/London -it ubuntu:latest

Behind the scenes, Podman will make a suitable mountain /etc/localtime The file for you will be maintained for the duration of the specified time zone container.

Podman lets you set default time zones without containers --tz Create or edit flags .config/containers/containers.conf Add to your home directory a tz Configuring a new line in the file:

# Used when no --tz flag is given
tz = "Europe/London"

Podman’s native time zone integration makes it easier to work than a docker. Because Podman’s CLI is compatible with dockers, it’s a good idea to make the switch if you’re frequently working with containers in different time zones.

Abstract

Time zones are often overlooked when designing docker containers. Most base images default to UTC time, which can cause confusion if the host’s time zone varies.

By installing tzdata The package is compatible with all time zones through your container TZ Environmental variables, /etc/timezone, And /etc/localtime. Alternatively, you can synchronize your host’s time zone by adding relevant files to your container.

Lastly, keep in mind that these considerations also apply to host docker services and cabinets clusters. Your container will use UTC time unless otherwise instructed. As long as you can configure environment variables, you will be able to use them TZ To adjust the time zone for your workload.

Write a Comment

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

x