Docker设置容器时区
小于 1 分钟...
Alpine
FROM alpine:latest
# 设置时区
ENV TZ="Asia/Shanghai"
# 安装tzdata
RUN apk add --no-cache tzdata && cp /usr/share/zoneinfo/${TZ} /etc/localtime \
&& echo ${TZ} > /etc/timezone \
&& apk del tzdata
验证
docker build -t alpine:time .
docker run --rm -it alpine:time date
Ubuntu
FROM ubuntu
# 设置localtime
# 此处需要优先设置localtime,否则安装tzdata将会进入时区选择
RUN ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 安装tzdata
RUN apt-get update \
&& apt-get install tzdata -y \
&& apt-get clean
验证
docker build -t ubuntu:time .
docker run --rm -it ubuntu:time date