DEV Community

Bo
Bo

Posted on

Set timezone in your docker image

TL;DR

RUN apt update && apt install tzdata -y
ENV TZ="America/New_York"
Enter fullscreen mode Exit fullscreen mode

Debian

When your image is based on Debian, or your image's root is based on Debian, you can use ENV TZ in Dockerfile:

FROM debian:10

ENV TZ="America/New_York"
Enter fullscreen mode Exit fullscreen mode

Ubuntu

When your image/root-image is based on Ubuntu, use:

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

If you don't know what your base image is, you could try putting both:

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

ENV TZ="America/New_York"
Enter fullscreen mode Exit fullscreen mode

Also quote from and credits to @peter279k in the comment below:

If using the ENV to set TZ to set the timezone, it should have the tzdata package installed on Linux distribution Docker base image.
After investigating some common Linux distributions, the Debian and CentOS have the tzdata installed on their Base Docker images.
And Ubuntu doesn't have the tzdata package on the Docker base image.

So another solution is:

RUN apt update && apt install tzdata -y
ENV TZ="America/New_York"
Enter fullscreen mode Exit fullscreen mode

Discussion (11)

Collapse
peter279k profile image
peter279k

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
0xbf profile image
Bo Author

Thank you, Peter. Question though, if the one line set env TZ works, what’s the benefits of using your long version ?

Collapse
peter279k profile image
peter279k

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
Thread Thread
0xbf profile image
Bo Author

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?

Thread Thread
peter279k profile image
peter279k • Edited

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.

Thread Thread
0xbf profile image
Bo Author

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.

Thread Thread
peter279k profile image
peter279k

If using the ENV to set TZ to set the timezone, it should have the tzdata package installed on Linux distribution Docker base image.

After investigating some common Linux distributions, the Debian and CentOS have the tzdata installed on their Base Docker images.

And Ubuntu doesn't have the tzdata package on the Docker base image.

Thread Thread
peter279k profile image
peter279k

That's the reason why I need to use apt-get update and apt-get install -yq tzdata firstly on my Dockerfile when using the Ubuntu 20.04 for my Docker base image.

Of course, we can simplify my Dockerfile when using the Ubuntu to be the Docker base image:

FROM ubuntu:20.04

RUN apt-get update && \
    apt-get install -yq tzdata

ENV TZ="Asia/Taipei"
Enter fullscreen mode Exit fullscreen mode
Thread Thread
0xbf profile image
Bo Author

Got it, thanks for the awesome explanation. πŸ‘

Collapse
kavindugeesara profile image
KavinduGeesara • Edited

Could you please tell me how to set on alpine base image?

my dockerfile is like this:

FROM alpine
ENV TZ="Asia/Colombo"

Collapse
0xbf profile image
Bo Author • Edited

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.