Search code examples
.netdockertimentpazure-container-instances

How to Time Sync in Azure Container Instances


I have a long running container deployed to Azure Container Instances where the time within the container must be accurate. The time drift when the container has been running for 24 hours can be as much as 7 seconds, this causing me a problem with my application running inside.

My docker container used to build the app is Microsoft's standard .NET 6 container definition. What is the best method of keeping a container in sync with the real time ? Do I have to install NTP somehow in my container ? Any help would be appreciated :)

Here is my Dockerfile:

RUN apt-get update && apt-get install -y libgdiplus
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["TradingBot.csproj", ""]
RUN dotnet restore "./TradingBot.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "TradingBot.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "TradingBot.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TradingBot.dll"]```

Solution

  • Docker uses the same clock as the host and the docker cannot change it. It means that doing an ntpdate inside the docker does not work.

    The simplest solution appears to be to run your container with the -v /etc/localtime:/etc/localtime:ro option. Thus:

    #run without tz info:
    docker run --rm -t -i ubuntu date
    Wed Apr  2 18:40:07 UTC 2014
    # run with tz info:
    docker run --rm -t -i -v /etc/localtime:/etc/localtime:ro ubuntu date
    Wed Apr  2 11:40:29 PDT 2014.
    

    Would suggest you to Refer these Thread1 and Thread2 for Troubleshooting your issue if above solution is not working