Search code examples
dockerdocker-composetimezone

Docker container time zone remains as UTC


I have been working through setting my ms-sql docker container to use the Linux host date time settings. I'm using a docker-compose file, and for the most part the solution appears to be to map volumes like so (references: Bobcares, SO ):

volume:
  - /etc/timezone:/etc/timezone:ro
  - /etc/localtime:/etc/localtime:ro

At first glance, this looks fine. I can check what date the container thinks it is, and it reflects the host local time zone. The SQL instance, however, is still displaying UTC time for GetDate().

There is a symbolic link within the container though, that always looks like this: localtime -> /usr/share/zoneinfo/Etc/UTC. This is not what I am expecting based on the volume mapping, as it is different from the host localtime -> ../usr/share/zoneinfo/Australia/Sydney. I can manually update this link within the container and GetDate() on SQL returns the correct local datetime. All is lost on a docker-compose down, as expected.

I need this to persist across container restarts, and I want to use the volume from the docker-compose file to handle time zones as we deploy to multiple time zones. I'm sure the problem is my noob linux skillset, so happy for advice on how to improve this. How do I fix the symbolic link problem (if that is indeed the root cause of the problem)?

Host is: Ubuntu 20.04.1 LTS (Focal Fossa) ms-sql image is: mcr.microsoft.com/mssql/server:2019-latest


Solution

  • I kept searching and I found the answer in this post.

    Full steps I needed:

    • Set timezone of host
    • add TZ enviroment variable to docker-compose.yml
    • add volume mapping for host time zone to docker-compose.yml

    My docker-conpose section for ms-sql now looks like this (some parts omitted for brevity):

    ms-sql-server:
      image: damo/sqlexpress:1.0.1
      ports: 
        - "14333:1433"
      volumes: 
        - sqldata:/var/opt/mssql
        - /etc/timezone:/etc/timezone:ro
        - /etc/localtime:/etc/localtime:ro
      restart: unless-stopped
    

    The only change I need to make between deployments is the environment variable.