Get the App
SLTechnology News&Howtos  ›  Servers  › 

How does Dockerfile set the default time zone

Shulou Source: shulou.com Published: 2022-06-01 17:47:57 10月21日 Update

This article mainly introduces "how to set the default time zone in Dockerfile". In daily operation, I believe many people have doubts about how to set the default time zone in Dockerfile. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to set the default time zone in Dockerfile". Next, please follow the editor to study!

The basic images we use when packaging images are basically UTC (Green time), which is 8 hours different from our commonly used Beijing (Shanghai) time (CST). When creating a container, we can hang the directory files related to the host time zone on the container:

Docker run-- rm-it-v / etc/localtime:/etc/localtime ubuntu# enters the container, date test root@3e6c1d137e02:/# dateMon Aug 5 15:26:20 CST 2017

This method is relatively troublesome. Each time you create a new container, you need to mount the configuration directory / file related to the time zone of the host machine. To simplify this step, you can package a new image through Dockerfile and change the default time zone of the container. The following are implemented in three basic system images:

Ubuntu

To use ubuntu as the basic image, you need to download and install the tzdata package first. The default time zone is UTC time zone. Modify the configuration file, and reset the time zone configuration through dpkg-reconfigure. After the installation is completed, in order to minimize the physical strength of the mirror, delete all kinds of unnecessary files generated during the installation.

FROM ubuntu MAINTAINER fastjrun ENV TIME_ZONE Asia/Shanghai RUN sed-is @ / archive.ubuntu.com/@/mirrors.aliyun.com/@g / etc/apt/sources.list\ & & apt-get update\ & & apt-get install-y tzdata\ & & ln-snf / usr/share/zoneinfo/$TIME_ZONE / etc/localtime & & echo $TIME_ZONE > / etc/timezone\ & & dpkg-reconfigure-f noninteractive tzdata\ & & apt-get clean\ & & rm-rf / tmp/* / var/cache/* / usr/share/doc/* / usr/share/man/* / var/lib/apt/lists/*Alphine

Alphine claims to be the smallest Linux system image (only 5m). First, use the apk package manager to install the tzdata package and set up the relevant configuration files.

FROM alpineMAINTAINER igitlib RUN apk add-- no-cache tzdata\ & & cp / usr/share/zoneinfo/Asia/Shanghai / etc/localtime\ & & echo "Asia/Shanghai" > / etc/timezone\ & & apk del tzdataCentos

Centos is relatively simple, just add a configuration file.

FROM centos ENV TIME_ZONE Asia/Shanghai RUN ln-snf / usr/share/zoneinfo/$TIME_ZONE / etc/localtime at this point, the study on "how to set the default time zone for Dockerfile" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Time zone mirror file container configuration learning foundation host method time more directory system help practical minimum necessary next three physical strength Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MariaDB OPPO Reno Shulou Technology Docker