[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[转帖]5种改变Docker容器时间的方法

上一篇:[GDS]Sabre的MultiPCC
下一篇:[备忘]keyStore和trustStore

添加日期:2020/1/16 9:38:31 快速返回   返回列表 阅读1476次
原文:https://bobcares.com/blog/change-time-in-docker-container/


Suppose you provide WordPress hosting using Docker containers to customers around the globe. Your customers would want to change time in Docker container to their time zones.

In our role as Server Support Specialists for web hosting companies and infrastructure providers, we provision and manage Docker systems for various business purposes.

Changing time in Docker container configuration is a task we perform as a part of this service. Here, we’ll see the different ways to do that.


How to change time in Docker container
The time in a Docker container can be changed in 5 ways. To know the current time, the ‘date’ command can be used.


docker exec -it container-id date



To know the timezone configured in a Docker container, the ‘ /etc/timezone ‘ file has to be checked.


docker exec -it container-id cat /etc/timezone



--------------------------------------------
1. Using date command
--------------------------------------------
The easiest way to change the time in a Docker container is to change the time using ‘date’ command after connecting to the container.


docker exec -it container-name /bin/bash
date +%T -s "10:00:00"



Though the time zone change usually reflects immediately, in some cases, the container needs a restart for the time to change.

 
--------------------------------------------
2. Using environment variables
--------------------------------------------
The timezone of a container can be set using an environment variable in the docker container when it is created.


docker run -e TZ=America/New_York ubuntu date



The time zone data package tzdata needs to be installed in the container for setting this timezone variable.

By configuring an NTP server, we ensure that the time zones in the containers are always synced.

 
--------------------------------------------
3. Using Dockerfile
--------------------------------------------
In hosting environment or cases which need too many identical containers to be spun up, the easiest way to manage is using Dockerfile.

The Dockerfile contains the basic configuration settings for each container. To change time in Docker container, the changes can be done in the corresponding Dockerfile.

The settings in the Dockerfile would reflect while recreating or adding a new container. And, all commands from the Docker file will be run as the root user. The entry in Dockerfile would look like:


RUN echo "Europe/Stockholm" > /etc/timezone
RUN dpkg-reconfigure -f noninteractive tzdata



The installation command for tzdata package would vary based on the OS image that the container is using.

We also update the entry point script of the containers to include the timezone settings. This ensures that the date would be set whenever the container is restarted.

 
--------------------------------------------
4. Using volumes
--------------------------------------------
A major issue with Docker containers is that the data in the container is not persistent over restarts. To work around this, we use Docker data volumes.

Data volumes in Docker machines are shared directories that contains the data specific to containers. The data in volumes are persistent and will not be lost during container recreation.

The directory ‘/usr/share/zoneinfo’ in Docker contains the container time zones available.  The desired time zone from this folder can be copied to /etc/localtime file, to set as default time.

This time zone files of the host machine can be set in Docker volume and shared among the containers by configuring it in the Dockerfile as shown.


volumes:
- "/etc/timezone:/etc/timezone:ro"
- "/etc/localtime:/etc/localtime:ro"



The containers created out of this Dockerfile (docker-compose.yml) will have the same timezone as the host OS (as set in /etc/localtime file) .

 
--------------------------------------------
5. Using images
--------------------------------------------
Manually changing time zone is not feasible when there are too many containers. To create more Docker instances with the same time zone, we use images.

After setting the desired time zone in a Docker container, we exit it and create a new image from that container using ‘docker commit’. NTP service is also configured in that image.


docker commit container-name image-name



Using this image, we can create one or more containers with the same time zone. The images are stored in repositories for future use.

 
--------------------------------------------
Conclusion
--------------------------------------------
Docker containers are widely used in DevOps and niche web hosting. Today we’ve discussed the various ways how our Docker Support Specialists change time in Docker container in the Docker infrastructure we manage.
 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved