docker container and the host vessel Time
docker container host synchronization time
The default time and UTC time the container is the difference between the host 8 hours
Resolve into the container
export TZ=Asia/Shanghai #修改时区
- When you create a container designated startup parameters, to automatically mount a container file localtime
docker run --name <name> -v /etc/localtime:/etc/localtime:ro ....
- Set the time zone is added to the Dockerfile
# CentOS
RUN echo "Asia/shanghai" > /etc/timezone;
# Ubuntu
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
Modifying Containers time
Testing students, because of the special scene, want to change my service time corresponding container
startup parameters participate --cap-add SYS_TIME
refer to the official documentation https://docs.docker.com/engine/reference/run/
docker run -it --cap-add SYS_TIME --name centos centos:7 /bin/bash
You can modify the container time, but when you modify the host container time time will change
even though this is not a joke can change in time test environment, the host of the way
Modify only time docker container, without affecting the host time
docker containers are lightweight, they share the same linux kernel, and the time & date is a function of kelnel all modifications docker container of time will cause the host time to be modified
There is a libfaketime project on github, we can use the LD_PRELOAD environment variable to the date the project is compiled libraries connected to "fool" the application, the purpose of modifying docker container time, specific practices:
1.pull items:
mkdir libfaketime && cd libfaketime && git init
git pull https://github.com/wolfcw/libfaketime.git
2 Use the make command to compile the code under the src, get DLL file: /usr/local/lib/faketime/libfaketime.so.1
3 Run docker exec -it. <Containter_id | containter_name> / bin / bash into the container
4 using the following command to modify the run-time linker, and set for 2 days before.
export LD_PRELOAD=/usr/local/lib/faketime/libfaketime.so.1 FAKETIME="-2d"
#取消的话, 环境变量设置空
# 时间设置 libfaketime
#!/usr/bin/env bash
export LD_PRELOAD=/usr/local/lib/faketime/libfaketime.so.1
#export FAKETIME="2020-12-24 20:30:00" #该时间会一直保持不变
export FAKETIME="@2020-12-24 20:30:00" #时间会从这里往后递增
Modified container, the files are copied into the corresponding container libfaketime.so.1
docker cp /usr/local/lib/faketime/libfaketime.so.1 9a5150123100: / usr / local / lib / faketime /
to 5 days before
export LD_PRELOAD = / usr / local / lib / faketime / libfaketime.so.1 FAKETIME = "- 5d"
#从硬件时间获取
hwclock
#硬件时间设置到系统上来
hwclock -s