TL;DR
RUN apt update && apt install tzdata -y
ENV TZ="America/New_York"
Enter fullscreen mode
Exit fullscre...
For further actions, you may consider blocking this person and/or reporting 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.If using the
ENVto set TZ to set the timezone, it should have thetzdatapackage installed on Linux distribution Docker base image.After investigating some common Linux distributions, the Debian and CentOS have the
tzdatainstalled on their Base Docker images.And Ubuntu doesn't have the
tzdatapackage on the Docker base image.That's the reason why I need to use
apt-get updateandapt-get install -yq tzdatafirstly on myDockerfilewhen using theUbuntu 20.04for my Docker base image.Of course, we can simplify my
Dockerfilewhen using theUbuntuto be the Docker base image:Got it, thanks for the awesome explanation. 👍
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.