Limits of Docker container time zone configuration across base images
0 reputation · 14 Nov 2024, 03:31 UTC
0 reputation · 14 Nov 2024, 03:31 UTC
Docker Engine does not offer a daemon-level default time zone for containers, leaving each container's local time dependent on image contents, environment variables, and bind mounts. Setting TZ via docker run -e is the documented way to configure a named zone, but its behavior is bounded by the image's userspace libraries and time zone database. In glibc-based images, TZ=America/New_York typically produces correct local time and DST rules when tzdata is present. Alpine-based images frequently omit tzdata, which can cause date to fall back to UTC or fail to resolve named zones entirely.
Because container date conversion libraries such as Python zoneinfo and Java ZoneId read the container's /etc/localtime and TZ rather than the Docker host's system clock directly, the absence of tzdata creates a boundary where the environment variable appears set but does not yield the expected zone abbreviation or offset. Bind-mounting the host /etc/localtime offers an alternative, yet that path may be absent or a symlink on some hosts, introducing another dependency.
Given that Docker Engine provides no built-in time zone inheritance, what are the precise behavioral limits of TZ versus a bind-mounted /etc/localtime when tzdata is missing, and how can teams predict whether a specific base image will honor a named zone without executing the container?
35275 reputation · 14 Nov 2024, 06:34 UTC
Docker’s TZ environment variable only takes effect if the container’s userspace has a time‑zone database that can resolve the zone name. If the base image lacks tzdata (common in Alpine‑based images), the variable is silently ignored and the container falls back to UTC or the system clock. Binding /etc/localtime from the host bypasses the need for tzdata, but it only works when the host path exists, is not a symlink, and the file contains a valid binary time‑zone file. In that case the container reads the binary directly, but no zone abbreviation or DST logic is supplied by name – the binary is already in the correct offset.
date, Python zoneinfo, Java ZoneId, and most libc functions use the zone name to compute offsets and DST transitions.tzdata is installed.
docker run --rm <image> sh -c "apk info tzdata 2>/dev/null || dpkg -l tzdata 2>/dev/null || rpm -q tzdata 2>/dev/null"
/usr/share/zoneinfo directory.
docker run --rm <image> ls /usr/share/zoneinfo | head
/etc/localtime before mounting.
ls -l /etc/localtime && file /etc/localtime
timezone file or symlink that points to a valid zone.
readlink -f /etc/localtime
tzdata, set TZ=America/New_York.
tzdata, either install it in a derived image or bind‑mount the host /etc/localtime.
tzdata and setting TZ.
To fine‑tune the recommendation, could you confirm whether the base image you plan to use ships with the tzdata package installed?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.