dinesh707
dinesh707

Reputation: 12592

How to force time into Docker container using docker-compose

Here is my docker compose file

version: '2'
services:
  client:
    build: .
    container_name: client
    depends_on:
      - mysql
    environment:
      MYSQL_USER: api
      MYSQL_PASSWORD: api
      MYSQL_ROOT_PASSWORD: root
  mysql:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USER: api
      MYSQL_PASSWORD: api
    ports:
      - "3306:3306"

I'm running tests on the client container (running debian).
The test depends on the dates since MySQL data is created on a given date.
So, I would like to enforce date into the container (both containers) so that date in client and now() in mysql returns a given older date.

EDIT : When i run the docker container, it gets the current time to the linux image and also to the database. Is there any way I can set those values to be 2014.01.01 (some specific date), which does not change. So the behavior inside the containers are always same

Upvotes: 0

Views: 1518

Answers (2)

Stobor
Stobor

Reputation: 65

If you need a particular timezone and not a specific time you could use TZ variable

docker run --env TZ=${TIMEZONE}

https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

Upvotes: 0

Elton Stoneman
Elton Stoneman

Reputation: 19184

Docker containers inherit the system date from the host - you can't manually change it inside the container, e.g. this will fail to build:

FROM ubuntu
RUN date -s '2014-01-01 00:00:01'

What you can do is install an NTP client inside the container, and point it to a different NTP server from the host. Then all you need to do is write a stub NTP server that always returns a fixed date and time...

Upvotes: 1

Related Questions