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 9, 2018 in Docker by shubham
• 7,340 points
23,528 views

5 answers to this question.

+1 vote
Best answer
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"

I hope it would help.

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

selected May 7, 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 7, 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 7, 2019 by Emilia
+1 vote

This should do the trick, simple and straight forward

environment:
  - TZ=America/Denver
answered May 7, 2019 by Umer
+1 vote

Share volume in docker-compose.yml. 

ipchanger:
  image: imagename
  volumes:
    - "/etc/localtime:/etc/localtime:ro"
answered May 7, 2019 by Ekya

Related Questions In Docker

0 votes
0 answers
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
• 52,350 points
6,392 views
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
• 52,350 points
4,702 views
+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
• 58,020 points
2,714 views
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
2,431 views
+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 34,135 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP