install-website.ps1
的内容可以参考使用PowerShell部署AspNetCore应用到IIS
Dockerfile (使用Muti-stage build的方式)
# 下面的 escape在window系统非常有用,它用以定义在Dockerfile中使用转义字符,Dockerfile中,escape默认为\,这里转义为`,而且必须放在第一行,参考https://blog.csdn.net/chengqiuming/article/details/79007598
# escape=`
# 这里本可以使用如下镜像,用Kerstrel作为WEB服务器,但本文不这样做
#FROM microsoft/dotnet:2.1-aspnetcore-runtime-nanoserver-sac2016 AS base
# 这里使用官方的Windows Server Core作为基础镜像
FROM microsoft/windowsservercore:latest AS base
# 这里是这个build镜像,专用于build我们的源代码
#FROM microsoft/dotnet:2.1-sdk-nanoserver-1803 AS build
FROM microsoft/dotnet:2.1-sdk-nanoserver-sac2016 AS build
WORKDIR /src
COPY ["./webapi.csproj", "."]
RUN dotnet restore "webapi.csproj"
COPY . .
RUN dotnet build "webapi.csproj" -c Release -o /app
# 这里发部程序
FROM build AS publish
RUN dotnet publish "webapi.csproj" -c Release -o /app
FROM base AS final
# 这里在镜像中启用powershell脚本环境
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
LABEL name="WSR" `
release-date="2019-1-4"
# Disable DNS cache so container addresses always fetched from Docker
#RUN Set-ItemProperty -path 'HKLM:\SYSTEM\CurrentControlSet\Services\Dnscache\Parameters' -Name ServerPriorityTimeLimit -Value 0 -Type DWord
# Activate Windows features based on the app's requirements.
#RUN Add-WindowsFeature Web-App-Dev, Web-Net-Ext45, Web-Asp-Net45, Web-ISAPI-Ext, Web-ISAPI-Filter, NET-Framework-45-ASPNET
# 将上面使用powershell脚本Copy到镜像中
RUN New-Item -ItemType Directory C:\install;
COPY .\install-website.ps1 C:\install
# DONT TOUCH - Paths used during CI build after solution MSBUILD.exe
# COPY .\bin\Release\PublishOutput C:\inetpub\wwwroot\
COPY --from=publish /app C:\inetpub\wwwroot\
RUN 'Log written in docker build' >> 'C:\Logs\docker-build.log';
#这里就是我们上面使用powershell命令安装dotnetcore程序到IIS的脚本
ENTRYPOINT ["powershell.exe", "C:\\install\\install-website.ps1"]
# 这里可以对你的程序作一些健康检查
# HEALTHCHECK --interval=60s --timeout=60s --start-period=100s --retries=3 CMD C:\install\healthcheck.bat