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,870 points
1,849 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 8, 2018 by Atul
• 10,240 points

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

This should do the trick, simple and straight forward

environment:
  - TZ=America/Denver
answered May 6 by Umer
+1 vote

Share volume in docker-compose.yml. 

ipchanger:
  image: imagename
  volumes:
    - "/etc/localtime:/etc/localtime:ro"
answered May 6 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
• 37,320 points
545 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
• 37,320 points
254 views
+1 vote
4 answers

Docker compose, running containers in net:host

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

answered May 7 in Docker by Kishore
1,299 views
0 votes
1 answer

docker set iptables options in docker-compose.yml

The --iptables option only applies to the ...READ MORE

answered Aug 2, 2018 in Docker by Kalgi
• 37,320 points
264 views
0 votes
1 answer
0 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
• 56,160 points
99 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
563 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,100 points

edited Oct 16, 2018 by lina 3,232 views