Published in DevOps
3 minutes read

Changing Timezone in a Docker Container

Changing Timezone in a Docker Container

By default, Docker containers inherit the timezone of the host system. However, there might be cases where you need to change the timezone inside a Docker container to match a different location.

Identifying the Current Timezone

Before changing the timezone, it's essential to identify the current timezone inside the Docker container. You can use the date command to check the current date and time.

docker exec <container_name_or_id> date

Replace <container_name_or_id> with the name or ID of your Docker container.

Installing the Timezone Data Package

To change the timezone inside the container, you will need to install the timezone data package specific to the timezone you want to set. The package name can vary depending on the Linux distribution used in the container.

For Debian/Ubuntu-based Containers

docker exec <container_name_or_id> apt-get update
docker exec <container_name_or_id> apt-get install -y tzdata

For Alpine-based Containers

docker exec <container_name_or_id> apk add --no-cache tzdata

Setting the Timezone

After installing the timezone data package, you can set the timezone using the timedatectl or ln command.

Using timedatectl (systemd-based containers)

docker exec <container_name_or_id> timedatectl set-timezone <timezone>

Replace <timezone> with the desired timezone, such as "America/New_York" or "Europe/London."

Using ln (non-systemd containers)

docker exec <container_name_or_id> ln -sf /usr/share/zoneinfo/<timezone> /etc/localtime

Replace <timezone> with the desired timezone path, such as "/usr/share/zoneinfo/America/New_York" or "/usr/share/zoneinfo/Europe/London."

Verifying the Timezone Change

To verify that the timezone has been changed successfully, run the date command again inside the Docker container:

docker exec <container_name_or_id> date

The output should now reflect the updated timezone.

0 Comment