+12 votes
1 view
in Devops and Agile by (910 points)

My docker-compose file has some running docker files to create my containers. I don't want the docker files to set the timezones.

I have a docker-compose.override.yml file to make the local changes. One of my containers seems to not pull the host time zone and that is causing problems for me. Now, I want to enforce timezones on all my containers. In my Dockerfiles right now I am doing:

ENV TZ=Canada/Alex RUN ln -snf /user/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

How can I replicate this command for docker-compose syntax? 

4 Answers

+15 votes
by (10.6k points)
  • Try out the following code:

         I hope it will be helpful for you.

version "2"

services:
  serviceS:
    ...
    environment:
      TZ: "Canada/Alex"
    command: >
      sh -c "ln -snf /usr/share/zoneinfo/$TZ /etc/localtime &&
      echo $TZ > /etc/timezone &&
      exec my-application"

If you'd like to know more about Docker, you can visit Docker tutorial and join Docker Training.

by (29.8k points)
Hiii thanks for the answer this helps !!!
by (47.2k points)
Alexjoy has not asked this in the question for it but you can add 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)
+4 votes
by (95.3k points)

The simplest way to change the time in a Docker container is to change the time using ‘date’ command after connecting to the container.

docker exec -it container-name /bin/bash

date +%T -s "10:00:00"

by (33.2k points)
Thank you for this solution.
by (40.4k points)
This worked for me...Thank you for explaining it well.
by (29k points)
If you don't want dockerfile to set timezones this is the best way to achieve it.
0 votes
by (47.2k points)
reshown by
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)
0 votes
by (107k points)

Using the below command will be the simple solution for using docker-compose to set containers timezones:

environment: 

- TZ=America/Denver

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...