Set containers timezones by using docker compose.

+1 vote

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

How do I replicate the same command in docker-compose syntax?

Jul 8, 2018 in Docker by shubham
• 6,910 points

5 answers to this question.

+1 vote
Best answer
version "2"

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

I hope it would help.

answered Jul 8, 2018 by Atul
• 10,240 points

selected May 6, 2019 by Kalgi
+1 vote

The easiest 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"
answered May 6, 2019 by Atul
+1 vote

It should look something like this in the Dockerfile:

RUN apk add --no-cache tzdata
ENV TZ America/Los_Angeles
answered May 6, 2019 by Emilia
+1 vote

This should do the trick, simple and straight forward

  - TZ=America/Denver
answered May 6, 2019 by Umer
+1 vote

Share volume in docker-compose.yml. 

  image: imagename
    - "/etc/localtime:/etc/localtime:ro"
answered May 6, 2019 by Ekya

Related Questions In Docker

0 votes
1 answer

Interactive shell using Docker Compose

You need to include the following lines ...READ MORE

answered Jul 26, 2018 in Docker by Kalgi
• 51,950 points
0 votes
1 answer

How to get docker-compose to always re-create containers from fresh images?

The containers are getting recreated to preserve ...READ MORE

answered Jul 27, 2018 in Docker by Kalgi
• 51,950 points
+1 vote
4 answers

Docker compose, running containers in net:host

Hey@Hannah, Check the compose file format specifications ...READ MORE

answered May 7, 2019 in Docker by Kishore
+2 votes
1 answer
+2 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 57,640 points
0 votes
1 answer

Docker containers as Jenkins Slave build agents

When you encapsulate the stage inside a ...READ MORE

answered Jun 16, 2018 in Docker by Atul
• 10,240 points
+4 votes
4 answers

How To Access a Service on Host From a Docker Container?

Adding to kalgi's answer, You can also ...READ MORE

answered Oct 16, 2018 in Docker by lina
• 8,220 points

edited Oct 16, 2018 by lina 15,443 views