Is it possible change date in docker container?

前端 未结 6 1134
难免孤独
难免孤独 2020-12-01 02:35

I have a container with a running program inside tomcat. I need to change date only in this container and test my program behaviour. I have time sensitive logic, and sometim

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-01 03:17

    That's not possible with Docker. Docker uses the same clock as the outside kernel. What you need is full virtualization which emulates a complete PC.

    The sudo fails because it only makes you root of the virtual environment inside of the container. This user is not related to the real root of the host system (except by name and UID) and it can't do what the real root could do.

    If you use a high level language like Python or Java, you often have hooks where you can simulate a certain system time for tests or you can write code which wraps "get current time from system" and returns what your test requires.

    Specifically for Java, use joda-time. There you can inject your own time source using DateTimeUtils.setCurrentMillis*().

提交回复
热议问题