Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+12 votes
2 views
in DevOps and Agile by (900 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.5k 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.

Using docker-compose to set containers timezones
Intellipaat-community
by (29.5k 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 (108k 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"

Using docker-compose to set containers timezones
Intellipaat-community
by (33.1k points)
Thank you for this solution.
by (40.7k points)
This worked for me...Thank you for explaining it well.
by (29.3k 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 (106k points)

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

environment: 

- TZ=America/Denver

Related questions

Browse Categories

...