地图网站怎么做,网站分站原理,大气全屏通用企业网站整站源码,建设手机网站哪个平台比较好windows container 踩坑记Intro我们有一些服务是 dotnet framework 的#xff0c;不能直接跑在 docker linux container 下面#xff0c;最近一直在折腾把它部署在 windows container 下#xff0c;折腾的有点恶心#xff0c;记录一下。Windows Container 介绍Windows Cont… windows container 踩坑记Intro我们有一些服务是 dotnet framework 的不能直接跑在 docker linux container 下面最近一直在折腾把它部署在 windows container 下折腾的有点恶心记录一下。Windows Container 介绍Windows Container 是微软在 Windows 上的虚拟化实践它可以提供操作系统级别的虚拟化。通过我们说的容器化大多是指 Linux Container基于 linux 的 container 实践除此之外还有 windows container如果你使用的是 windows 且使用过 Docker for Desktop你也许会注意到 docker 右键的时候会有一个 “Switch to windows container” 的选项。Windows container 架构Windows container 分为两大部分windows container on windows(下文简称 Windows Container), linux container on windows(下文简称 Linux Container), 我们今天将要用到的是 Windows container.上图所示的两种方式对应着 Docker for Desktop 里 Windows Container 和 Linux Container 两种 docker 容器化运行时两种运行时不能同时使用可以切换切换过程中数据是不会丢失的你不可以在 windows container 环境下操作 linux container 的镜像与容器更不能在 linux container 环境 下操作 windows container 的镜像和容器两者架构上不一致。windows container 是相当于 docker 在 linux 下的原生实现linux container 是通过 Hyper-V 托管了一个小型虚拟机以实现 linux 环境。Windows 容器类型你应该知道, 有两个不同的容器类型 (也称为运行时)。Windows Server 容器通过进程和命名空间隔离技术提供应用程序隔离, 这就是这些容器也称为进程隔离的容器的原因。Windows Server 容器与容器主机和该主机上运行的所有容器共享内核。这些进程隔离的容器不提供敌意安全边界, 不应用于隔离不受信任的代码。由于共享内核空间这些容器要求具有相同的内核版本和配置。Hyper-v 隔离通过在高度优化的虚拟机中运行每个容器来扩展 Windows Server 容器提供的隔离。在此配置中, 容器主机不与同一主机上的其他容器共享其内核。这些容器旨在托管敌对多租户并且具有与虚拟机相同的安全保证。由于这些容器不与主机上的主机或其他容器共享内核, 因此它们可以运行具有不同版本和配置 (受支持版本内) 的内核。例如, Windows 10 上的所有 Windows 容器都使用 Hyper-v 隔离来利用 Windows Server 内核版本和配置尽管两者在技术架构上有差异但是 docker 的基本操作都是适用的如果你的磁盘不够大网速不够好不建议直接在自己电脑上尝试 windows containerwindows container 大部分是基于 windows-sever 的镜像动则十几个G下载镜像都不一定能下载成功。GetStarted部署一个简单的 dotnet framework 应用到 windows 容器docker pull microsoft/iis 拉一个 iis 的 docker 镜像docker run --rm -p 8080:80 --nameiis microsoft/iis:latest
docker run --it iis powershell看着上面的命令是不是感觉很熟悉啊下面再看一个 Dockerfile 这是一个 asp.net 的应用的 dockerfile来自微软的官方示例FROM mcr.microsoft.com/dotnet/framework/sdk:4.8 AS build
WORKDIR /app
# copy csproj and restore as distinct layers
COPY *.sln .
COPY aspnetapp/*.csproj ./aspnetapp/
COPY aspnetapp/*.config ./aspnetapp/
RUN nuget restore
# copy everything else and build app
COPY aspnetapp/. ./aspnetapp/
WORKDIR /app/aspnetapp
RUN msbuild /p:ConfigurationRelease
FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8 AS runtime
WORKDIR /inetpub/wwwroot
COPY --frombuild /app/aspnetapp/. ./踩的坑项目是 AspNetCore 只是项目是 dotnet framework471 的并且有引用几个 netstandard2.0 的项目1. 在 msbuild 的时候出错尚未找到解决方法提了一个 issue 还没回复https://github.com/microsoft/dotnet-framework-docker/issues/315看到有人说要引用 “Microsoft.Net.Compilers” 这个包在项目里加上了这个包的引用还是有问题这个问题很神奇本地 dotnet build/ msbuild 都是正常的msbuild 有问题之后便想用 dotnet build 来编译最初尝试安装 dotnet core 后来发现这个 framework 镜像里已经安装 dotnet core sdk太好了不用再自己装了后来用 dotnet build 代替了 msbuild2. host 应用dotnet 不能执行 dotnet framework 生成的 exe原本想着像在 linux 下面跑 dotnet core 一样直接 dotnetxxx.dll使用 kestrel 来托管然而并不能直接运行起初按错误提示以为要手动加一个 runtimeconfig.json 来指定框架类型及版本后来才知道 runtimeconfig.json 只支持 dotnetcore详见https://github.com/dotnet/core-setup/issues/7149后来还是放到 iis 下面当时使用的是 microsoft/iis 这个镜像发现放上去之后不能运行报 500 错误后来又装 dotnetcore-windows-hosting 还是不行最终在 Event-Log 中发现没有framework471 ...后来使用 mcr.microsoft.com/dotnet/framework/aspnet:4.7.1 这个镜像然后在安装 dotnetcore-windows-hosting 的时候又是一波多折。。最后先安装了 chocolatey , chocolatey 是 windows 上的一个包管理器像管理nuget包一样管理你PC上的软件然后使用 choco install dotnetcore-windowshosting-y 来安装 dotnetcore-windowshosting 模块。Solution最终使用的 Dockerfile 如下FROM mcr.microsoft.com/dotnet/framework/sdk:4.7.1 AS build
WORKDIR /app
# copy csproj and restore as distinct layers
COPY Projects/*.csproj ./Projects/
COPY nuget.config ./
RUN dotnet restore ./Projects/Projects.csproj
# copy everything else and build app
COPY . .
WORKDIR /app/Projects
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/framework/aspnet:4.7.1
WORKDIR /inetpub/wwwroot
# install choco
RUN Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString(https://chocolatey.org/install.ps1))
# install dotnetcore-windowshosting
RUN choco install dotnetcore-windowshosting -y
RUN powershell -NoProfile -Command Remove-Item -Recurse C:\inetpub\wwwroot\*
COPY --frombuild /app/Projects/out/ ./Memo折腾起来真是麻烦可以直接上 dotnetcore 还是直接上 dotnetcore 吧Tipswindows container 里 RUN 是相当于在 powershell 中执行命令Referencehttps://docs.microsoft.com/zh-cn/virtualization/windowscontainers/quick-start/quick-start-windows-10https://chocolatey.org/installhttps://github.com/microsoft/dotnet-framework-dockerhttps://github.com/microsoft/dotnet-framework-docker/tree/master/sampleshttps://docs.microsoft.com/en-us/virtualization/windowscontainers/