Is it possible change date in docker container?

前端 未结 6 1142
难免孤独
难免孤独 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 03:33

    I created a Docker image containing libfaketime for use with Alpine but the process can be done in other distributions.

    Here's an example of using it Java using Groovy as an example. But Tomcat can be used as well.

    FROM groovy:alpine
    COPY --from=trajano/alpine-libfaketime  /faketime.so /lib/faketime.so
    ENV LD_PRELOAD=/lib/faketime.so \
        DONT_FAKE_MONOTONIC=1
    

    Then build and pass the FAKETIME environment variable when doing a docker run for example

    docker build -f fakedemo-java.Dockerfile . -t fakedemo
    docker run --rm -e FAKETIME=+15d fakedemo groovy -e "print new Date();"
    

    Source is in https://github.com/trajano/alpine-libfaketime and the docker image is in https://hub.docker.com/r/trajano/alpine-libfaketime

    I also created a variant of it based on Ubuntu https://github.com/trajano/ubuntu-faketime

提交回复
热议问题