問題
在 node debian 鏡像中,用 (new Date()).getHours()
與系統(tǒng)時間(東 8 區(qū))少了 8 小時
系統(tǒng)時間
$ node
> (new Date()).getHours()
11
容器中的時間
$ node
> (new Date()).getHours()
3
原 Dockerfile
FROM node:20.5-bullseye
ARG proxy
RUN set -eux && \
sed -i -e 's#http://deb.debian.org#http://mirrors.aliyun.com#g' \
-e 's#http://security.debian.org#http://mirrors.aliyun.com#g' \
/etc/apt/sources.list && \
apt-get update && \
rm -rf /var/lib/apt/lists/
WORKDIR /app
COPY . .
RUN env http_proxy=$proxy https_proxy=$proxy npm install
ENTRYPOINT [ "node", "index.js" ]
原因
鏡像運(yùn)行起來容器未設(shè)置指定時區(qū)文章來源:http://www.zghlxwxcb.cn/news/detail-625138.html
解決
Dockerfile 中添加 ENV TZ='Asia/Shanghai'
及 apt-get install -yq tzdata
文章來源地址http://www.zghlxwxcb.cn/news/detail-625138.html
FROM node:20.5-bullseye
ARG proxy
# 設(shè)置時區(qū)
ENV TZ='Asia/Shanghai'
RUN set -eux && \
sed -i -e 's#http://deb.debian.org#http://mirrors.aliyun.com#g' \
-e 's#http://security.debian.org#http://mirrors.aliyun.com#g' \
/etc/apt/sources.list && \
apt-get update && \
# 安裝 tzdata
apt-get install -yq tzdata && \
rm -rf /var/lib/apt/lists/
WORKDIR /app
COPY . .
RUN env http_proxy=$proxy https_proxy=$proxy npm install
ENTRYPOINT [ "node", "index.js" ]
驗證
系統(tǒng)時間
$ node
> (new Date()).getHours()
11
容器中的時間
$ node
> (new Date()).getHours()
11
參考
- https://dev.to/0xbf/set-timezone-in-your-docker-image-d22
Set timezone in your docker image
到了這里,關(guān)于node debian 鏡像 new Date 獲取時間少 8 小時問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!