Docker: how to sync date between a CentOs host and a Debian container

dockerdocker-composetimezone

I have a CentOs host running a docker Debian container.

The container has the wrong localtime and timezone: how can I synchronize it with the host date?

I'm trying with mounting volumes on the docker-compose with /etc/localtime but it doesn't work.

The solution I prefer should be one of them:

  • set a particular ENV in the Dockerfile
  • mount properly volumes in the docker-compose.yml

Best Answer

You can add your local file /etc/localtime as volume in your docker-container. Update your docker-compose.yml with the following lines.

volumes:
    - "/etc/localtime:/etc/localtime:ro"

And, Then:

# docker-compose up -d

Now the container time is the same as on your host.