找回密码
 会员注册
查看: 174|回复: 0

如何让传统ASP.NET网站在Docker中运行

[复制链接]

1389

主题

5

回帖

496万

积分

管理员

积分
4962988
发表于 2024-2-29 08:40:04 | 显示全部楼层 |阅读模式

本文主要描述如何让传统ASP.NET网站在Docker中运行,侧重Docker image 搭建。
使用条件:

  1. Docker for windows 用户切换到Windows 容器模式
  2. Windows Server 2016 用户 开启 Windows Container

关于Docker for windows,nanoserver,Windows Container一些概念区分

  1. Docker for windows 在 win10 和 Windows Server 2016 上都能安装,但使用Docker for windows 开启Windows Container本质上都是在Hyper-v上运行。所以效率肯定没有原生的高,同时win10家庭版用户也跑不了Windows Container。
  2. 在Windows Server 2016上开启的Windows Container 是“原生”的,Windows Container与主机共享内核(博主未验证),用于企业生产环境
  3. nanoserver是超简版Windows Server 2016,目前微软只允许它作为在容器中运行,由于是超简版,没有IIS,但是可以手工部署,好像也没有framework,自然也跑不了ASP.NET ,但是正是由于它的精简,特别适合作为ASP.NET Core的 windows 生产运行环境!详情 docs.microsoft.com

MSSQL Docker

1.1 Dockerfile方式

参考Github Microsoft/mssql-docker 以下内容microsoft/windowsservercore处与Github略有不同,仅供参考

  1. FROM microsoft/windowsservercore:ltsc2016
  2. LABEL maintainer "Perry Skountrianos"
  3. # Download Links:
  4. ENV sql_express_download_url "https://go.microsoft.com/fwlink/?linkid=829176"
  5. ENV sa_password="_" \
  6. attach_dbs="[]" \
  7. ACCEPT_EULA="_" \
  8. sa_password_path="C:\ProgramData\Docker\secrets\sa-password"
  9. SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
  10. # make install files accessible
  11. COPY start.ps1 /
  12. WORKDIR /
  13. RUN Invoke-WebRequest -Uri $env:sql_express_download_url -OutFile sqlexpress.exe ; \
  14. Start-Process -Wait -FilePath .\sqlexpress.exe -ArgumentList /qs, /x:setup ; \
  15. .\setup\setup.exe /q /ACTION=Install /INSTANCENAME=SQLEXPRESS /FEATURES=SQLEngine /UPDATEENABLED=0 /SQLSVCACCOUNT='NT AUTHORITY\System' /SQLSYSADMINACCOUNTS='BUILTIN\ADMINISTRATORS' /TCPENABLED=1 /NPENABLED=0 /IACCEPTSQLSERVERLICENSETERMS ; \
  16. Remove-Item -Recurse -Force sqlexpress.exe, setup
  17. RUN stop-service MSSQL`$SQLEXPRESS ; \
  18. set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql14.SQLEXPRESS\mssqlserver\supersocketnetlib\tcp\ipall' -name tcpdynamicports -value '' ; \
  19. set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql14.SQLEXPRESS\mssqlserver\supersocketnetlib\tcp\ipall' -name tcpport -value 1433 ; \
  20. set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql14.SQLEXPRESS\mssqlserver\' -name LoginMode -value 2 ;
  21. CMD .\start -sa_password $env:sa_password -ACCEPT_EULA $env:ACCEPT_EULA -attach_dbs "$env:attach_dbs" -Verbose
复制代码
start.ps1
  1. # The script sets the sa password and start the SQL Service
  2. # Also it attaches additional database from the disk
  3. # The format for attach_dbs
  4. param(
  5. [Parameter(Mandatory=$false)]
  6. [string]$sa_password,
  7. [Parameter(Mandatory=$false)]
  8. [string]$ACCEPT_EULA,
  9. [Parameter(Mandatory=$false)]
  10. [string]$attach_dbs
  11. )
  12. if($ACCEPT_EULA -ne "Y" -And $ACCEPT_EULA -ne "y")
  13. {
  14. Write-Verbose "ERROR: You must accept the End User License Agreement before this container can start."
  15. Write-Verbose "Set the environment variable ACCEPT_EULA to 'Y' if you accept the agreement."
  16. exit 1
  17. }
  18. # start the service
  19. Write-Verbose "Starting SQL Server"
  20. start-service MSSQL`$SQLEXPRESS
  21. if($sa_password -eq "_") {
  22. $secretPath = $env:sa_password_path
  23. if (Test-Path $secretPath) {
  24. $sa_password = Get-Content -Raw $secretPath
  25. }
  26. else {
  27. Write-Verbose "WARN: Using default SA password, secret file not found at: $secretPath"
  28. }
  29. }
  30. if($sa_password -ne "_")
  31. {
  32. Write-Verbose "Changing SA login credentials"
  33. $sqlcmd = "ALTER LOGIN sa with password=" +"'" + $sa_password + "'" + ";ALTER LOGIN sa ENABLE;"
  34. & sqlcmd -Q $sqlcmd
  35. }
  36. $attach_dbs_cleaned = $attach_dbs.TrimStart('\\').TrimEnd('\\')
  37. $dbs = $attach_dbs_cleaned | ConvertFrom-Json
  38. if ($null -ne $dbs -And $dbs.Length -gt 0)
  39. {
  40. Write-Verbose "Attaching $($dbs.Length) database(s)"
  41. Foreach($db in $dbs)
  42. {
  43. $files = @();
  44. Foreach($file in $db.dbFiles)
  45. {
  46. $files += "(FILENAME = N'$($file)')";
  47. }
  48. $files = $files -join ","
  49. $sqlcmd = "IF EXISTS (SELECT 1 FROM SYS.DATABASES WHERE NAME = '" + $($db.dbName) + "') BEGIN EXEC sp_detach_db [$($db.dbName)] END;CREATE DATABASE [$($db.dbName)] ON $($files) FOR ATTACH;"
  50. Write-Verbose "Invoke-Sqlcmd -Query $($sqlcmd)"
  51. & sqlcmd -Q $sqlcmd
  52. }
  53. }
  54. Write-Verbose "Started SQL Server."
  55. $lastCheck = (Get-Date).AddSeconds(-2)
  56. while ($true)
  57. {
  58. Get-EventLog -LogName Application -Source "MSSQL*" -After $lastCheck | Select-Object TimeGenerated, EntryType, Message
  59. $lastCheck = Get-Date
  60. Start-Sleep -Seconds 2
  61. }
复制代码
  1. docker build -t yourname/mssql:2017-CU1 .
复制代码

1.2 Docker pull方式

  1. docker pull microsoft/mssql-server-windows-express:2017-CU1
复制代码

2 生成Container

SQLServer 密码规则 请点击 Password Policy
有关SQL Server 2016 Express Edition in Windows Containers 请点击 SQL Server 2016 Express Edition in Windows Containers
注意:虽然SQL Server是Express版本的,但连接实例名中没有"\SQLEXPRESS"

  1. # 非必需
  2. docker tag microsoft/mssql-server-windows-express:2017-CU1 microsoft/mssql:latest
  3. # 运行实例
  4. docker run -d -p 1433:1433 --name mssql01 -e sa_password=Jsdfk237 -e ACCEPT_EULA=Y --restart=always microsoft/mssql
复制代码

MySQL Docker (MySQL Container on Windows)

尚未实践,仅供参考

  1. docker pull dnikolayev/sonarqube-mysql-windows
复制代码

ASP.NET Docker

1.1 Dockerfile方式

参考 Github Microsoft/aspnet-docker

  1. # escape=`
  2. FROM microsoft/dotnet-framework:4.7.2-runtime-windowsservercore-ltsc2016
  3. SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
  4. RUN Add-WindowsFeature Web-Server; `
  5. Add-WindowsFeature NET-Framework-45-ASPNET; `
  6. Add-WindowsFeature Web-Asp-Net45; `
  7. Remove-Item -Recurse C:\inetpub\wwwroot\*; `
  8. Invoke-WebRequest -Uri https://dotnetbinaries.blob.core.windows.net/servicemonitor/2.0.1.3/ServiceMonitor.exe -OutFile C:\ServiceMonitor.exe
  9. #download Roslyn nupkg and ngen the compiler binaries
  10. RUN Invoke-WebRequest https://api.nuget.org/packages/microsoft.net.compilers.2.8.2.nupkg -OutFile c:\microsoft.net.compilers.2.8.2.zip ; `
  11. Expand-Archive -Path c:\microsoft.net.compilers.2.8.2.zip -DestinationPath c:\RoslynCompilers ; `
  12. Remove-Item c:\microsoft.net.compilers.2.8.2.zip -Force ; `
  13. &C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ngen.exe update ; `
  14. &C:\Windows\Microsoft.NET\Framework\v4.0.30319\ngen.exe update ; `
  15. &C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ngen.exe install c:\RoslynCompilers\tools\csc.exe /ExeConfig:c:\RoslynCompilers\tools\csc.exe | `
  16. &C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ngen.exe install c:\RoslynCompilers\tools\vbc.exe /ExeConfig:c:\RoslynCompilers\tools\vbc.exe | `
  17. &C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ngen.exe install c:\RoslynCompilers\tools\VBCSCompiler.exe /ExeConfig:c:\RoslynCompilers\tools\VBCSCompiler.exe | `
  18. &C:\Windows\Microsoft.NET\Framework\v4.0.30319\ngen.exe install c:\RoslynCompilers\tools\csc.exe /ExeConfig:c:\RoslynCompilers\tools\csc.exe | `
  19. &C:\Windows\Microsoft.NET\Framework\v4.0.30319\ngen.exe install c:\RoslynCompilers\tools\vbc.exe /ExeConfig:c:\RoslynCompilers\tools\vbc.exe | `
  20. &C:\Windows\Microsoft.NET\Framework\v4.0.30319\ngen.exe install c:\RoslynCompilers\tools\VBCSCompiler.exe /ExeConfig:c:\RoslynCompilers\tools\VBCSCompiler.exe ;
  21. ENV ROSLYN_COMPILER_LOCATION c:\\RoslynCompilers\\tools
  22. EXPOSE 80
  23. ENTRYPOINT ["C:\\ServiceMonitor.exe", "w3svc"]
复制代码

注意:2018/06/08 访问 https://api.nuget.org/packages/microsoft.net.compilers.2.8.2.nupkg 地址时 CDN 会转换成
https://nuget.cdn.azure.cn/packages/microsoft.net.compilers.2.8.2.nupkg 但这个网址并不存在此文件

  1. docker build -t yourname/websitename:version .
复制代码

1.2 Docker pull方式

  1. docker pull microsoft/aspnet:4.7.2-windowsservercore-ltsc2016
复制代码

2 更换Tag(非必需) 个人习惯

  1. docker tag microsoft/aspnet:4.7.2-windowsservercore-ltsc2016 microsoft/aspnet:latest
复制代码
有两种方式运行网站
直接使用ASPNET镜像运行网站
  1. docker run `
  2. -d `
  3. --link mssql01 `
  4. --name siteserver01 `
  5. -v C:/Users/Administrator/Desktop/Web/SiteServer01:C:/inetpub/wwwroot `
  6. -p 80:80 `
  7. --restart=always `
  8. microsoft/aspnet
复制代码
将网站封装进容器

Dockerfile
其中"."代表网站根目录

  1. FROM microsoft/aspnet
  2. COPY . /inetpub/wwwroot
  3. EXPOSE 80
复制代码
  1. docker build -t yourname/website:version .
复制代码
  1. docker run --link mssql01 -d -p 80:80 --name website01 --restart=always yourname/website:version
复制代码
容器互联(可选 除link的另外一种方式)
  1. docker network create website01_network
  2. docker network connect website01_network website01
  3. docker network connect website01_network mssql01
复制代码
清理孤立的数据卷
  1. docker volume ls -qf dangling=true
复制代码

参考

将旧版整体式 .NET Framework 应用程序迁移到 Windows 容器

将 ASP.NET MVC 应用程序迁移到 Windows 容器

知识共享许可协议

本文采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。


来源:https://blog.csdn.net/hatmen2/article/details/80628864
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?会员注册

×
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2024-12-25 13:52 , Processed in 0.304729 second(s), 26 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表