Set timezone in your docker image BC on August 13, 2021 TL;DR RUN apt update && apt install tzdata -y ENV TZ="America/New_York" Debian When your imag... Read full post Submit Preview Dismiss Collapse Expand peter279k peter279k peter279k Follow I'm a open source project contributor from Taiwan. Location Taipei City Work Associate engineer at Institute for Information Industry Joined Apr 7, 2019 • Aug 14 '21 Dropdown menu Copy link Hide To set correct system timezone on Ubuntu 18.04 or 20.04 base image, it should use following Dockerfile: FROM ubuntu:18.04 RUN apt-get update && \ apt-get install -yq tzdata && \ ln -fs /usr/share/zoneinfo/Asia/Taipei /etc/localtime && \ dpkg-reconfigure -f noninteractive tzdata Enter fullscreen mode Exit fullscreen mode FROM ubuntu:20.04 RUN apt-get update && \ apt-get install -yq tzdata && \ ln -fs /usr/share/zoneinfo/Asia/Taipei /etc/localtime && \ dpkg-reconfigure -f noninteractive tzdata Enter fullscreen mode Exit fullscreen mode Collapse Expand BC BC BC Follow Coder, Learner Location New York Joined Nov 4, 2019 • Aug 14 '21 Dropdown menu Copy link Hide Thank you, Peter. Question though, if the one line set env TZ works, what’s the benefits of using your long version ? Collapse Expand peter279k peter279k peter279k Follow I'm a open source project contributor from Taiwan. Location Taipei City Work Associate engineer at Institute for Information Industry Joined Apr 7, 2019 • Aug 14 '21 Dropdown menu Copy link Hide Setting ENV TZ cannot be worked for setting system timezone. For example, using the following Dockefile: FROM ubuntu:20.04 ENV TZ="Asia/Taipei" Enter fullscreen mode Exit fullscreen mode When building above Docker image is done, running this image as container with interactive pseudo terminal: lee@lee-VirtualBox:~/test$ docker run -it test_timezone:latest bash root@b16f08e2d542:/# root@b16f08e2d542:/# date Sat Aug 14 03:18:44 Asia 2021 Enter fullscreen mode Exit fullscreen mode The timezone is correct, but current time is incorrect. Then using the following Dockerfile and build them to be the Docker image: FROM ubuntu:20.04 RUN apt-get update && \ apt-get install -yq tzdata && \ ln -fs /usr/share/zoneinfo/Asia/Taipei /etc/localtime && \ dpkg-reconfigure -f noninteractive tzdata Enter fullscreen mode Exit fullscreen mode After building above Docker image is done, running this as a container with interactive pseudo terminal: lee@lee-VirtualBox:~/test$ docker run -it test_timezone:tzdata bash root@9f778cfd6e1a:/# date Sat Aug 14 11:23:09 CST 2021 root@9f778cfd6e1a:/# Enter fullscreen mode Exit fullscreen mode BC BC BC Follow Coder, Learner Location New York Joined Nov 4, 2019 • Aug 14 '21 Dropdown menu Copy link Hide Ah, interesting, thank you. When I was using the "ENV TZ" method, my base image is FROM node:16-slim, and it worked perfectly when run date: Fri Aug 13 23:47:09 EDT 2021 Enter fullscreen mode Exit fullscreen mode Do you know why that works while FROM ubuntu:20.04 does not? Is it b/c the node:16 image already set the tzdata like you did? peter279k peter279k peter279k Follow I'm a open source project contributor from Taiwan. Location Taipei City Work Associate engineer at Institute for Information Industry Joined Apr 7, 2019 • Aug 14 '21 • Edited on Aug 14 • Edited Dropdown menu Copy link Hide The node:16-slim is based on Debian:10 image to build the Node.js version 16 environment. And it's worked for Debian 10 when setting the ENV TZ to set the timezone. I think setting ENV TZ is not the common way to set system timezone on every Linux distributions. BC BC BC Follow Coder, Learner Location New York Joined Nov 4, 2019 • Aug 14 '21 Dropdown menu Copy link Hide Hmm, you are right, just tried FROM debian:10 , the ENV TZ way works. Seems just not working for ubuntu. Thank you for pointing out. TIL. Collapse Expand Brent Groves Brent Groves Brent Groves Follow A simple programmer Work Python, React.js, and SQL development Joined Sep 15, 2022 • Sep 15 '22 • Edited on Sep 15 • Edited Dropdown menu Copy link Hide For ubuntu 22.04 I suggest blog.game-changing.de/how-to-set-t... The author explains the details but the gist is to include the following in your dockerfile. FROM ubuntu:22.04 ARG DEBIAN_FRONTEND=noninteractive ENV TZ Europe/Berlin RUN apt-get update \ && apt-get install -yq tzdata locales \ && rm -rf /var/lib/apt/lists/* \ && sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen \ && locale-gen \ && ln -fs /usr/share/zoneinfo/${TZ} /etc/localtime \ && dpkg-reconfigure tzdata ENV LANG en_US.UTF-8 ENV LANGUAGE en_US:en ENV LC_ALL en_US.UTF-8 CMD ["/bin/bash"] Collapse Expand KavinduGeesara KavinduGeesara KavinduGeesara Follow Joined Aug 31, 2021 • Aug 31 '21 • Edited on Aug 31 • Edited Dropdown menu Copy link Hide Could you please tell me how to set on alpine base image? my dockerfile is like this: FROM alpine ENV TZ="Asia/Colombo" Collapse Expand BC BC BC Follow Coder, Learner Location New York Joined Nov 4, 2019 • Sep 2 '21 • Edited on Sep 2 • Edited Dropdown menu Copy link Hide I think you need to install the tzdata first before setting the TZ, like ubuntu: FROM alpine:latest RUN apk update && apk add tzdata ENV TZ="America/New_York" Enter fullscreen mode Exit fullscreen mode This works for me. Code of Conduct • Report abuse
To set correct system timezone on Ubuntu 18.04 or 20.04 base image, it should use following
Dockerfile:Thank you, Peter. Question though, if the one line set env TZ works, what’s the benefits of using your long version ?
Setting ENV TZ cannot be worked for setting system timezone.
For example, using the following
Dockefile:When building above
Dockerimage is done, running this image as container with interactive pseudo terminal:The timezone is correct, but current time is incorrect.
Then using the following
Dockerfileand build them to be the Docker image:After building above Docker image is done, running this as a container with interactive pseudo terminal:
Ah, interesting, thank you. When I was using the "ENV TZ" method, my base image is
FROM node:16-slim, and it worked perfectly when rundate:Do you know why that works while
FROM ubuntu:20.04does not? Is it b/c the node:16 image already set the tzdata like you did?The
node:16-slimis based onDebian:10image to build the Node.js version 16 environment.And it's worked for
Debian 10when setting theENVTZ to set the timezone.I think setting
ENVTZ is not the common way to set system timezone on every Linux distributions.Hmm, you are right, just tried
FROM debian:10, theENV TZway works. Seems just not working for ubuntu. Thank you for pointing out. TIL.For ubuntu 22.04 I suggest blog.game-changing.de/how-to-set-t...
The author explains the details but the gist is to include the following in your dockerfile.
FROM ubuntu:22.04
ARG DEBIAN_FRONTEND=noninteractive
ENV TZ Europe/Berlin
RUN apt-get update \
&& apt-get install -yq tzdata locales \
&& rm -rf /var/lib/apt/lists/* \
&& sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen \
&& locale-gen \
&& ln -fs /usr/share/zoneinfo/${TZ} /etc/localtime \
&& dpkg-reconfigure tzdata
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
CMD ["/bin/bash"]
Could you please tell me how to set on alpine base image?
my dockerfile is like this:
FROM alpine
ENV TZ="Asia/Colombo"
I think you need to install the
tzdatafirst before setting the TZ, like ubuntu:This works for me.