无法在Docker中运行Asp.Net核心Web API支架应用程序?

时间:2018-12-13 16:48:01

标签: .net visual-studio docker asp.net-core .net-core

我使用Visual Studio 2017并创建了具有docker(Linux)支持的Web API应用程序。并且在运行时给出了以下消息。

PS C:\> docker run hack2
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
      No XML encryptor configured. Key {93297fb1-e111-46f6-91e1-1bb4878a3941} may be persisted to storage in unencrypted
 form.
Hosting environment: Production
Content root path: /app
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.

这是脚手架的Docker文件。

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["hack2/hack2.csproj", "hack2/"]
RUN dotnet restore "hack2/hack2.csproj"
COPY . .
WORKDIR "/src/hack2"
RUN dotnet build "hack2.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "hack2.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "hack2.dll"]

在本地Windows 10 PC上尝试时,它会显示

$ curl localhost/api/values
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0curl: (7) Failed to connect to localhost port 80: Connection refused

然后我将映像复制到Linux盒中,并尝试curl

curl localhost/api/values
<HTML><HEAD>
<TITLE>Request Error</TITLE>
</HEAD>
<BODY>
<FONT face="Helvetica">
<big><strong></strong></big><BR>
</FONT>
<blockquote>
<TABLE border=0 cellPadding=1 width="80%">
<TR><TD>
<FONT face="Helvetica">
<big>Request Error (invalid_request)</big>
<BR>
<BR>
</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica">
Your request could not be processed. Request could not be handled
</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica">
This could be caused by a misconfiguration, or possibly a malformed request.
</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica" SIZE=2>
<BR>
For assistance, contact your network support team.
</FONT>
</TD></TR>
</TABLE>
</blockquote>
</FONT>
</BODY></HTML>

1 个答案:

答案 0 :(得分:0)

您的包装盒上是否有80端口可用(默认Web服务器未使用?)

尝试docker run -p 80:80 hack2

检查Docker docs中的docker run --publish,以了解如何与主机共享端口。