问题描述
我的Dockerfile是:
My Dockerfile is:
FROM gliderlabs/alpine:3.3
RUN set -x \
&& buildDeps='\
python-dev \
py-pip \
build-base \
' \
&& apk --update add python py-lxml py-mysqldb $buildDeps \
&& rm -rf /var/cache/apk/* \
&& mkdir -p /app
ENV INSTALL_PATH /app
ENV TZ=Asia/Shanghai
WORKDIR $INSTALL_PATH
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
COPY requirements-docker.txt ./
RUN pip install -r requirements-docker.txt
COPY . .
RUN apk del --purge $buildDeps
ENTRYPOINT ["celery", "-A", "tasks", "worker", "-l", "info", "-B"]
我将时区设置为亚洲/上海
,但是它不起作用,给了我有8小时偏差的UTC,结果是:
I setted the timezone as Asia/Shanghai
, but it did not work and gave me the UTC which had 8 hours deviation, the result is :
2016-01-24 11:25:07:[2016-01-24 03:25:07,893: WARNING/Worker-2] 2016-01-24 03:25:07.892718
2016-01-24 11:25:08:[2016-01-24 03:25:08,339: INFO/MainProcess] Task tasks.crawl[98c9a9fc-0817-45cb-a2fc-40320d63c41a] succeeded in 0.447403368002s: None
2016-01-24 11:27:07:[2016-01-24 03:27:07,884: INFO/Beat] Scheduler: Sending due task spider (tasks.crawl)
然后我尝试了其他方法,例如:
Then I tried other methods like:
RUN echo "Asia/Shanghai" > /etc/timezone && dpkg-reconfigure -f noninteractive tzdata
和
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
它们都不起作用,如何设置时区?非常感谢。
none of them did work, how can set the timezone? Thanks very much.
推荐答案
通常的解决方法是挂载 / etc / localtime
,如
The usual workaround is to mount /etc/localtime
, as in issue 3359
$ docker run --rm busybox date
Thu Mar 20 04:42:02 UTC 2014
$ docker run --rm -v /etc/localtime:/etc/localtime:ro busybox date
Thu Mar 20 14:42:20 EST 2014
$ FILE=$(mktemp) ; echo $FILE ; echo -e "Europe/Brussels" > $FILE ; docker run --rm -v $FILE:/etc/timezone -v /usr/share/zoneinfo/Europe/Brussels:/etc/localtime:ro busybox date
/tmp/tmp.JwL2A9c50i
Thu Mar 20 05:42:26 CET 2014
同一主题提到(尽管基于ubuntu的图像)
The same thread mentions (for ubuntu-based image though), but you already tried it.
RUN echo Europe/Berlin > /etc/timezone && dpkg-reconfigure --frontend noninteractive tzdata
(还有)
另一种选择是是建立自己的图片,其中包含。
Another option would be to build your own gliderlabs/docker-alpine
image with builder/scripts/mkimage-alpine.bash
.
该脚本允许您。
[[ "$TIMEZONE" ]] && \
cp "/usr/share/zoneinfo/$TIMEZONE" "$rootfs/etc/localtime"
您可以看到中使用的图像生成器脚本:
You can see that image builder script used in Digital Ocean: Alpine Linux:
- 确保Docker在本地运行。
-
下载并解压缩。
wget -O docker-alpine-master.zip https://github.com/gliderlabs/docker-alpine/archive/master.zip unzip docker-alpine-master.zip
-
构建构建器()。
export TIMEZONE=xxx docker build -t docker-alpine-builder docker-alpine-master/builder/
-
构建根文件系统(将v3.3更改为您要构建的Alpine版本)。
Build the root file system (change v3.3 to the Alpine version you want to build).
docker run --name alpine-builder docker-alpine-builder -r v3.4
-
从容器中复制根文件系统。
Copy the root file system from the container.
docker cp alpine-builder:/rootfs.tar.gz .
一旦您拥有 rootfs.tar.gz
在您自己的文件系统上,您可以使用它(作为),使用以下Dockerfile构建自己的Alpine映像:
Once you have the rootfs.tar.gz
on your own filesystem, you can use it (as mentioned here) to build your own Alpine image, with the following Dockerfile:
FROM SCRATCH
ADD rootfs.tar.gz /
建成后,您可以在正确的时区使用该Alpine图片。
Once built, you can use that Alpine image with the right timezone.
这篇关于如何使用gliderlabs / alpine:3.3在Dockerfile中设置时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!