2019-01-02 how to change date wi

2019-01-02  本文已影响6人  五大RobertWu伍洋
  1. 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

  2. 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.

  1. to know the timezone inside a container :
    docker exec -it container-id cat /etc/timezone
    or use echo $TZ if there is such ENV Variable inside the container.
  2. to set timezone in a container
docker run -e TZ=America/New_York ubuntu date
  1. 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

  1. 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

  1. 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'

上一篇 下一篇

猜你喜欢

热点阅读