在这个时间点,咱们可能已经对 Linux 容器使用已经达到熟练掌握的程度,由于 Docker 与 Kubernetes 都是最先为 Linux 平台设计。当咱们从容器这项技术中体会到种种收益,对于咱们的 windows 的应用是否也能利用容器技术简化咱们的开发运维?对于大型的企业来讲,Windows 系列的开发程序也会占必定的比例,这个时候领导可能会有一个指示下来:“咱们 .Net 应用也要上容器云”。html
好的,任务拿到之后咱们首先要解决的第一件事就是 Windows 应用容器化,虽然咱们知道 .Net Core 是一个能够跨平台运行,但仍然有不少使用 .Net Framwork 编写的应用仍在运行和迭代,因此 Docker on Windows 是一条必需要走的路,好在微软和 Docker 在这方面有足够的投入。git
小贴士: 对于企业来讲,转型并非把原来全部的资产所有抛弃,是利用能利用的原有资产和新的技术继续向前进
虽然咱们常说 Container 的实现方案不只只有 Docker, 但咱们在实际使用上用的最最最多仍是 Docker。这里心疼 Docker 三秒钟😭。在 Windows 容器化的实现上分为两类:github
这两种不一样的容器类型,从操做角度上是一致的,像Build、Push、Run 等等,不一样的是它是 Windows 环境,须要使用 powershell 或者 cmd 去写 Dockerfile, 固然这个对于 Windows 的运维人员没什么问题。web
看一个简单的例子:docker
FROM microsoft/windowsservercore:1803 COPY ConsoleTest.exe C:/ ENTRYPOINT C:/ConsoleTest.exe
咱们注意到这个 Dockerfile 的 base 镜像是 windowsservercore:1803 ,意味着这个镜像是能够和 windowsserver 1803 兼容的 Docker 镜像, 这里提到到了一个 Windows Host OS 与 容器 OS 的版本兼容性:shell
Container OS version | Host OS Version | |||||
---|---|---|---|---|---|---|
Windows Server 2016 Builds: 14393. | Windows 10 1609, 1703 Builds: 14393., 15063. | Windows Server version 1709 Builds 16299. | Windows 10 Fall Creators Update Builds 16299. | Windows Server version 1803 Builds 17134. | Windows 10 version 1803 Builds 17134. | |
Windows Server 2016 Builds: 14393. | Supports process orhyperv isolation |
Supports Onlyhyperv isolation |
Supports Onlyhyperv isolation |
Supports Onlyhyperv isolation |
Supports Onlyhyperv isolation |
Supports Onlyhyperv isolation |
Windows Server version 1709 Builds 16299. | Not supported | Not supported | Supports process orhyperv isolation |
Supports Onlyhyperv isolation |
Supports Onlyhyperv isolation |
Supports Onlyhyperv isolation |
Windows Server version 1803 Builds 17134. | Not supported | Not supported | Not supported | Not supported | Supports process orhyperv isolation |
Supports Onlyhyperv isolation |
翻译过是:windows
再看一个例子:安全
buildapp.ps1app
# Remove existing default web site files remove-item C:\inetpub\wwwroot\iisstart.* # Ensure write permissions over web app project files icacls C:\inetpub\wwwroot\WebTest /grant Everyone:F /t /q # Import necessary IIS modules then set app project folder as web application Import-Module IISAdministration Import-Module WebAdministration New-Item 'IIS:\Sites\Default Web Site\WebTest' -Type Application -PhysicalPath 'C:\inetpub\wwwroot\WebTest' Set-WebConfigurationProperty -p 'MACHINE/WEBROOT/APPHOST' -fi 'system.applicationHost/log' -n 'centralLogFileMode' -v 'CentralW3C'; ` Set-WebConfigurationProperty -p 'MACHINE/WEBROOT/APPHOST' -fi 'system.applicationHost/log/centralW3CLogFile' -n 'truncateSize' -v 4294967295; ` Set-WebConfigurationProperty -p 'MACHINE/WEBROOT/APPHOST' -fi 'system.applicationHost/log/centralW3CLogFile' -n 'period' -v 'MaxSize'; ` Set-WebConfigurationProperty -p 'MACHINE/WEBROOT/APPHOST' -fi 'system.applicationHost/log/centralW3CLogFile' -n 'directory' -v 'c:\iislog'runapp.ps1运维
Start-Service W3SVC; ` Invoke-WebRequest http://localhost -UseBasicParsing | Out-Null; ` netsh http flush logbuffer | Out-Null; ` Get-Content -path 'c:\iislog\W3SVC\u_extend1.log' -Tail 1 -WaitDockerfile
FROM microsoft/dotnet-framework:4.7.2-sdk-20180814-windowsservercore-1803 # WebTest.NET dependencies RUN dism.exe /online /enable-feature /all /featurename:iis-webserver /NoRestart RUN powershell add-windowsfeature web-asp-net45 # Configure Web App COPY runapp.ps1 buildapp.ps1 WebTest.zip C:/ SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"] RUN powershell -Command { Expand-Archive -Path C:\WebTest.zip -DestinationPath C:\inetpub\wwwroot\WebTest } RUN powershell -Command { Remove-Item C:\WebTest.zip -Force } RUN powershell.exe C:/buildapp.ps1 EXPOSE 80 ENTRYPOINT ["powershell", "C:/runapp.ps1"]
上面的例子作了一件事是把 iis 的文件日志输出经过 tail 的方式转换成了标准输出,这样 docker logs
就能看到日志输出了
下一篇: 快速搭建 Windows Kubernetes 环境
Ref:
- https://docs.docker.com/docker-for-mac/
- https://github.com/moby/hyperkit
- https://docs.microsoft.com/en-us/windows-server/virtualization/hyper-v/hyper-v-technology-overview
- https://docs.microsoft.com/en-us/virtualization/windowscontainers/about/#windows-container-types
- https://docs.microsoft.com/en-us/virtualization/windowscontainers/deploy-containers/version-compatibility