By default, regardless of the time zone settings on the host machine, Docker containers use UTC as the time zone.
You can set the timezone for the Docker Container to start using the TZ environment variable:
$ docker run -e TZ='America/Los_Angeles' ubuntu:latest date
You can set the timezone via Dockerfile:
FROM ubuntu:22.04
RUN apt-get update && \
apt-get install -yq tzdata
ENV TZ="America/Los_Angeles"
In this example, the tzdata package is also installed.
However, this may not work in some docker images because they have removed time zones from the /usr/share/zoneinfo directory to save space.
In this case, you can mount this directory from the host to the Docker container:
$ docker run -e TZ='America/Los_Angeles' –volume /usr/share/zoneinfo/America:/usr/share/zoneinfo/America:ro -it ubuntu:latest bash
It is also possible to mount the local host directory with time zones through the Docker Compose file:
services:
ubuntu:
image: ubuntu:latest
container_name: ubuntu_container
environment:
- TZ=America/Los_Angeles
volumes:
- /usr/share/zoneinfo/America:/usr/share/zoneinfo/America:ro