Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Best way to set the timezone #12084
Comments
|
You need to do that when you run the container with -v On Saturday, April 4, 2015, Dreamcat4 notifications@github.com wrote:
|
|
If you need help with using docker please see #docker on freenode or On Saturday, April 4, 2015, Jessica Frazelle jess@docker.com wrote:
|
jessfraz
closed this
Apr 4, 2015
dreamcat4
commented
Apr 4, 2015
|
@jfrazelle So are you saying that regular docker volumes work for single files from the cmd line, but not in a Dockerfile?. If that is true then am pretty surprised by that. I was under the impression that only worked for bind mounted volumes (you didn't say). But regardless if that were true, I believe such way would not be as suitable / convenient for my specific intended usage. As it would be inconsistent when setting up all of the other config data for my app from within that particular Dockerfile. I now think (sorry, after filing my original report) that there may be another way to circumvent / work around the limitation. All from within the Dockerfile(s). Just by making a new folder and then symlinking the 2 individual files on to their proper location in the underlying container. Then mounting a volume over that folder. Example: base Dockerfile: FROM ubuntu-debootstrap:14.04
RUN mkdir /tz && mv /etc/timezone /tz/ && mv /etc/localtime /tz/ \
&& ln -s /tz/timezone /etc/ && ln -s /tz/localtime /etc/localized dockerfile: FROM base
# Set the time zone
RUN echo "Europe/London" > /etc/timezone && dpkg-reconfigure -f noninteractive tzdata \
&& cp /etc/timezone /tz/ && cp /etc/localtime /tz/
VOLUME /tzIn other words, in the base image those files are missing or set to the distro's generic default. However the symlinks are created to [EDIT] It is useful for the time zone data, as that is an often-localized part of the image. When generating variants for different countries / region. Which is a common thing for many apps (including whenever setting up PHP etc). |
dreamcat4
commented
Apr 4, 2015
|
Right. Well that work-around did not keep things all so simple in the end. Perhaps at run-time may be overall a more elegant way to set the time zone. Or like this: |
dreamcat4
commented
Apr 5, 2015
|
Ok. I have determined that actually there is no foolproof elegant way to set the time zone inside of a docker container. So have finally settled on this solution: App dockerfile: # Relocate the timezone file
RUN mkdir -p /config/etc && mv /etc/timezone /config/etc/ && ln -s /config/etc/timezone /etc/App entrypoint script: # Set timezone as specified in /config/etc/timezone
dpkg-reconfigure -f noninteractive tzdataData volume # Set the time zone
RUN echo "Europe/London" > /config/etc/timezone... it is not elegant b/c involving 3 seperate files, and re-creating |
dreamcat4
changed the title from
Can't mount single file as a regular docker volume
to
Best way to set the timezone
Apr 5, 2015
|
I mount /etc/localtime in my images so it is in sync w my host -v On Saturday, April 4, 2015, Dreamcat4 notifications@github.com wrote:
|
dreamcat4
commented
Apr 5, 2015
|
@jfrazelle Yes that is the most popular way. And I also had been doing that myself until now. However it is not correct b/c does not work when the software requires instead the file You can just declare another volume for the other file. But kinnda messy. And you have to remember to keep including those 2 volumes for every My situation: Also needs other localization changes not just only the time zone. e.g. to set the language, set the currency, date format and so on. Then it makes sense to do all of those things in one place. For that So I have separate Dockerfile(s) to seed the app's initial configration data. Where all of the relevant localisation is set. One for me and other users can easily make for themselves. This is the Data Containers paradigm. When we create the data container with certain initial data - that is also an opportunity to set these things. And remains seperate from the App Image. Then we run our app with |
darkermatter
commented
Jun 2, 2015
|
@jfrazelle |
dreamcat4
commented
Jun 2, 2015
|
Hello again. i've just uploaded a real-world example of my solution.
https://github.com/dreamcat4/docker-images/blob/master/tvh/master/Dockerfile#L36-45
https://github.com/dreamcat4/docker-images/blob/master/tvh/master/entrypoint.sh#L6-7
https://github.com/dreamcat4/docker-images/blob/master/tvh/config/Dockerfile#L8-20 And that workaround (for me) solves my timezone issue. At least, so long as i've got other kinds things to put in the config volume. Then it's no extra hassle. I can appreciate that just purely for timezone only, that would be rather an overkill. |
chrisbutler
referenced this issue
in arunoda/meteor-up-legacy
Jul 15, 2015
Open
docker doesn't know about server localtime #535
pecigonzalo
commented
Aug 27, 2015
|
How come this is still not marked as feature/bug whatever? i believe its pretty critical for production systems... and everyone is rolling its own workaround/hack to make it work based on |
MichaelBitard
referenced this issue
in fchauveau/rpi-domoticz-docker
Nov 13, 2015
Closed
Hour is not correct #3
jjfraney
commented
Nov 27, 2015
|
use TZ environment variable. In vanilla unix, the libraries will observe this, the presence/content of /etc/timezone is irrelevant. [jfraney@openldap-testvm ~]$ sudo docker run -i -t -e TZ=UTC centos /bin/bash [jfraney@openldap-testvm ~]$ sudo docker run -i -t -e TZ=Europe/London centos /bin/bash |
darkermatter
commented
Nov 27, 2015
|
@jjfraney Thanks for that. It works great on bare metal. However it doesn't seem to work on boot2docker. It looks like b2d assumes the host time is UTC :-( |
jjfraney
commented
Nov 27, 2015
|
@DarkMatter. Some docker containers won't have access to tz data, and so TZ will have no effect. The b2d MAY still be setting the TZ environment correctly, or as you say, it may not. This is not really about what b2d 'assumes'....its only about making sure the TZ environment variable is conveyed to the process running in the container. If b2d can export any variable, it can export TZ. Try this to isolate, though. This busybox container does not handle TZ....there is no tzdata installed, so it doesn't know how to localize the time. The result of date command does not show what is intended (time localized to Chicago), but the TZ variable is set correctly so b2d did its job. (I used b2d-1.9.0 via docker toolbox.) docker@default:~$ docker run -it -e TZ=America/Chicago busybox /bin/sh |
darkermatter
commented
Nov 28, 2015
|
@jjfraney I completely agree. Your method works perfectly on Ubuntu (and all LSB distros?) and b2d is passing in the TZ variable. I just want to figure out why it's assuming the time on my Mac is UTC. The first date command is run on my Mac. Note the Madrid time.
|
stayclassychicago
commented
Dec 8, 2015
|
fwiw- wanted to share the amalgamated approach we've taken to handle this: |
This was referenced Dec 14, 2015
rdantassilva
commented
Aug 26, 2016
|
The best solution i consider and use is to define an environment variable in your dockerfile as bellow: Or run docker specifying the timezone like this: |
rpicheta
commented
Sep 28, 2016
•
|
For me it works in Dockerfile ps. if You are NOT ROOT you have to add 'sudo' before 2nd (before 'tee') and 3rd line at the beginning. |
dreamcat4
commented
Sep 28, 2016
|
Haven't they changed things in ubuntu 16.04? Not sure this works for that distro anymore. |
rpicheta
commented
Sep 28, 2016
•
|
@dreamcat4 it works on for mysql image version 5.6
|
siddontang
referenced this issue
in pingcap/tikv
Oct 6, 2016
Closed
log time is incorrect in docker #1135
MaxiReglisse
commented
Oct 13, 2016
|
it works for me |
sisao
commented
Nov 2, 2016
|
@dreamcat4 found this http://stackoverflow.com/a/40235306. Seems to work for me with ubuntu 16.04 guest... |
This was referenced Dec 12, 2016
joranbeasley
commented
Feb 14, 2017
|
a day late but how bout just
that seemed to work great for me |
moacirosa
commented
May 4, 2017
|
Comment above seems to work corectly but not for all images. Ubuntu 16.04+ based, for example, is affected by a tzdata package bug as reported here. |
andho
commented
May 8, 2017
|
Latest versions of Ubuntu image doesn't contain the tzdata package by default: So you should install tzdata in Dockerfile first. If the TZ variable is set, installing a package that depends on tzdata (eg. php) will auto configure it upon installation. Therefore, just setting the TZ env variable seems to work. The /etc/timezone and /etc/localtime is still using UTC but If you really want to have the correct values in /etc/timezone and /etc/localtime, then following contents in Dockerfile seems to do the trick:
I've put this here as this is the first result that appears when searching for how to set tz in docker. |
dreamcat4 commentedApr 4, 2015
Hello.
I would very much like to do this:
But unfortunately docker isn't having any of it.