Complete Guide to Setting Docker Container Timezone to UTC+8 (Asia/Shanghai)
Content
In daily containerized deployments, Docker containers use UTC (Coordinated Universal Time) as the default standard time. For developers in other timezones, this often results in an 8-hour discrepancy in log times and database records. Using a Python Gunicorn application as an example, this article explains in detail how to change a Docker container's timezone to UTC+8 (Asia/Shanghai).
## Method 1: Mount the Host's Timezone File (Quickest)
If you prefer not to modify the Docker image, the simplest and most recommended method is to mount the host machine's timezone file directly in the `docker run` command.
```bash
docker run -d \
--name fcp-archive-web-lib00 \
--restart unless-stopped \
-p 18080:18080 \
-v /etc/localtime:/etc/localtime:ro \
fcp-archive-web:1.0 \
gunicorn --bind 0.0.0.0:18080 wsgi:app
```
**How it works**: Linux systems typically read `/etc/localtime` to determine the timezone. By using `-v /etc/localtime:/etc/localtime:ro` (`ro` stands for read-only), the container automatically syncs with the host's timezone settings. This is currently the most compatible OS-level modification method.
---
## Method 2: Use Environment Variables (Conditional)
You might often see the `-e TZ=Asia/Shanghai` approach. However, **this is not guaranteed to work 100% of the time**.
Whether this environment variable takes effect depends on whether the base image (e.g., `alpine`, `debian`) has `tzdata` (timezone database) installed. If the image is too minimal and lacks this component, or if the application (like statically compiled Go binaries) hardcodes timezone logic, the environment variable will be ignored.
---
## Method 3: Configure in Dockerfile (Best Practice)
To ensure **high portability** of the image (meaning the time is correct regardless of which host it's deployed on), the best practice is to explicitly install `tzdata` and configure the timezone within the `Dockerfile`.
Taking `python:3.11-slim` as an example, since it is Debian-based and includes `tzdata` package sources, we can optimize it like this:
```dockerfile
# Base image
FROM python:3.11-slim
# Maintainer info
LABEL maintainer="DP@lib00"
# Install tzdata to ensure the timezone database is complete, and set system timezone
RUN apt-get update && apt-get install -y tzdata && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo "Asia/Shanghai" > /etc/timezone && \
rm -rf /var/lib/apt/lists/*
# Set environment variable
ENV TZ=Asia/Shanghai
# Set working directory for wiki.lib00.com project
WORKDIR /app
COPY requirements.txt /app/requirements.txt
RUN pip install --no-cache-dir -r /app/requirements.txt
COPY . /app
CMD ["gunicorn", "--bind", "0.0.0.0:18080", "wsgi:app"]
```
This approach removes the dependency on the host machine's configuration and avoids potential configuration conflicts.
---
## Pitfall Warning: Timezone Issues at the Python Code Level
Even if the system-level timezone configuration is perfectly correct, incorrect function calls at the code level will still result in wrong times. Be sure to check your Python business logic:
* ✅ **Correct usage**: `datetime.datetime.now()`. This reads the current system timezone (UTC+8).
* ❌ **Incorrect usage**: `datetime.datetime.utcnow()`. **No matter how you configure the system timezone, this will ALWAYS return UTC time**. If your business logic requires it, replace it with `datetime.datetime.now(datetime.timezone.utc)` or simply use `now()`.
Related Contents
Complete Guide to Installing and Configuring Git Server on Synology NAS: Basic to Advanced
Duration: 00:00 | DP | 2026-07-16 20:39:02Why Do .smbdelete Hidden Files Appear After Deleting on Mac SMB Shares? Causes & Ultimate Solutions
Duration: 00:00 | DP | 2026-06-27 19:10:00Fixing 'Unable to locate package openjdk-17-jdk' in PHP 8 Docker (Debian Trixie)
Duration: 00:00 | DP | 2026-07-25 09:23:18Docker Compose Advanced: Configuring Static IPs and Cross-Container SOCKS5 Proxies
Duration: 00:00 | DP | 2026-07-26 09:28:30Nginx Reverse Proxy Guide: Elegantly Routing Specific Subdirectories to Docker Containers
Duration: 00:00 | DP | 2026-07-26 21:31:06DevOps Practice: How to Safely Clear Logs of a Running Docker Container?
Duration: 00:00 | DP | 2026-07-27 09:33:42Practical Guide: Translating Complex Docker Compose to Docker Run Commands
Duration: 00:00 | DP | 2026-07-29 09:44:07The Ultimate Guide to Docker Cron Logging: Host vs. Container Redirection - Are You Doing It Right?
Duration: 00:00 | DP | 2026-01-05 08:03:52Cron Job Failing? The Ultimate Guide to Fixing 'docker: command not found'
Duration: 00:00 | DP | 2026-08-01 09:59:44The Ultimate 'Connection Refused' Guide: A PHP PDO & Docker Debugging Saga of a Forgotten Port
Duration: 00:00 | DP | 2025-12-03 09:03:20Solving the MySQL Docker "Permission Denied" Error on Synology NAS: A Step-by-Step Guide
Duration: 00:00 | DP | 2025-12-03 21:19:10How Can a Docker Container Access the Mac Host? The Ultimate Guide to Connecting to Nginx
Duration: 00:00 | DP | 2025-12-08 23:57:30Docker Exec Mastery: The Right Way to Run Commands in Containers
Duration: 00:00 | DP | 2026-01-08 08:07:44How to Fix the "tsx: not found" Error During Vue Vite Builds in Docker
Duration: 00:00 | DP | 2026-01-10 08:10:19Python String Matching Mastery: Elegantly Check for Multiple Prefixes like 'go' or 'skip'
Duration: 00:00 | DP | 2025-11-17 18:07:14Master cURL Timeouts: A Definitive Guide to Fixing "Operation timed out" Errors
Duration: 00:00 | DP | 2025-11-23 19:03:46The Ultimate Guide to Docker Cron Jobs: Effortlessly Scheduling PHP Tasks in Containers from the Host
Duration: 00:00 | DP | 2025-12-29 10:30:50Should You Encode Chinese Characters in Sitemap URLs? The Definitive Guide
Duration: 00:00 | DP | 2025-11-27 08:19:23Recommended
The SEO Dilemma: Is `page=1` Causing a Duplicate Content Disaster?
00:00 | 122In web pagination, `example.com/list` and `example...
Vue i18n Pitfall Guide: How to Fix the "Invalid Linked Format" Compilation Error Caused by Email Addresses?
00:00 | 148Encountering an "Invalid linked format" compilatio...
A Curated List of Bootstrap Icons for Your Wiki and Knowledge Base
00:00 | 197Choosing the right icons is crucial when building ...
One-Click Google Analytics Integration for LobeChat: Easily Track Your AI Chat App Traffic
00:00 | 93Wondering how many users are visiting your self-ho...