Raj*_* .E 14 datetime containers docker
我使用Ubuntu 16.04映像创建了一个Docker容器.
docker run -it -d --name containername -v /var/www/public --privileged ubuntu
Run Code Online (Sandbox Code Playgroud)
创建容器后,我检查了容器内的日期:
$ date
Tue Oct 25 08:10:34 UTC 2016
Run Code Online (Sandbox Code Playgroud)
但是,我需要它来使用亚洲/加尔各答时区.所以,我试图改变/etc/timezone的文件,然后docker stop和docker start容器,但它不工作.它仍然显示同一时间.
如何在创建Docker容器后更改时区?
Elt*_*man 26
更新/etc/timezone是常用方法,但Xenial中存在一个错误,这意味着无法正常工作.
相反,您需要创建从所需时区到etc/localtime以下链接的链接:
FROM ubuntu:xenial
RUN ln -fs /usr/share/zoneinfo/US/Pacific-New /etc/localtime && dpkg-reconfigure -f noninteractive tzdata
Run Code Online (Sandbox Code Playgroud)
小智 24
在ubuntu 16.04中我缺少tzdata所以我不得不安装它.工作解决方案是
ENV TZ 'Europe/Tallinn'
RUN echo $TZ > /etc/timezone && \
apt-get update && apt-get install -y tzdata && \
rm /etc/localtime && \
ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && \
dpkg-reconfigure -f noninteractive tzdata && \
apt-get clean
Run Code Online (Sandbox Code Playgroud)
Mit*_*tar 17
尝试:
echo "Asia/Kolkata" > /etc/timezone
rm -f /etc/localtime
dpkg-reconfigure -f noninteractive tzdata
Run Code Online (Sandbox Code Playgroud)
你必须这样做rm /etc/localtime是因为Ubuntu错误.
正如在这里说,秘诀就在于dpkg-reconfigure tzdata简单地创建/etc/localtime一个副本,固网或符号链接(符号链接最好),以在一个文件中/usr/share/zoneinfo.因此可以完全从Dockerfile中完成此操作.考虑:
ENV TZ=America/Los_Angeles
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
Run Code Online (Sandbox Code Playgroud)
作为奖励,TZ也将在容器中正确设置.
这也是与发行无关的,所以它适用于几乎任何Linux.
我的问题已经通过这个非常简单的解决方案解决了(https://serverfault.com/a/826222):在环境变量中添加时区。
命令是docker run -e TZ=Europe/Amsterdam ...
或者,像我一样使用 docker-compose :
version: '3'
services:
web:
build: ./app
ports:
- ...
volumes:
- ...
environment:
- TZ=Europe/Paris
Run Code Online (Sandbox Code Playgroud)
就我而言,不再需要 tzdata,或与 /etc/timezone 和 /etc/localtime 共享卷。
希望能帮助到你 !
| 归档时间: |
|
| 查看次数: |
26294 次 |
| 最近记录: |