Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using docker-compose to set containers timezones

I have a docker-compose file running a few Dockerfiles to create my containers. I don't want to edit my Dockerfiles to set timezones because they could change at any time by members of my team and I have a docker-compose.override.yml file to make local environment changes. However, one of my containers (a Selenium based one) seems to not pull host time zone and that causes problems for me. Based on that I want to enforce timezones on all my containers. In my Dockerfiles right now I do

ENV TZ=America/Denver RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone 

And everything works fine. How do I replicate the same command in docker-compose syntax?

like image 597
Ben Nelson Avatar asked Aug 26 '16 18:08

Ben Nelson


People also ask

How do I change timezone in running container?

If you prefer, you can set the TZ variable when you start containers. Pass it as an environment variable to docker run . This lets you override an image's default timezone, provided it includes the tzdata package. An alternative to environment variables is the /etc/timezone file.


2 Answers

This is simple solution:

environment:   - TZ=America/Denver 
like image 147
MxWild Avatar answered Oct 11 '22 16:10

MxWild


version "2"  services:   serviceA:     ...     environment:       TZ: "America/Denver"     command: >       sh -c "ln -snf /usr/share/zoneinfo/$TZ /etc/localtime &&        echo $TZ > /etc/timezone &&       exec my-main-application" 

Edit: The question didn't ask for it but I've just added exec my-main-application to show how the main process would be specified. exec is important here to make sure that my-main-application receives Ctrl-C (SIGINT/SIGKILL).

like image 36
Bernard Avatar answered Oct 11 '22 15:10

Bernard