Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker container date/time totally different to host PC

Tags:

date

docker

time

When I run a docker container on my PC it has a totally different date/time to the host PC. See commands below. The time on the container recognizerDev is for the previous day, different hour, different minutes to the host. Any idea what is going on?

PS C:\Users\Bobby> date
11 October 2016 19:51:38

PS C:\Users\Bobby> docker exec recognizerDev date
Mon Oct 10 21:43:54 UTC 2016

When I try the same thing running on an AWS linux host the date/time is correct except for a 1 hour difference due to timezones.

Note that the first command returns the correct time/date in UTC+1 (London) as per my PC. The second command says it is in UTC but this cannot be right as if so it would return the same result less 1 hour.

like image 845
BobbyG Avatar asked Oct 11 '16 19:10

BobbyG


People also ask

How do I change the date and time in a Docker container?

The easiest way to change the time in a Docker container is to change the time using 'date' command after connecting to the container. Though the time zone change usually reflects immediately, in some cases, the container needs a restart for the time to change.

How do I change timezone in running container?

The directory /usr/share/zoneinfo in Docker contains the container time zones available. The desired time zone from this folder can be copied to /etc/localtime file, to set as default time. The containers created out of this Dockerfile will have the same timezone as the host OS (as set in /etc/localtime file).


3 Answers

This is only a partial answer (because it does not necessarily resolve the problem), but may help with diagnosis.

When you are running docker under Linux (as on your AWS host), you are just running processes on the host. That is, there isn't a substantial difference between docker run fedora ls vs running ls, except that the former has a slightly different view of system resources. The time reported in the container will always match the time reported on the host, modulo timezone settings.

When you running docker anywhere else (e.g., under Windows or MacOS), there is an additional layer in play: docker spawns a Linux virtual machine (historically using VirtualBox, although I think they may take advantage of other options these days) and then runs docker inside the virtual machine.

Because this is effectively a different machine from your host, it is possible for the time to drift. There are various ways of solving this sort of problem, including running ntp inside the virtual machine or running special guest agents that take care of keeping time in sync with the host. I don't know enough about how Docker configures these systems to know how or if they handle this explicitly.

If your docker vm has been running for a long period of time, simply restarting it may resolve the problem. Possibly docker machine restart is what you need.

like image 100
larsks Avatar answered Sep 28 '22 02:09

larsks


To some people looking on still how to correct the date and time, not sure if this will work to you but it happens on me using windows.

Just have to restart the locker, and once the docker booted up, it will sync time and time to the localhost machine :)

like image 35
Jaype Sison Avatar answered Sep 28 '22 02:09

Jaype Sison


If you don't want to restart the docker then the following script can synch time by and disabling and re-enabling time synch through Windows PowerShell

$vm = Get-VM -Name DockerDesktopVM

$feature = "Time Synchronization"

Disable-VMIntegrationService -vm $vm -Name $feature

Enable-VMIntegrationService -vm $vm -Name $feature

like image 44
Integration Avatar answered Sep 28 '22 03:09

Integration