For a project, I need a container whose timezone is synced to host machine’s timezone. My container should have the ability to sync to timezone changes on host machine at runtime. For this purpose I mounted /etc/localtime symlink and /etc/timezone file as per googling the topic.
My host machine is Ubuntu 20.04.4 LTS.
Below is my docker run command:
docker run
-v /etc/localtime:/etc/localtime:ro
-v /etc/timezone:/etc/timezone:ro
--name openjdk-jdk11-slim
-it --rm
openjdk:11-jre-slim
After container starts, if I change the timezone of host machine via;
sudo timedatectl set-timezone Africa/Bissau
I see that date command inside container (container’s interactive terminal) still outputs the timezone state of when container created. After changing timezone on host, if I recreate container, then only I can get the new timezone in container.
Is there an elegant way to fix this?
PS: My Docker version is as follows;
testuser@test-ubuntu:~$ docker --version
Docker version 20.10.7, build 20.10.7-0ubuntu5~20.04.2
2
Answers
Did you try to set an environment variable in the Docker container? As an example,
Populate the TZ via cat so it’s automatic: