Last active
June 2, 2021 13:03
-
-
Save artisticcheese/e4776f420f1d0088d87335284b103cce to your computer and use it in GitHub Desktop.
Dockerfile to build IIS + nanoserver + ASP.NET core
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# escape=` | |
# Image with NET CORE installation to extract executables for final image | |
FROM microsoft/aspnetcore:2.0.0-nanoserver As CoreBuild | |
# Middleware image used to extract ASP.NET core module | |
From microsoft/iis as WindowsBuild | |
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'Continue'; $verbosePreference='Continue';"] | |
# Installing NET CORE webhosting in middleware image so latest module and configuration schema is extracted for final image | |
ADD https://download.microsoft.com/download/B/1/D/B1D7D5BF-3920-47AA-94BD-7A6E48822F18/DotNetCore.2.0.0-WindowsHosting.exe ".\hosting.exe" | |
RUN start-process -Filepath .\hosting.exe -ArgumentList @('/install', '/quiet', '/norestart') -Wait | |
FROM microsoft/iis:nanoserver | |
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'Continue'; $verbosePreference='Continue';"] | |
RUN start-process -Filepath dism.exe -ArgumentList @('/online', '/enable-feature:IIS-WindowsAuthentication', '/ALL') -Wait | |
# Adding NET CORE EXE to Path environment variable | |
RUN setx /M PATH $($Env:PATH + ';' + $Env:ProgramFiles + '\dotnet') | |
#Copy dotnet core installation from middleware image | |
COPY --from=CoreBuild ["c:\\Program Files\\dotnet", "c:\\program files\\dotnet"] | |
#Copy dotnet core module from middleware image | |
COPY --from=WindowsBuild ["c:\\Windows\\System32\\inetsrv\\aspnetcore.dll", "c:\\Windows\\System32\\inetsrv\\"] | |
COPY --from=WindowsBuild ["c:\\Windows\\System32\\inetsrv\\config\\schema\\aspnetcore_schema.xml", "c:\\Windows\\System32\\inetsrv\\config\\schema\\"] | |
# Configure IIS to use ASPNET core module | |
RUN Import-Module IISAdministration; ` | |
Start-IISCommitDelay; ` | |
(Get-IISServerManager).GetApplicationHostConfiguration().RootSectionGroup.Sections.Add('appSettings') ; ` | |
(Get-IISServerManager).GetApplicationHostConfiguration().GetSection('system.webServer/handlers').Overridemode = 'Allow' ; ` | |
(Get-IISServerManager).GetApplicationHostConfiguration().RootSectionGroup.SectionGroups['system.webServer'].Sections.Add('aspNetCore'); ` | |
(Get-IISServerManager).GetApplicationHostConfiguration().RootSectionGroup.SectionGroups['system.webServer'].Sections['aspNetCore'].OverrideModeDefault = 'Allow'; ` | |
New-IISConfigCollectionElement (Get-IISConfigSection 'system.webServer/globalModules' | Get-IISConfigCollection) -ConfigAttribute @{'name'='AspNetCoreModule';'image'='C:\windows\system32\inetsrv\aspnetcore.dll'}; ` | |
New-IISConfigCollectionElement (Get-IISConfigSection 'system.webServer/modules' | Get-IISConfigCollection) -ConfigAttribute @{'name'='AspNetCoreModule'}; ` | |
Stop-IISCommitDelay | |
EXPOSE 80 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why doesn't Microsoft provide an image with Asp.Net Core on IIS?
Updated it for Windows Server 1803 and Asp.Net Core 2.1.1
RegisterAspNetCoreHostIISModule.ps1:
Having an issue with the last section where it updates the application config. Docker throws "APPCMD failed with error code 183 Failed to update IIS configuration" on start up. Ideally, the Powershell needs to check if the "appSettings" config section already exists before adding it. So for now, I put the script in a separate file which can be called at-will.