Docker Timezone in Ubuntu 16.04 Image

2019-02-01 01:25发布

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?

8条回答
孤傲高冷的网名
2楼-- · 2019-02-01 01:58

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

查看更多
劫难
3楼-- · 2019-02-01 01:59

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
查看更多
贪生不怕死
4楼-- · 2019-02-01 02:01

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 !

查看更多
甜甜的少女心
5楼-- · 2019-02-01 02:03

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
查看更多
Anthone
6楼-- · 2019-02-01 02:07

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
查看更多
Melony?
7楼-- · 2019-02-01 02:08

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.

查看更多
登录 后发表回答