Docker on Windows: 为.NET 容器选择目标操作系统

本文探讨了Docker在Windows平台的应用,特别是如何利用Docker部署静态文件至IIS,以及.NET Framework和.NET Core的镜像选择。文章还介绍了如何为不同.NET框架选择合适的操作系统,并提供了创建和部署.NET应用的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Docker 运行在Linux上,可能你已经了解很多,但运行在windows上呢,本文来试一下部署一个静态文件在IIS上,目的不在于练习docker, 而在于让你了解windows的一些镜像

一、前提条件

需要在windows上安装docker,然后切换到windows 容器。

你基本初步了解docker

二、为.NET 容器选择目标操作系统

下图展示了取决于应用程序所用的.NET Framework 版本,用户可以选择的操作系统版本。

在迁移基于.NET Framework 的现有或遗留应用程序时,主要依赖项包括 Windows 和 IIS。 此时只能选择基于 Windows Server Core 和.NET Framework 的 Docker 镜像。 在将镜像名称加入 Dockerfile 文件的过程中,可以使用标签选择操作系统与版本,例如通过 下列标签可选择基于.NET Framework 的 Windows 容器镜像:

下面是dotnetcore镜像:

  • microsoft/dotnet——适用于 Linux 和 Nano Server 的 .NET Core 运行时 和 SDK 镜像。
  • microsoft/aspnetcore — 适用于 Linux 和 Nano Server 的ASP.NET Core 镜像。
  • microsoft/aspnetcore-build——适用于 Linux 和 Nano Server 的 ASP.NET Core 镜像,用于构建应用程序。
  • microsoft/iis——适用于 Nano Server 的IIS镜像。

下面是dotnet-framework镜像:

  • microsoft/dotnet-framework——适用于 Windows Server Core 的 .NET Framework 3.5 和 4.6.2 镜像。
  • microsoft/aspnet——适用于 Windows Server Core 的 .NET Framework 3.5 和 4.6.2 ASP.NET 镜像。

我们也在 Docker Hub 上发布了示例,以便大家可以更加容易地使用。

  • microsoft/dotnet-samples——.NET Core 示例。
  • microsoft/dotnet-framework-samples——.NET Framework 示例。

各种容器的名称和标签太多了,不太好记,总结一下:

  • 微软是爱linux的,如果没有显示说明是nanoserver,那容器默认是基于于linux的Docker主机。

  • .Net Core是发展方向,没有明确说明是.Net Framework的,那容器默认是.Net Core的。

  • 对于Web或Web API的.Net Core项目,建议使用microsoft/aspnetcore

三、决策表:什么情况下,在 Docker 中使用怎样 的.NET 框架

下表概括总结了应该使用.NET Framework 或.NET Core 的决策因素。请务必注意,Linux 容器必须使用 基于 Linux 的 Docker 主机(虚拟机或服务器),而 Windows 容器必须使用基于 Windows Server 的 Docker 主机(虚拟机或服务器)。

四、Demo

(一)使用Muti-stage build的方式创建一个WindowServerCore的镜像

参考这里

 

(二)创建一个Nano Server 的IIS镜像

1. 写一个Dockerfile

FROM microsoft/iis
RUN echo "Hello World - Dockerfile" > c:\inetpub\wwwroot\index.html

2. 运行命令,生成image

docker build -t wucong60/iis-dockerfile .

3. 部署容器

docker run -d -p 80:80 wucong60/iis-dockerfile

4. 在本机上访问,结果如下:

(三)创建并部署一个基于.net framework的mvc应用程序 

首先使用Visual studio创建一个传统的asp.net

 创建完成之后,结构如下:

,它是基于http.sys的web服务器的,接下来直接加一个dockerfile打包成一个镜像。


FROM microsoft/aspnet
workdir "c:\inetpub\wwwroot"
copy . .
expose 81

将dockerfile与编译后的文件放在一起,如下:

PS C:\Users\Administrator\Desktop\Demo> docker build -t wucong60/mvcdemo .
Sending build context to Docker daemon  27.26MB
Step 1/4 : FROM microsoft/aspnet:4.7.1
 ---> 3b5cbec4eca1
Step 2/4 : workdir "c:\inetpub\wwwroot"
 ---> Running in 93db8d119042
Removing intermediate container 93db8d119042
 ---> 97b18b167959
Step 3/4 : copy . .
 ---> b4b35d836fcb
Step 4/4 : expose 80
 ---> Running in ca4e577938f5
Removing intermediate container ca4e577938f5
 ---> dd0cf25f483b
Successfully built dd0cf25f483b
Successfully tagged wucong60/mvcdemo:latest


PS C:\Users\Administrator\Desktop\Demo> docker run -itd -p 85:80 wucong60/mvcdemo --name myinstance
128e79eba4e12c695e950d431dc6442d9f25003b020ac7e040c6a2e57f80c5f6


PS C:\Users\Administrator\Desktop\Demo>

可以看到这里使用的基础镜像是microsoft/aspnet ,显示的效果如下:

 

参考文档

https://docs.microsoft.com/zh-cn/dotnet/standard/microservices-architecture/net-core-net-framework-containers/net-container-os-targets 

查看所有支持Windows的镜像: https://hub.docker.com/r/microsoft/dotnet/ 

Docker on Windows by Elton Stoneman English | 14 July 2017 | ISBN: 1785281658 | 358 Pages | AZW3/EPUB/MOBI/PDF (conv) | 23.77 MB Key Features Start building applications as Docker images and running containers in a Windows production environment Design and implement distributed applications that run across multiple networked containers Build a full Continuous Deployment pipeline for a .NET Core application running on Windows Book Description Docker has become hugely popular because it offers benefits at all stages of software design and delivery. Until recently, it was impossible to use a Windows Server platform to host the Docker Engine without adding an additional layer of virtualization. Starting with Windows Server 2016, you are able to use Docker on Windows and take advantage to easily build, deploy, and run Windows Containers. From building and running simple images to designing and implementing distributed applications, this book will teach everything you need to know while using Docker on Windows. You will start by installing Docker on different versions of Windows and Windows Server, and run a simple container to verify that the setup is working correctly. You will see how to build Docker images with a Dockerfile for a simple website, and then run it on Windows. Moving on, we'll cover the practical usage of Docker and the tools in the Docker ecosystem, such as Docker Compose, Docker Trusted Registry, and Docker Swarm Mode. Then, you'll be able to apply that knowledge by building a full continuous deployment pipeline for a .NET Core application running on Windows. We'll also dive deep into some of the core features of Docker, such as networking and security. At the end of the book, you'll be guided through a business case that will help you practically implement Docker in a real-world scenario. What you will learn Understand how Docker works, build images, and run containers Run Docker on different versions of Windows, and on the cloud with Azure Des
Docker 是 PaaS 提供商 dotCloud 开源的一个基于 LXC 的高级容器引擎,源代码托管在 Github 上, 基于go语言并遵从Apache2.0协议开源。 Docker自2013年以来非常火热,无论是从 github 上的代码活跃度,还是Redhat在RHEL6.5中集成对Docker的支持, 就连 Google 的 Compute Engine 也支持 docker 在其之上运行。 一款开源软件能否在商业上成功,很大程度上依赖三件事 - 成功的 user case(用例), 活跃的社区和一个好故事。 dotCloud 自家的 PaaS 产品建立在docker之上,长期维护且有大量的用户,社区也十分活跃,接下来我们看看docker的故事。 环境管理复杂 - 从各种OS到各种中间件到各种app, 一款产品能够成功作为开发者需要关心的东西太多,且难于管理,这个问题几乎在所有现代IT相关行业都需要面对。 云计算时代的到来 - AWS的成功, 引导开发者将应用转移到 cloud 上, 解决了硬件管理的问题,然而中间件相关的问题依然存在 (所以openstack HEAT和 AWS cloudformation 都着力解决这个问题)。开发者思路变化提供了可能性。 虚拟化手段的变化 - cloud 时代采用标配硬件来降低成本,采用虚拟化手段来满足用户按需使用的需求以及保证可用性和隔离性。然而无论是KVM还是Xen在 docker 看来,都在浪费资源,因为用户需要的是高效运行环境而非OS, GuestOS既浪费资源又难于管理, 更加轻量级的LXC更加灵活和快速 LXC的移动性 - LXC在 linux 2.6 的 kernel 里就已经存在了,但是其设计之初并非为云计算考虑的,缺少标准化的描述手段和容器的可迁移性,决定其构建出的环境难于迁移和标准化管理(相对于KVM之类image和snapshot的概念)。docker 就在这个问题上做出实质性的革新。这是docker最独特的地方。 VM技术和容器技术对比 VM技术和容器技术对比 面对上述几个问题,docker设想是交付运行环境如同海运,OS如同一个货轮,每一个在OS基础上的软件都如同一个集装箱,用户可以通过标准化手段自由组装运行环境,同时集装箱的内容可以由用户自定义,也可以由专业人员制造。这样,交付一个软件,就是一系列标准化组件的集合的交付,如同乐高积木,用户只需要选择合适的积木组合,并且在最顶端署上自己的名字(最后个标准化组件是用户的app)。这也就是基于docker的PaaS产品的原型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值