-
try to see the date within a container first :
docker exec -it container-id date
now you see the date result is the same as the OS date -
if you set OS date by
sudo date -s "2019-01-01T01:00:00" && date
in a test machine , then run command in #1 again, you will see container gets the new date as OS.
In one word, all containers share the same OS date or say clock
.
- to know the timezone inside a container :
docker exec -it container-id cat /etc/timezone
or useecho $TZ
if there is such ENV Variable inside the container. - to set timezone in a container
docker run -e TZ=America/New_York ubuntu date
- try to set date inside a container:
docker run -it -P --rm -v $(pwd):/code -e TZ="Asia/Shanghai" -e "NODE_ENV=production" --name taobao-sup-callback-dev node sh -ic ' date -s "2019-01-01T01:00:00" && date'
this returns error :
date: cannot set date: Operation not permitted 20190101
- search online and add
--privileged
to see if helps:
docker run -it --privileged -P --rm -v $(pwd):/code -e TZ="Asia/Shanghai" -e "NODE_ENV=production" --name taobao-sup-callback-dev node sh -ic ' date -s "2019-01-01T01:00:00" && date'
it does helps! but it changed the OS machine time!
so use date -s "2019-01-02T14:52:00"
in OS to change back the system time !
This proves that docker use the machine time and if allowed to change the time, it changes system time
another way works the same:
docker run -it --cap-add SYS_TIME --rm --name centos centos /bin/bash
-
so it seems impossible to change container time if you do not want to change machine time ?
I thought so.
But I have to make it work!
So I finally find my solution :
docker run -itd -P --rm -v $(pwd):/code -e TZ="America/Adak" -e "NODE_ENV=production" --name taobao-sup-callback-dev node sh -ic ' cd /code && DEBUG=* /code/node_modules/nodemon/bin/nodemon.js app.js > sup-log.txt'
网友评论