在 Docker 中启动准系统 MVC C# ASP.NET 项目得到 "Compilation Error"

Starting a barebones MVC C# ASP.NET project in Docker gives "Compilation Error"

首先,如果 Overflow 不是适合此目的的 Stack 站点,请告诉我!另外,如果您需要查看任何其他文件,请随时询问。

我启动了一个示例 Visual Studio C#/ASP.NET MVC 应用程序来尝试将 Docker 作为概念证明来查看如何配置这两个部分以协同工作。它在本地工作,但通过 Docker 部署时会抛出错误。

Server Error in '/' Application.
Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS0016: Could not write to output file 'c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\e22c2559c7e946\App_Web_index.cshtml.a8d08dba.w8h4d0pg.dll' -- 'The directory name is invalid. '

Source Error:

[No relevant source lines]

我的工作流程是发布到文件系统,然后通过下面的 docker 文件构建 docker 图像。我使用 "precompile" 源的选项(让我对错误感到困惑)。以下是我认为相关的文件(添加主题标签后的评论 post,而不是实际文件)。

docker文件

FROM microsoft/aspnet
COPY ./ /inetpub/wwwroot #this copies from the published folder, which contains 'bin'

web.config

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <system.web>
    <customErrors mode="Off"/>
  </system.web>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0"/>
    <add key="webpages:Enabled" value="false"/>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
    <httpRuntime targetFramework="4.0"/>
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed"/>
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-5.2.2.0" newVersion="5.2.2.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

我使用以下命令来 build/run 环境:

docker build -t testapp .
docker run -d --name test -p 20012:20012 testapp

-p 似乎不想工作,我尝试转到 docker 的 IP 上的那个端口,但它总是被拒绝,所以我直接转到 docker 的 IP 从检查中获得。

我尝试过不同程度失败的事情:

如有任何帮助,我们将不胜感激!

更新 1:

在使用答案解决问题后,它成功构建,并且正如我的最终目标一样打印服务器的 IP(docker 容器)以显示 load-balancing/Swarming 的工作原理。对于任何对预构建 docker 图像(在 windows 容器中)感兴趣的人,它会打印您点击的 docker 图像的 IP,作为加载 [=57= 的概念证明] 和 docker,图片在 hub.docker.com 上,图片名称 acmiles/aspnet_printserverip。请随意查看,但这只是一个示例项目,添加了在索引页上打印 Docker 容器 IP 的逻辑。下面是我用来构建图像的 docker 文件的最终版本。

FROM microsoft/aspnet
RUN New-Item c:\testapp -type directory
WORKDIR /testapp
COPY ./outputRelease/ .
RUN Remove-WebSite -Name 'Default Web Site' 
RUN New-Website -Name 'webapp' -Port 80 -PhysicalPath 'c:\testapp' -ApplicationPool '.NET v4.5'

我在尝试让现有的 MVC 应用程序在 docker 中工作时遇到了同样的问题。我走遍了各个房子试图解决这个问题,并在周末花了很多时间!我尝试将各种临时文件夹的权限设置为各种帐户,并尝试使用本地配置文件将应用程序池设置为 运行。

我终于通过将应用程序池帐户添加到本地管理员组来让它工作。这不是最终解决方案,但它确实有效,并为我指明了正确的方向,我需要为其分配权限。

这是我的 Dockerfile;

FROM microsoft/aspnet
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
ARG source
WORKDIR /inetpub/wwwroot
COPY ${source:-obj/Docker/publish} .

RUN Add-LocalGroupMember -Group 'Administrators' -Member 'IIS AppPool\DefaultAppPool'; \
IISRESET; 

希望对您有所帮助。

谢谢

贾里德

另一种解决方法是在不同的网站上工作。在我的 docker 文件中,我删除了默认网站并添加了一个新网站。

RUN New-Item c:\webapp -type directory
RUN Remove-WebSite -Name 'Default Web Site' 
RUN New-Website -Name 'webapp' -Port 80 -PhysicalPath 'c:\webapp' -ApplicationPool '.NET v4.5'