Here's how to set the system timezone in containers so that Local::now() automatically uses the correct timezone (e.g., Europe/London with BST/GMT):
🐳 Docker Container Timezone Setup
1️⃣ Debian/Ubuntu-based Images
dockerfile
FROM debian:latest
# Install tzdata for timezone support
RUN apt-get update && apt-get install -y tzdata
# Set timezone to Europe/London
ENV TZ=Europe/London
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# Your app
COPY target/release/myapp /usr/local/bin/
CMD ["myapp"]
2️⃣ Alpine Linux (smaller images)
dockerfile
FROM alpine:latest
# Install timezone data
RUN apk add --no-cache tzdata
# Set timezone
ENV TZ=Europe/London
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# Your app
COPY target/release/myapp /usr/local/bin/
CMD ["myapp"]
3️⃣ Using Environment Variable Only (simplest)
dockerfile
FROM debian:latest
# Just set the TZ environment variable
ENV TZ=Europe/London
# Install tzdata (required for TZ to work)
RUN apt-get update && apt-get install -y tzdata && rm -rf /var/lib/apt/lists/*
# Your app
COPY target/release/myapp /usr/local/bin/
CMD ["myapp"]