docker / for-win

Bug reports for Docker Desktop for Windows
https://www.docker.com/products/docker#/windows
1.86k stars 289 forks source link

time is wrong in container running inside WSL2 backend #10347

Open fanoush opened 3 years ago

fanoush commented 3 years ago

Actual behavior

after running container with WSL2 backend for several days the time is wrong. If suspend/hibernate is used it is even off by days. Today I noticed this issue and date inside container was off by 5 days!

Expected behavior

time is correct inside docker container

Information

There is related WSL2 bug https://github.com/microsoft/WSL/issues/5324 going on for quite some time Easy workaround is to run ntpdate or hwclock -s inside WSL2.

Please add running this periodically to your "docker-desktop" WSL2 distro so that time inside containers is correct.

Or can you possibly suggest how users can add this 'cron job' to their docker-desktop wsl2 distro themselves?

Tested with docker desktop community 3.1.0

stephen-turner commented 3 years ago

Microsoft have said in microsoft/WSL#6521 that they are fixing this. I'm a bit reluctant to start adding workarounds in our code if it's going to be fixed in the right place.

fanoush commented 3 years ago

Well, but that probably means waiting for next windows 10 version for WSL refresh so can take very long to get to users using docker now (after they actually fix it, which can be tricky considering the nature of the bug - can involve multiple parts - windows, hyper-v, linux kernel).

If you decide to not add it, can you possibly suggest workaround how users can add this to their docker-desktop wsl2 distro themselves? Many people using docker for windows don't have another wsl distro installed to run this workaround there.

fanoush commented 3 years ago

oh, just found that I can run wsl -d docker-desktop to enter docker wsl2 distro and get root shell. And hwclock is there so I can even run this from batch file or command window

wsl -d docker-desktop -e /sbin/hwclock -s
Danielovich commented 3 years ago

This is an issue due to your machine having been in sleep mode and the OS and WSL is not syning time there after.

WSL - - shutdown in an admin cmd should fix it s a workaround

fanoush commented 3 years ago

Thanks but both the cause and the workaround was already mentioned in initial description of the issue. As for wsl --shutdown

wsl --help
Copyright (c) Microsoft Corporation. All rights reserved.
....
    --shutdown
        Immediately terminates all running distributions and the WSL 2 lightweight utility virtual machine.

so that would kill all my docker containers immediately and also other WSL stuff currently running. Not really a good idea. Much better workaround is mentioned in the comment just above yours, that will keep everything still running. I verified the time inside container really changes and running processes notice the change and catch up with the time.

BTW, below is piece of powershell that would test if docker WSL2 distro is actually running before running hwclock -s in it. It was a bit tricky since wsl.exe output is unicode which confuses cmd.exe (FOR /F %i IN ('wsl -l -q --running') DO @echo %i does show only first letter of first line and then terminates) and powershell is confused too unless one sets output encoding properly there.

[Console]::OutputEncoding = [System.Text.Encoding]::Unicode; foreach ($i in (& wsl.exe -l -q --running)) { if ($i -eq "docker-desktop") { & wsl.exe -d $i /sbin/hwclock -s }}

One can put this into scheduled task to run periodically or after resume from sleep.

fanoush commented 3 years ago

I have set scheduled task to run this after resume as per this article and it runs fine and solves the issue. image

the powershell.exe command line is like this

-NoProfile -NonInteractive -WindowStyle Hidden -command "[Console]::OutputEncoding = [System.Text.Encoding]::Unicode; foreach ($i in (& wsl.exe -l -q --running)) { if ($i -eq 'docker-desktop') { & wsl.exe -d $i /sbin/hwclock -s }}"
docker-robott commented 3 years ago

Issues go stale after 90 days of inactivity. Mark the issue as fresh with /remove-lifecycle stale comment. Stale issues will be closed after an additional 30 days of inactivity.

Prevent issues from auto-closing with an /lifecycle frozen comment.

If this issue is safe to close now please do so.

Send feedback to Docker Community Slack channels #docker-for-mac or #docker-for-windows. /lifecycle stale

fanoush commented 3 years ago

/remove-lifecycle stale /lifecycle frozen