Search code examples
dockerazure-devopsazure-powershell

Unable to set timezone in a Dockerfile for a Windows container


I am new to dockerfile in Azure Devops. I am currently writing a dockerfile in which I need to set the timezone. Based on this dockerfile, I need an image to be created which already has the correct timezone. I used several attempts, but it keeps failing. I tried tzutil also, but it's not recognized during the creation of the image.

The image is being built by Azure DevOps agents.

# Base image
FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8 AS runtime

In my last attempt I tried to set the timezone like this.

RUN powershell -Command Set-TimeZone -Name "W. Europe Standard Time"

But if failed with the following exception

Set-TimeZone : A positional parameter cannot be found that accepts argument 'Eu
rope'.
At line:1 char:1
+ Set-TimeZone -Name W. Europe Standard Time1.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-TimeZone], ParameterBi 
   ndingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell 
   .Commands.SetTimeZoneCommand

Can anyone help me setting the timezone for this image?


Solution

  • Eventually I got this working with the following steps. First I create a powershell script that looks like this

    Write-Host "set W. Europe Standard Time"
    tzutil /s "W. Europe Standard Time"
    
    Write-Host "Starting IIS"
    C:\LogMonitor\LogMonitor.exe C:\ServiceMonitor.exe w3svc
    

    Next I tell the docker container not automatically startup IIS. I configure the Dockerfile to copy the Powershell script (see above) to the container. At the end of the dockerfile I startup the powershell script, which sets the timezone and will startup IIS

    # Base image
    FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8 AS runtime
    
    SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; Set-ExecutionPolicy Unrestricted -Force;"]
    
    # Disable IIS auto start
    RUN ["cmd", "/S", "/C", "sc", "config", "w3svc", "start=demand"]
    
    # Copy powershell script to change 
    RUN md c:\aspnet-startup
    COPY ContainerFiles/. c:/aspnet-startup
    
    #DO SOME OTHER STUFF THAT YOU NEED TO DO
    
    
    #Now start IIS with that script, which also sets the timezone
    ENTRYPOINT [ "powershell.exe", "c:\\aspnet-startup\\start.ps1" ]