Skip to content

Instantly share code, notes, and snippets.

@sjimenez44
Created September 8, 2021 19:19
Show Gist options
  • Save sjimenez44/1b73afeae3eec26a1915b0d4d5873b8f to your computer and use it in GitHub Desktop.
Save sjimenez44/1b73afeae3eec26a1915b0d4d5873b8f to your computer and use it in GitHub Desktop.
Change time zone Docker container

Change TimeZone in Docker containers

With Docker Engine

The timezone of a container can be set using an environment variable in the docker container when it is created. For example:

$ docker run ubuntu:latest date
Sat Feb 27 15:58:32 UTC 2021
$ docker run -e TZ=America/Bogota ubuntu:latest date
Sat Feb 27 15:58:40 Asia 2021

With Dockerfile

We can also control container timezone using the Dockerfile. For this, we first need to install tzdata package and then specify timezone setting using the environmental variable:

FROM ubuntu:16.04
 
# tzdata for timzone
RUN apt-get update -y
RUN apt-get install -y tzdata
 
# timezone env with default
ENV TZ=America/Bogota

Lets build docker image and run it:

# build docker image
$ docker build -t ubuntu_modified_tz:20210221 .

# run docker container
$ docker run ubuntu_modified_tz:20210221 date
Sat Feb 27 16:58:17 America 2021

With Docker Compose

We can control timezone in the container, by setting TZ environment variable as part of docker-compose:

version: "3.9"
services:
  ubuntu:
    image: ubuntu:latest
    container_name: ubuntu_container
    environment:
        - TZ=America/Bogota

With Storage Data Volumes

The directory /usr/share/zoneinfo in Docker contains the container time zones available. The desired time zone from this folder can be copied to /etc/localtime file, to set as default time.

This time zone files of the host machine can be set in Docker volume and shared among the containers by configuring it in the Dockerfile as below:

volumes: 
- "/etc/timezone:/etc/timezone:ro" 
- "/etc/localtime:/etc/localtime:ro"

The containers created out of this Dockerfile will have the same timezone as the host OS (as set in /etc/localtime file).

This method can also be used to set timezone when using docker compose. However as we have noted above, this might not work for all cases.

With Kubernetes Pods

Again, we have to rely here on setting up of the TZ variable:

spec:
      containers:
      - name: demo
        image: docker.io/ubuntu:latest
        imagePullPolicy: Always
        env:
        - name: TZ
          value: America/Bogota

If we are using deployments, we can mention environment variable as part of container spec:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo
spec:
  replicas: 3
  selector:
    matchLabels:
        app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: demo
        image: docker.io/ubuntu:latest
        imagePullPolicy: Always
        env:
        - name: TZ
          value: America/Bogota
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      terminationGracePeriodSeconds: 0

If above does not work, you may also choose to use host volumes to map /etc/localtime file with the pods/deployments.

Blog

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment