本文主要描述如何让传统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略有不同,仅供参考

FROM microsoft/windowsservercore:ltsc2016

LABEL maintainer "Perry Skountrianos"

# Download Links:
ENV sql_express_download_url "https://go.microsoft.com/fwlink/?linkid=829176" ENV sa_password="_" \
attach_dbs="[]" \
ACCEPT_EULA="_" \
sa_password_path="C:\ProgramData\Docker\secrets\sa-password" SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"] # make install files accessible
COPY start.ps1 /
WORKDIR / RUN Invoke-WebRequest -Uri $env:sql_express_download_url -OutFile sqlexpress.exe ; \
Start-Process -Wait -FilePath .\sqlexpress.exe -ArgumentList /qs, /x:setup ; \
.\setup\setup.exe /q /ACTION=Install /INSTANCENAME=SQLEXPRESS /FEATURES=SQLEngine /UPDATEENABLED=0 /SQLSVCACCOUNT='NT AUTHORITY\System' /SQLSYSADMINACCOUNTS='BUILTIN\ADMINISTRATORS' /TCPENABLED=1 /NPENABLED=0 /IACCEPTSQLSERVERLICENSETERMS ; \
Remove-Item -Recurse -Force sqlexpress.exe, setup RUN stop-service MSSQL`$SQLEXPRESS ; \
set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql14.SQLEXPRESS\mssqlserver\supersocketnetlib\tcp\ipall' -name tcpdynamicports -value '' ; \
set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql14.SQLEXPRESS\mssqlserver\supersocketnetlib\tcp\ipall' -name tcpport -value 1433 ; \
set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql14.SQLEXPRESS\mssqlserver\' -name LoginMode -value 2 ; CMD .\start -sa_password $env:sa_password -ACCEPT_EULA $env:ACCEPT_EULA -attach_dbs \"$env:attach_dbs\" -Verbose

start.ps1

# The script sets the sa password and start the SQL Service
# Also it attaches additional database from the disk
# The format for attach_dbs param(
[Parameter(Mandatory=$false)]
[string]$sa_password, [Parameter(Mandatory=$false)]
[string]$ACCEPT_EULA, [Parameter(Mandatory=$false)]
[string]$attach_dbs
) if($ACCEPT_EULA -ne "Y" -And $ACCEPT_EULA -ne "y")
{
Write-Verbose "ERROR: You must accept the End User License Agreement before this container can start."
Write-Verbose "Set the environment variable ACCEPT_EULA to 'Y' if you accept the agreement." exit 1
} # start the service
Write-Verbose "Starting SQL Server"
start-service MSSQL`$SQLEXPRESS if($sa_password -eq "_") {
$secretPath = $env:sa_password_path
if (Test-Path $secretPath) {
$sa_password = Get-Content -Raw $secretPath
}
else {
Write-Verbose "WARN: Using default SA password, secret file not found at: $secretPath"
}
} if($sa_password -ne "_")
{
Write-Verbose "Changing SA login credentials"
$sqlcmd = "ALTER LOGIN sa with password=" +"'" + $sa_password + "'" + ";ALTER LOGIN sa ENABLE;"
& sqlcmd -Q $sqlcmd
} $attach_dbs_cleaned = $attach_dbs.TrimStart('\\').TrimEnd('\\') $dbs = $attach_dbs_cleaned | ConvertFrom-Json if ($null -ne $dbs -And $dbs.Length -gt 0)
{
Write-Verbose "Attaching $($dbs.Length) database(s)" Foreach($db in $dbs)
{
$files = @();
Foreach($file in $db.dbFiles)
{
$files += "(FILENAME = N'$($file)')";
} $files = $files -join ","
$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;" Write-Verbose "Invoke-Sqlcmd -Query $($sqlcmd)"
& sqlcmd -Q $sqlcmd
}
} Write-Verbose "Started SQL Server." $lastCheck = (Get-Date).AddSeconds(-2)
while ($true)
{
Get-EventLog -LogName Application -Source "MSSQL*" -After $lastCheck | Select-Object TimeGenerated, EntryType, Message
$lastCheck = Get-Date
Start-Sleep -Seconds 2
}
docker build -t yourname/mssql:2017-CU1 .

1.2 Docker pull方式

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"

# 非必需
docker tag microsoft/mssql-server-windows-express:2017-CU1 microsoft/mssql:latest
# 运行实例
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)

尚未实践,仅供参考

docker pull dnikolayev/sonarqube-mysql-windows

ASP.NET Docker

1.1 Dockerfile方式

参考 Github Microsoft/aspnet-docker

# escape=`

FROM microsoft/dotnet-framework:4.7.2-runtime-windowsservercore-ltsc2016

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

RUN Add-WindowsFeature Web-Server; `
Add-WindowsFeature NET-Framework-45-ASPNET; `
Add-WindowsFeature Web-Asp-Net45; `
Remove-Item -Recurse C:\inetpub\wwwroot\*; `
Invoke-WebRequest -Uri https://dotnetbinaries.blob.core.windows.net/servicemonitor/2.0.1.3/ServiceMonitor.exe -OutFile C:\ServiceMonitor.exe #download Roslyn nupkg and ngen the compiler binaries
RUN Invoke-WebRequest https://api.nuget.org/packages/microsoft.net.compilers.2.8.2.nupkg -OutFile c:\microsoft.net.compilers.2.8.2.zip ; `
Expand-Archive -Path c:\microsoft.net.compilers.2.8.2.zip -DestinationPath c:\RoslynCompilers ; `
Remove-Item c:\microsoft.net.compilers.2.8.2.zip -Force ; `
&C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ngen.exe update ; `
&C:\Windows\Microsoft.NET\Framework\v4.0.30319\ngen.exe update ; `
&C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ngen.exe install c:\RoslynCompilers\tools\csc.exe /ExeConfig:c:\RoslynCompilers\tools\csc.exe | `
&C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ngen.exe install c:\RoslynCompilers\tools\vbc.exe /ExeConfig:c:\RoslynCompilers\tools\vbc.exe | `
&C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ngen.exe install c:\RoslynCompilers\tools\VBCSCompiler.exe /ExeConfig:c:\RoslynCompilers\tools\VBCSCompiler.exe | `
&C:\Windows\Microsoft.NET\Framework\v4.0.30319\ngen.exe install c:\RoslynCompilers\tools\csc.exe /ExeConfig:c:\RoslynCompilers\tools\csc.exe | `
&C:\Windows\Microsoft.NET\Framework\v4.0.30319\ngen.exe install c:\RoslynCompilers\tools\vbc.exe /ExeConfig:c:\RoslynCompilers\tools\vbc.exe | `
&C:\Windows\Microsoft.NET\Framework\v4.0.30319\ngen.exe install c:\RoslynCompilers\tools\VBCSCompiler.exe /ExeConfig:c:\RoslynCompilers\tools\VBCSCompiler.exe ; ENV ROSLYN_COMPILER_LOCATION c:\\RoslynCompilers\\tools EXPOSE 80 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 但这个网址并不存在此文件

docker build -t yourname/websitename:version .

1.2 Docker pull方式

docker pull microsoft/aspnet:4.7.2-windowsservercore-ltsc2016

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

docker tag microsoft/aspnet:4.7.2-windowsservercore-ltsc2016 microsoft/aspnet:latest

有两种方式运行网站

直接使用ASPNET镜像运行网站

docker run `
-d `
--link mssql01 `
--name siteserver01 `
-v C:/Users/Administrator/Desktop/Web/SiteServer01:C:/inetpub/wwwroot `
-p 80:80 `
--restart=always `
microsoft/aspnet

将网站封装进容器

Dockerfile

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

FROM microsoft/aspnet
COPY . /inetpub/wwwroot
EXPOSE 80
docker build -t yourname/website:version .
docker run --link mssql01 -d -p 80:80 --name website01 --restart=always yourname/website:version

容器互联(可选 除link的另外一种方式)

docker network create website01_network
docker network connect website01_network website01
docker network connect website01_network mssql01

清理孤立的数据卷

docker volume ls -qf dangling=true

参考

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

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

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

如何让传统ASP.NET网站在Docker中运行的更多相关文章

  1. ASP.NET Core 网站在Docker中运行

    Docker作为新一代的虚拟化方式,未来肯定会得到广泛的应用,传统虚拟机的部署方式要保证开发环境.测试环境.UAT环境.生产环境的依赖一致性,需要大量的运维人力,使用Docker我们可以实现一次部署, ...

  2. 在docker中运行ASP.NET Core Web API应用程序

    本文是一篇指导快速演练的文章,将介绍在docker中运行一个ASP.NET Core Web API应用程序的基本步骤,在介绍的过程中,也会对docker的使用进行一些简单的描述.对于.NET Cor ...

  3. docker中运行ASP.NET Core Web API

    在docker中运行ASP.NET Core Web API应用程序 本文是一篇指导快速演练的文章,将介绍在docker中运行一个ASP.NET Core Web API应用程序的基本步骤,在介绍的过 ...

  4. .NET Core Web 应用部署到 Docker 中运行

    环境介绍 : 虚拟机:VirtualBox 5.1.6 系 统:Ubuntu 16.04.1 LTS 系统准备完成后可以使用 sudo apt-get udpate 和 sudo apt-get up ...

  5. 在Docker中运行纸壳CMS并配置使用MySql

    纸壳CMS是基于ASP.Net Core开发的可视化内容管理系统,可以跨平台部署,可以在容器中运行.接下来看看如何在docker中运行纸壳CMS. GitHub:https://github.com/ ...

  6. 在Docker中运行torch版的neural style

    相关的代码都在Github上,请参见我的Github,https://github.com/lijingpeng/deep-learning-notes 敬请多多关注哈~~~ 在Docker中运行to ...

  7. 在Docker中运行EOS(MAC版)

    在Docker中运行EOS(MAC版) 在Docker中也可以简单快速的构建EOS.IO.笔者在Mac平台下参考官方文档躺了一次河.记录如下: 安装依赖 Docker 版本 17.05或者更高 tes ...

  8. 在docker中运行jenkins实现代码自动发布到测试服务器

    在docker中运行jenkins 用的镜像是apline版:lts-alpine,并设置正确的时区. docker run --name jenkins_master -d \ -p 8081:80 ...

  9. 在docker中运行elasticsearch时go程序无法连接到节点

    错误信息: panic: no active connection found: no Elasticsearch node available 在docker中运行es时,默认启动sniffing  ...

随机推荐

  1. 从无到有构建vue实战项目(三)

    四.响应式布局的实现 elemnt-ui参考bootatrap提供了响应式布局,附上地址:https://element.eleme.cn/#/zh-CN/component/layout 以下是我的 ...

  2. SQL Server温故系列(4):SQL 查询之集合运算 & 聚合函数

    1.集合运算 1.1.并集运算 UNION 1.2.差集运算 EXCEPT 1.3.交集运算 INTERSECT 1.4.集合运算小结 2.聚合函数 2.1.求行数函数 COUNT 2.2.求和函数 ...

  3. 机器学习读书笔记(七)支持向量机之线性SVM

    一.SVM SVM的英文全称是Support Vector Machines,我们叫它支持向量机.支持向量机是我们用于分类的一种算法. 1 示例: 先用一个例子,来了解一下SVM 桌子上放了两种颜色的 ...

  4. 在windowx的Hyper-v 安装CentOS系统

    博客写的很少,一方面是因为我觉得目前很多博客都是相互抄袭,或者有很多部分都是重复的内容.而我自己再去写同样的内容的画,有点浪费时间. 所以,如果我要写,我希望是写一些与众不同,或者重复率比较低的内容, ...

  5. Codeforces 755B:PolandBall and Game(map+思维)

    http://codeforces.com/problemset/problem/755/B 题意:A可以喊出n个字符串,B可以喊出m个字符串,如果一个字符串之前被喊过,那么它再也不能喊了,A先喊,最 ...

  6. (转)VSCode调试go语言出现:exec: "gcc": executable file not found in %PATH%

    原文:https://www.cnblogs.com/zsy/p/5958170.html 1.问题描述 由于安装VS15 Preview 5,搞的系统由重新安装一次:在用vscdoe编译go语言时, ...

  7. js random获取随机数,获取任意范围内随机整数

     壹 ❀ 引 想着好久没做笔试题了,去GitHub找了面试相关的项目,结果被第一道题难住了.....说难其实也不难,而是我忘记了取范围随机整数怎么写了,不可否认如果当时是我在笔试,肯定也凉了,那么就由 ...

  8. 音频编辑器 OcenAudio v3.1.9.0 绿色便携版

    下载地址:点我 基本介绍 ocenaudio是一款跨平台的,易于使用的,快速的,功能强大的,好用的音频编辑软件.该软件支持Virtual Studio Technology插件,美观.统一的跨平台界面 ...

  9. Java学习笔记之---方法和数组

    Java学习笔记之---方法与数组 (一)方法 (1)什么是方法? 方法是解决一类问题的步骤的有序组合 方法包含于类或对象中 方法在程序中被创建,在其他地方被引用 (2)方法的优点 使程序变得更简短而 ...

  10. Spring Cloud Alibaba | Nacos集群部署

    目录 Spring Cloud Alibaba | Nacos集群部署 1. Nacos支持三种部署模式 2. 集群模式下部署Nacos 2.1 架构图 2.2 下载源码或者安装包 2.3 配置集群配 ...