After running a Ubuntu docker container, I found that the default time zone is UTC. I want to change it to my local timezone.

This is how to change the timezone when we are building the docker container:

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

Since we can enter input when building docker images, by setting DEBIAN_FRONTEND to noninteractive, we make sure apt-get will install tzdata package in a non-interactive way.

Package tzdata will install a lot of timezone files under /usr/share/zoneinfo directory. We then create a symlink to use Asia/Shanghai as the new timezone:

ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

Finally, we use dpkg-reconfigure to configure tzdata to use the new timezone:

dpkg-reconfigure --frontend noninteractive tzdata

The option --frontend noninteractive avoids the interactive prompt to choose area and city.

References