Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a Docker container whose timezone matches that of my local machine?

I'm using Docker 19. I have the following Dockerfile. Notice how I set the time zone to US EST ...

FROM microsoft/mssql-server-linux:latest
  
RUN apt-get update
RUN apt-get install unzip -y

RUN apt-get install tzdata
ENV TZ=America/New_York
RUN ln -fs /usr/share/zoneinfo/$TZ /etc/localtime && dpkg-reconfigure -f noninteractive tzdata
...

Is there any way I can author my file such that the time zone gets set to the local machine that it's running on? I'm on Central time zone but have team mates in other time zones. If it's useful, the above is part of a docker-compose file that looks roughly like this ...

version: "3.2"
services:

  sql-server-db:
    build: ./
    container_name: sql-server-db
    image: microsoft/mssql-server-linux:2017-latest
    ports:
      - 1433:1433

Some of us are on Macs, but it's possible for folks to be on Windows or Ubuntu. If it's easier you can consider that everyone is on Macs.

like image 319
Dave Avatar asked Jul 30 '20 20:07

Dave


People also ask

What timezone does Docker use?

Docker containers don't inherit the host's timezone, so you can run into unexpected scheduling issues that wreak havoc with your application. The container is using the UTC timezone, creating a one hour difference between the two times.

How do I find the container time zone?

The directory /usr/share/zoneinfo in Docker contains the container time zones available. The desired time zone from this folder can be copied to /etc/localtime file, to set as default time. The containers created out of this Dockerfile will have the same timezone as the host OS (as set in /etc/localtime file).


2 Answers

using docker run command:

-e TZ=`ls -la /etc/localtime | cut -d/ -f8-9`

Source

if you still want to use volumes you need to share /etc form the Docker UI in your MAC "Prefernces --> Resources --> FILE SHARING"

Update

for docker-compose :

under build section use:

args:
  - TZ

and then:

environment:
    - TZ=${TZ}

and then start it like - after re-build -:

export TZ=`ls -la /etc/localtime | cut -d/ -f8-9` && docker-compose up -d --build
like image 182
LinPy Avatar answered Oct 10 '22 11:10

LinPy


you can set the time zone whatever you want in your host machine then you can map your local time to the container through volume mapping. Like this,

volumes:
    - /etc/localtime:/etc/localtime

For mac the location of localtime could be different. You can mount that location through volume mapping.

like image 45
Bhargav Tavethiya Avatar answered Oct 10 '22 10:10

Bhargav Tavethiya