You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
About time incorrect of docker image about aspnet-6 #62545
Hello:
I ran asp.net core application in docker container , used version of .Net 6 for mcr.microsoft.com/dotnet/aspnet:6.0-focal mirror versions. In my program, executed:
[Console.WriteLine (Datetime.Now.ToString("yyyy-MM-dd HH:mm:ss"))] ,the command output was [2021-11-26 12:58:12]. And then I entered the container and executed [date] command.The output was [Fri Nov 26 20:59:12 CST 2021].
That was almost an eight-hour difference. In.NET 5, there is no such problem.
Thanks, I can repro this now by including -v /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime with the docker run command.
@mattjohnsonpint - Is this a known change in behavior for .NET 6 to not use the /etc/localtime file? In .NET 6, setting /etc/localtime to /usr/share/zoneinfo/Asia/Shanghai causes the following time zone difference:
.NET 5: System.TimeZoneInfo.Local == (UTC+08:00) Coordinated Universal Time
.NET 6: System.TimeZoneInfo.Local == (UTC) Coordinated Universal Time
I have same issue. I've tried 6.0, 6.0.1-focal and bullseye but it didn't fixed.
We are using kubernetes, and we mount /etc/localtime as a hostPath to change timezone:
It is wrong to hostPath the timezone file. I explained before in my comment #62545 (comment). The file you are mounting was symbolically linked too inside the OS. That causes confusion in general when reporting the time. In .NET 5.0 was reporting the time from one zone and reporting the zone name from a different zone (UTC). so even it looks it was working in .NET 5.0, but the behavior for sure was wrong. The .NET 6.0 is more consistent and following the OS symbolic link which results in the expected result as how the time zone is setup on such container.
Please try to avoid using -v for the zone files and configure the zone in the container instead when launching it as you configure anything else.
I'd like to drop a friendly reminder here that it continues to remain a best practice (IMHO) for servers (and by extension containers) to be set to UTC, and for the applications running on them to be designed such that they are time zone agnostic.
If the application you're running requires the environment's time zone to be set to anything in particular, that's generally a smell that you should revisit what "local time" means in the context of your application. For .NET, some APIs that use the concept of "local time" are: DateTime.Today, DateTime.Now, DateTimeOffset.Now, TimeZoneInfo.Local, DateTimeKind.Local, DateTime.ToLocalTime, DateTimeOffset.ToLocalTime, DateTime.ToUniversalTime and TimeZoneInfo.ConvertTimeToUtc when the input Kind is Local or Unspecified, etc. Avoid those in your application and you won't have to worry about setting the server's time zone (or container's time zone).
As a brief example, instead of calling DateTimeOffset.Now, call TimeZoneInfo.ConvertTime(DateTimeOffset.UtcNow, someTimeZone). You can control someTimeZone any way you'd like in your application - either application-wide, or under some particular context.
Reacted by Tarek Mahmoud Sayed, Jacob Bakholt and Dan Moseley
@meirkr as mentioned earlier in this issue it is wrong to use -v to map time zone files. Please have a look at my comment #62545 (comment). The right thing to do here is to configure the container by setting time zones there using tools like timedatectl or any other way as you set it on VMs/machines.
.NET 6.0 behavior is right as the containers internally hard linking the time zone file and always report consistent zone info. Also, .NET 5.0 was wrong as indicated before it reports wrong data (like when you request the display name of the time zone).
I am closing this issue, please send any more questions if there is anything unclear here.
@meirkr as mentioned earlier in this issue it is wrong to use -v to map time zone files. Please have a look at my comment #62545 (comment). The right thing to do here is to configure the container by setting time zones there using tools like timedatectl or any other way as you set it on VMs/machines.
.NET 6.0 behavior is right as the containers internally hard linking the time zone file and always report consistent zone info. Also, .NET 5.0 was wrong as indicated before it reports wrong data (like when you request the display name of the time zone).
I am closing this issue, please send any more questions if there is anything unclear here.
But I don't need spcific TZ. I need the container to use host's TZ whatever it is, automatically.
What do you suggest to do in such case?
P.S
Pay attention in my previous comment, that "date" command within the container always showed the time using host's TZ. IMO, it expected from any other app running in the container to behave the same. .Net app shall not be an exception.
The physical location of the server should be irrelevant; requiring configuration of the host's timezone is going to cause problems/confusion if you move the server. For that matter, it can cause you problems if you want to start hosting this application for people (since it would require to use a container per customer/region). Server applications should be written to use timezones and other data from relevant entities referenced during web requests.
ghost locked as resolved and limited conversation to collaborators on Jun 2, 2022
Activity
mthalman commentedon Nov 29, 2021
I'm unable to repro this. Here are the steps I took to attempt to repro this:
git clone https://github.com/mthalman/bug-repros.git -b dotnet-docker-3307cd bug-repros/projectdocker build --pull -t test .docker run --rm testOutput:
2021-11-29 14:39:15docker run --rm --entrypoint date testOutput:
Mon Nov 29 14:39:54 UTC 2021Are you sure you don't have any other configuration in your Dockerfile that may be leading to this behavior? Please provide repro steps.
tonyjianhu commentedon Nov 29, 2021
when I run the container,I map the local time into it --use -v - /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime
mthalman commentedon Nov 29, 2021
Thanks, I can repro this now by including
-v /usr/share/zoneinfo/Asia/Shanghai:/etc/localtimewith thedocker runcommand.@mattjohnsonpint - Is this a known change in behavior for .NET 6 to not use the
/etc/localtimefile? In .NET 6, setting/etc/localtimeto/usr/share/zoneinfo/Asia/Shanghaicauses the following time zone difference:System.TimeZoneInfo.Local==(UTC+08:00) Coordinated Universal TimeSystem.TimeZoneInfo.Local==(UTC) Coordinated Universal TimeIs this behavior difference documented anywhere?
mthalman commentedon Dec 8, 2021
Triage: Moving this to dotnet/runtime repo to be investigated.
mattjohnsonpint commentedon Dec 8, 2021
Is
tzdatapackage installed in the container image?mthalman commentedon Dec 8, 2021
No, not by default. It doesn't exist in the 5.0 image either but it returns the expected result in that version.
mattjohnsonpint commentedon Dec 8, 2021
Not sure about that from memory, but
tzdatawould need to be installed to recognize whatAsia/Shanghaimeans.Also note the old display name was wrong.
mattjohnsonpint commentedon Dec 8, 2021
See also related dotnet/dotnet-docker#1366
mthalman commentedon Dec 8, 2021
Sorry about that, I was mistaken. Yes, tzdata is installed for both 5.0 and 6.0 on Debian/Ubuntu.
12 remaining items
oruchreis commentedon Jan 14, 2022
I have same issue. I've tried 6.0, 6.0.1-focal and bullseye but it didn't fixed.
We are using kubernetes, and we mount /etc/localtime as a hostPath to change timezone:
It was working for .net 3 and .net 5 until upgraded to .net 6 images. So I've tried @tarekgh workaround for .net 6 images, and it seems that worked:
I don't know why overwritten the localtime file in .net 6 image, but the hostPath mount was working before .net 6.
tarekgh commentedon Jan 14, 2022
It is wrong to hostPath the timezone file. I explained before in my comment #62545 (comment). The file you are mounting was symbolically linked too inside the OS. That causes confusion in general when reporting the time. In .NET 5.0 was reporting the time from one zone and reporting the zone name from a different zone (UTC). so even it looks it was working in .NET 5.0, but the behavior for sure was wrong. The .NET 6.0 is more consistent and following the OS symbolic link which results in the expected result as how the time zone is setup on such container.
Please try to avoid using
-vfor the zone files and configure the zone in the container instead when launching it as you configure anything else.mattjohnsonpint commentedon Jan 14, 2022
I'd like to drop a friendly reminder here that it continues to remain a best practice (IMHO) for servers (and by extension containers) to be set to UTC, and for the applications running on them to be designed such that they are time zone agnostic.
If the application you're running requires the environment's time zone to be set to anything in particular, that's generally a smell that you should revisit what "local time" means in the context of your application. For .NET, some APIs that use the concept of "local time" are:
DateTime.Today,DateTime.Now,DateTimeOffset.Now,TimeZoneInfo.Local,DateTimeKind.Local,DateTime.ToLocalTime,DateTimeOffset.ToLocalTime,DateTime.ToUniversalTimeandTimeZoneInfo.ConvertTimeToUtcwhen the inputKindisLocalorUnspecified, etc. Avoid those in your application and you won't have to worry about setting the server's time zone (or container's time zone).As a brief example, instead of calling
DateTimeOffset.Now, callTimeZoneInfo.ConvertTime(DateTimeOffset.UtcNow, someTimeZone). You can controlsomeTimeZoneany way you'd like in your application - either application-wide, or under some particular context.meirkr commentedon Apr 24, 2022
Happens also when mapping to the host's /etc/localtime.
Ubuntu
(GMT+3)
Example:
Starting a simple new console app for both targets 5 and 6:
Program.cs
Console.WriteLine("Now: " + DateTime.Now.ToString());tmpProj.csproj
Running
dotnet buildTest
.net 5 docker - works fine:
docker run --rm -it -v /etc/localtime:/etc/localtime:ro -v ~/tmp_net_time_docker/tmpProj/bin/Debug/net5.0/:/app mcr.microsoft.com/dotnet/aspnet:5.0-focal bashroot@4828c5d1344a:/# /app/tmpProj
Now: 04/24/2022 12:33:48
root@4828c5d1344a:/# date
Sun Apr 24 12:34:02 IDT 2022
.net 6 docker - BAD! we see the output doesn't include the GMT+3 of the host:
docker run --rm -it -v /etc/localtime:/etc/localtime:ro -v ~/tmp_net_time_docker/tmpProj/bin/Debug/net6.0/:/app mcr.microsoft.com/dotnet/aspnet:6.0 bashroot@f2fed401c0dc:/# /app/tmpProj
Now: 04/24/2022 09:35:17
root@f2fed401c0dc:/# date
Sun Apr 24 12:35:19 IDT 2022
danmoseley commentedon Apr 24, 2022
@MichaelSimons fyi
tarekgh commentedon Apr 24, 2022
@meirkr as mentioned earlier in this issue it is wrong to use
-vto map time zone files. Please have a look at my comment #62545 (comment). The right thing to do here is to configure the container by setting time zones there using tools liketimedatectlor any other way as you set it on VMs/machines..NET 6.0 behavior is right as the containers internally hard linking the time zone file and always report consistent zone info. Also, .NET 5.0 was wrong as indicated before it reports wrong data (like when you request the display name of the time zone).
I am closing this issue, please send any more questions if there is anything unclear here.
arashkarami commentedon May 2, 2022
We have same problem with net6.0 on the docker engine.
meirkr commentedon May 2, 2022
But I don't need spcific TZ. I need the container to use host's TZ whatever it is, automatically.
What do you suggest to do in such case?
P.S
Pay attention in my previous comment, that "date" command within the container always showed the time using host's TZ. IMO, it expected from any other app running in the container to behave the same. .Net app shall not be an exception.
Clockwork-Muse commentedon May 2, 2022
docker run --rm -e TZ=$(cat /etc/timezone) mcr.microsoft.com/dotnet/aspnet:6.0-focal date... but it's extremely unusual to care about host timezone at all on a server/in a container.
meirkr commentedon May 2, 2022
When running on premise dockerized apps....
I'll try that approach, hopefully to find a docker compose solution... tx.
Clockwork-Muse commentedon May 3, 2022
The physical location of the server should be irrelevant; requiring configuration of the host's timezone is going to cause problems/confusion if you move the server. For that matter, it can cause you problems if you want to start hosting this application for people (since it would require to use a container per customer/region). Server applications should be written to use timezones and other data from relevant entities referenced during web requests.