How to Fix Timezone Issues in Docker Containers

Timezone mismatches in Docker containers can lead to confusing log timestamps, scheduling errors, and inconsistent behavior between environments. In this guide, you’ll learn how to fix timezone issues inside your Docker containers.

Why Timezone Matters in Containers

By default, many Docker base images (especially slim ones) use UTC. If your application or logs expect a specific timezone (like Asia/Ho_Chi_Minh or America/New_York), failing to configure this can create bugs that are hard to trace.

Example Problem: Flask App with Incorrect Time

Let’s use a simple Python Flask app to demonstrate.

app.py

from flask import Flask
from datetime import datetime

app = Flask(__name__)

print("Server time is:", datetime.now())

Dockerfile

FROM python:3.11-slim

WORKDIR /app
COPY app.py .
RUN pip install flask

CMD ["python", "app.py"]

Build and Run:

docker build -t timezone-demo .
docker run timezone-demo

You’ll see the time in UTC, not your local timezone.

✅ Option 1: Set Timezone Using Environment Variable (for Linux Alpine/Debian images)

You can mount your host timezone configuration or set the TZ environment variable.

Update your Dockerfile:

FROM python:3.11-slim

ENV TZ=Asia/Ho_Chi_Minh
RUN apt-get update && apt-get install -y tzdata

WORKDIR /app
COPY app.py .
RUN pip install flask

CMD ["python", "app.py"]

This does two things:

  • Installs timezone data
  • Sets the timezone to your desired location (Asia/Ho_Chi_Minh)

Rebuild:

docker build -t timezone-demo .
docker run timezone-demo

Now, when you check the time via the Flask app, it will reflect Vietnam time.

✅ Option 2: Mount Host Timezone Files (Linux Host Only)

You can mount the host’s timezone files directly into the container:

docker run \
  -v /etc/timezone:/etc/timezone:ro \
  -v /etc/localtime:/etc/localtime:ro \
  timezone-demo

This works well if:

  • Your host already has the correct timezone
  • You want the container to exactly mirror the host’s time

✅ Option 3: Pass The Timezone At Runtime

docker run -e TZ=Asia/Ho_Chi_Minh timezone-demo

🧠 Tips and Gotchas

IssueSolution
Time shows in UTCInstall tzdata and set TZ env variable
Timezone not recognizedUse full name (e.g., Asia/Ho_Chi_Minh, not Vietnam)
You’re using Alpine LinuxUse apk add tzdata instead of apt-get
Docker Compose setupAdd TZ under environment: section

Docker Compose Example

services:
  app:
    build: .
    ports:
      - "5000:5000"
    environment:
      - TZ=Asia/Ho_Chi_Minh

Timezone issues in Docker are easy to overlook but can lead to serious confusion in production. Whether you’re debugging logs or scheduling tasks, configuring the correct timezone ensures consistency between your containers and the real world.

You can read more useful articles like Docker Security Best Practices For Developers.

Follow us for the more helpful posts!

We hope this is a useful post for you.

 

5 1 vote
Article Rating

Aaron LX

Aaron is a passionate writer, crazy about shopping, eCommerce and trends. Besides his outstanding research skills and a positive mind, Aaron eagerly shares his experience with the readers.

Leave a Reply or put your Question here

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x