我是 Docker 新手,并尝试在 Windows 容器内运行旧版 .NET 应用程序(使用 docker 桌面)。这是我的 dockerfile
FROM mcr.microsoft.com/windows/servercore:ltsc2019
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop';"]
RUN dism.exe /online /enable-feature /all /featurename:iis-webserver /NoRestart
#Configure IIS
RUN Import-Module WebAdministration;New-Item –Path IIS:\AppPools\CustomApppool
RUN C:\windows\system32\inetsrv\appcmd.exe set apppool /apppool.name:CustomApppool /processModel.identityType:SpecificUser /processModel.username:us\user1 /processModel.password:P@@Ss
RUN Install-WindowsFeature Web-Windows-Auth;
RUN Install-WindowsFeature Web-IP-Security;
RUN Install-WindowsFeature NET-WCF-HTTP-Activation45;
RUN Install-WindowsFeature Web-Dyn-Compression;
RUN Install-WindowsFeature Web-Scripting-Tools;
RUN Install-WindowsFeature Web-AppInit;
RUN Install-WindowsFeature Web-Http-Redirect;
RUN Install-WindowsFeature Web-WebSockets;
# Update permissions on website folder
RUN icacls 'c:\inetpub\wwwroot' /Grant 'IUSR:(OI)(CI)(RX)'
RUN icacls 'c:\inetpub\wwwroot' /Grant 'IIS AppPool\DefaultAppPool:(OI)(CI)(RX)'
RUN icacls 'c:\inetpub\wwwroot' /Grant 'IIS AppPool\CustomApppool:(OI)(CI)(RX)'
#Copy website files from App host folder to container wwwroot folder
COPY Admin/ "c:/inetpub/wwwroot/Admin"
COPY Sade/ "c:/inetpub/wwwroot/Sade"
COPY Rater/ "c:/inetpub/wwwroot/Rater"
#Copy Service Monitor file
COPY ServiceMonitor.exe C:/
RUN New-WebApplication -Name Admin -Site 'Default Web Site' -PhysicalPath C:\inetpub\wwwroot\Admin -ApplicationPool CustomApppool;
RUN New-WebApplication -Name Sade -Site 'Default Web Site' -PhysicalPath C:\inetpub\wwwroot\Sade -ApplicationPool CustomApppool;
RUN New-WebApplication -Name Rater -Site 'Default Web Site' -PhysicalPath C:\inetpub\wwwroot\Rater -ApplicationPool CustomApppool;
#Authentication Settings
# Enable Directory browsing
RUN C:\Windows\system32\inetsrv\appcmd.exe set config 'Default Web Site' /section:system.webServer/directoryBrowse /enabled:'True'
# Enable anonymous authentication
RUN Set-WebConfigurationProperty -Filter '/system.webServer/security/authentication/anonymousAuthentication' -Location 'Default Web Site' -Name enabled -Value True;
# Enable basic authentication
#RUN Set-WebConfigurationProperty -Filter '/system.webServer/security/authentication/basicAuthentication' -Location 'Default Web Site' -Name enabled -Value True;
# Enable Windows authentication
RUN Set-WebConfigurationProperty -Filter '/system.webServer/security/authentication/windowsAuthentication' -Location 'Default Web Site' -Name Enabled -Value False;
RUN Restart-Service W3SVC
EXPOSE 80
ENTRYPOINT ['C:\ServiceMonitor.exe','w3svc']
我可以通过运行来构建图像
docker build -t demo/site
我正在运行此命令来启动容器 docker run -p 8000:80 demo/site:latest
但我收到此错误:
At line:1 char:35
+ $ErrorActionPreference = 'Stop'; ['C:\ServiceMonitor.exe','w3svc']
+ ~
Missing type name after '['.
At line:1 char:58
+ $ErrorActionPreference = 'Stop'; ['C:\ServiceMonitor.exe','w3svc']
+ ~
Missing argument in parameter list.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordEx
ception
+ FullyQualifiedErrorId : MissingTypename
请问我错过了什么?我需要做什么才能启动容器并能够在本地主机上浏览我的网站?
将最后一行的单引号替换为双引号。 也可以尝试用 CMD 替换 ENTRYPOINT。
Windows 容器上的 Docker 文件对转义字符串的方式很挑剔。
您看到的问题显然是覆盖的 SHELL 和 ENTRYPOINT 的组合。
尝试使用 CMD 而不是 ENTRYPOINT:
CMD ["C:\ServiceMonitor.exe","w3svc"]
此外,在设置之后和入口点之前恢复您的 SHELL:
SHELL ["cmd.exe"]
CMD ["C:\ServiceMonitor.exe","w3svc"]
您可以在此处找到适用于 IIS 的 dockerfile 示例:
https://github.com/david-garcia-garcia/windowscontainers/blob/master/servercore2022iis/dockerfile
这个位于另一个图像之上,该图像实际上处理入口点/shell/cmd 配置:
https://github.com/david-garcia-garcia/windowscontainers/blob/master/servercore2022/dockerfile
我建议不要使用服务监视器,你最好使用无限循环的 powershell 入口点,并进行适当的运行状况检查(如果这是 K8S/AKS)。
# escape=`
FROM mcr.microsoft.com/windows/servercore:ltsc2022
SHELL ["powershell.exe"]
##### YOUR SETUP SCRIPT
CMD ["powershell.exe", "-File", "C:\\entrypoint\\entrypoint.ps1" ]