28

I have created a Docker container using the Ubuntu 16.04 image.

docker run -it -d --name containername -v /var/www/public --privileged ubuntu

after creating the container, I checked the date inside the container:

$ date
Tue Oct 25 08:10:34 UTC 2016

But, I need it to use the Asia/Kolkata timezone. So I tried changing the /etc/timezone file, then docker stop and docker start the container, but it doesn't work. It still shows the same time.

How can I change the time zone in the Docker container after creating it?

Daniel Serodio
  • 4,229
  • 5
  • 37
  • 33
Rajkumar .E
  • 1,440
  • 3
  • 20
  • 34

11 Answers11

37

Updating /etc/timezone is the usual way, but there's a bug in Xenial which means that doesn't work.

Instead you need to create a link from the desired timezone to etc/localtime:

FROM ubuntu:xenial     
RUN ln -fs /usr/share/zoneinfo/US/Pacific-New /etc/localtime && dpkg-reconfigure -f noninteractive tzdata
Elton Stoneman
  • 17,906
  • 5
  • 47
  • 44
  • Instead of Pacific-New, which one is for Mountain? – Jose Cabrera Zuniga Nov 20 '17 at 14:24
  • The only path my container has under `/usr/share/zoneinfo` is `/usr/share/zoneinfo/Etc/UTC`, so that does not work ;( – Andrew Savinykh Dec 27 '17 at 02:31
  • 2
    dpkg-query: package 'tzdata' is not installed and no information is available Use dpkg --info (= dpkg-deb --info) to examine archive files, and dpkg --contents (= dpkg-deb --contents) to list their contents. /usr/sbin/dpkg-reconfigure: tzdata is not installed – rjurney Jul 12 '18 at 17:12
  • echo $TZ > /etc/timezone would appear to be the ? debian way? . ln -sf /usr/share/zoneinfo/$TZ /etc/localtime is the CentOS/Redhat way. However mileage varies more with other OS and applications do not always use the OS timezone. Application service start scripts may need to be customized with the timezone. – gaoithe Jan 22 '19 at 17:31
31

In ubuntu 16.04 i was missing tzdata so i had to install it. Working solution was

    ENV TZ 'Europe/Tallinn'
    RUN echo $TZ > /etc/timezone && \
    apt-get update && apt-get install -y tzdata && \
    rm /etc/localtime && \
    ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && \
    dpkg-reconfigure -f noninteractive tzdata && \
    apt-get clean
qwerty
  • 1,451
  • 13
  • 10
  • Note that we need also to add `ENV DEBIAN_FRONTEND=noninteractive`. Otherwise, `apt-get install -y tzdata` will run in interactive mode. – jdhao Jun 10 '20 at 07:10
24

Try:

echo "Asia/Kolkata" > /etc/timezone
rm -f /etc/localtime
dpkg-reconfigure -f noninteractive tzdata

You have to do rm /etc/localtime because of the Ubuntu bug.

Mitar
  • 6,756
  • 5
  • 54
  • 86
13

As said here, the secret is that dpkg-reconfigure tzdata simply creates /etc/localtime as a copy, hardlink or symlink (a symlink is preferred) to a file in /usr/share/zoneinfo. So it is possible to do this entirely from your Dockerfile. Consider:

ENV TZ=America/Los_Angeles
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

And as a bonus, TZ will be set correctly in the container as well.

This is also distribution-agnostic, so it works with pretty much anything Linux.

Anthony O.
  • 22,041
  • 18
  • 107
  • 163
5

My issue has been solved with this very simple solution (https://serverfault.com/a/826222) : Add timezone in environment variable.

The command is docker run -e TZ=Europe/Amsterdam ...

Or, using docker-compose, like I do :

version: '3'
services:
    web:
        build: ./app
        ports:
            - ...
        volumes:
            - ...
        environment:
            - TZ=Europe/Paris

In my case, no more tzdata needed, or volume share with /etc/timezone & /etc/localtime.
Hope it helps !

Jacques Cornat
  • 1,612
  • 1
  • 19
  • 34
2

If you use docker-compose, just add one line to your docker-compose.yml file.

version: '3'

services:
  ubuntu-local:
    image: ubuntu:16.04
    restart: on-failure
    command: python3 run_my_code.py
    working_dir: /code
    volumes:
      - ./code:/code
      - /etc/localtime:/etc/localtime:ro   # <--add this line to set timezone
    environment:
      - PYTHONUNBUFFERED=1
Belter
  • 3,573
  • 5
  • 42
  • 58
  • 1
    tzdata package seems not installed under a purpose in official docker ubuntu images, for which it sound better to make usage of original docker mechanism. – 千木郷 Jun 09 '19 at 23:28
2

I took this approach:

  1. Copy file /etc/localtime somewhere.

  2. Open it and find the this number (highlighted with yellow) enter image description here

  3. -3 corresponds to Moscow time. For Berlin set -1 . If you need positive value, set UTC2

  4. Copy and modify /etc/timezone according to your time zone.

  5. Link them to your container

Result:

enter image description here

Tebe
  • 3,176
  • 8
  • 40
  • 60
1

SOLVED:

FROM ubuntu:16.04

RUN apt-get update && \
    apt-get install -y software-properties-common apt-utils locales tzdata

RUN echo "tzdata tzdata/Areas select Europe" > timezone.txt
RUN echo "tzdata tzdata/Zones/Europe select Rome" >> timezone.txt
RUN debconf-set-selections timezone.txt
RUN rm /etc/timezone
RUN rm /etc/localtime
RUN dpkg-reconfigure -f noninteractive tzdata
Flavio Troia
  • 2,451
  • 1
  • 25
  • 27
1

Simply map the volume while running docker container

-v /etc/timezone:/etc/timezone:ro

0

I am also experiencing this issue for a Ubuntu 18.04 docker container. Since tzdata package is not installed. There is no /usr/share/zoneinfo dir inside the docker. We need to first install tzdata and use dpkg-reconfigure to set the timezone. The following docker command works for me:

ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y --no-install-recommends tzdata \
    && rm -rf /var/lib/apt/lists/*
RUN ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && dpkg-reconfigure --frontend noninteractive tzdata
jdhao
  • 24,001
  • 18
  • 134
  • 273
0

Dockerfile:

RUN apt-get update && \
    apt-get install -yq tzdata && \
    ln -fs /usr/share/zoneinfo/America/New_York /etc/localtime && \
    dpkg-reconfigure -f noninteractive tzdata

Reference: https://dev.to/0xbf/set-timezone-in-your-docker-image-d22

Note: Need to run it as root user

Yakir GIladi Edry
  • 2,511
  • 2
  • 17
  • 16