Docker Timezone in Ubuntu 16.04 Image

前端 未结 10 1741
借酒劲吻你
借酒劲吻你 2020-12-13 10:40

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

docker run -it -d --name containername -v /var/www/public --privileged ubuntu
相关标签:
10条回答
  • 2020-12-13 10:49

    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
    
    0 讨论(0)
  • 2020-12-13 10:53

    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
    
    0 讨论(0)
  • 2020-12-13 10:53

    I took this approach:

    1. Copy file /etc/localtime somewhere.

    2. Open it and find the this number (highlighted with yellow)

    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:

    0 讨论(0)
  • 2020-12-13 10:55

    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.

    0 讨论(0)
  • 2020-12-13 10:59

    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 !

    0 讨论(0)
  • 2020-12-13 11:00

    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
    
    0 讨论(0)
提交回复
热议问题