I am experiencing a timezone error inside a Docker container using the ubuntu:24.04 base image. This issue does not occur when I use the ubuntu:22.04 base image. Here is a minimal reproducible example.
22.04:
docker run -it --mount type=bind,source=/etc/localtime,target=/etc/localtime,readonly --mount type=bind,source=/etc/timezone,target=/etc/timezon,readonly ubuntu:22.04 bash
24.04:
docker run -it --mount type=bind,source=/etc/localtime,target=/etc/localtime,readonly --mount type=bind,source=/etc/timezone,target=/etc/timezon,readonly ubuntu:24.04 bash
Host system timezone: /etc/localtime is a symlink to /usr/share/zoneinfo/America/Chicago.Docker container with ubuntu:24.04: /etc/localtime is a symlink to /usr/share/zoneinfo/UTC.I have tried mounting the host's timezone files into the container:
# timezone synchronization with host- /etc/timezone:/etc/timezone:ro- /etc/localtime:/etc/localtime:ro
Despite this, the timezone inside the container does not match the host's timezone. The tzlocal.get_localzone() function works on the host but throws an error inside the container:
File "/usr/local/lib/python3.12/dist-packages/tzlocal/unix.py", line 180, in _get_localzone tzname = _get_localzone_name(_root) ^^^^^^^^^^^^^^^^^^^^^^^^^^File "/usr/local/lib/python3.12/dist-packages/tzlocal/unix.py", line 159, in _get_localzone_name raise zoneinfo.ZoneInfoNotFoundError(message)zoneinfo._common.ZoneInfoNotFoundError: 'Multiple conflicting time zone configurations found: /etc/timezone: America/Chicago /etc/localtime is a symlink to: Etc/UTC Fix the configuration, or set the time zone in a TZ environment variable.'The problem seems specific to ubuntu:24.04. How can I ensure the container correctly reflects the host's timezone settings?
Additional Information:
I prefer not to explicitly set the timezone inside the Dockerfile.Installing tzdata and other dependencies does not seem to resolve the issue.Any advice on how to resolve this discrepancy would be greatly appreciated!I suspect that Ubuntu 24.04 automatically overwrittes the symlink to UTC time (/usr/share/zoneinfo/UTC)