ASP.NET Core 3.0 Docker容器端口映射失败

时间:2019-07-03 09:41:20

标签: docker asp.net-core

我正在尝试从Windows 10计算机上的Docker容器中运行全新的ASP.NET核心MVC Web应用程序。

我不是Docker专家,但是端口似乎没有绑定到主机上。我得到一个 来自容器内部的响应,但不是来自主机的响应。 2.2示例对我来说很好用。

dotnet --version
3.0.100-preview6-012264

复制步骤:

mkdir aspnetcore3_test
cd .\aspnetcore3_test\

mkdir aspnetapp
cd .\aspnetapp\

dotnet new mvc

cd ..
dotnet new sln

dotnet sln "aspnetcore3_test.sln" add "aspnetapp/aspnetapp.csproj"

创建具有以下内容的docker文件:

FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build
WORKDIR /app

# copy csproj and restore as distinct layers
COPY *.sln .
COPY aspnetapp/*.csproj ./aspnetapp/
RUN dotnet restore ./aspnetapp/*.csproj

# copy everything else and build app
COPY aspnetapp/ ./aspnetapp/
WORKDIR /app/aspnetapp
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0 AS runtime
WORKDIR /app
COPY --from=build /app/aspnetapp/out ./
EXPOSE 80
ENTRYPOINT ["dotnet", "aspnetapp.dll"]

然后执行docker位:

docker build -t dockertest .
docker run --rm -it --name=dockertest dockertest:latest -p 80:80

Docker run输出以下内容:

warn: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[60]
      Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed.warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
      No XML encryptor configured. Key {5b22d641-4632-4440-8f7d-8ad9b5e48098} may be persisted to storage in unencrypted form.
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://[::]:80
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /app

运行Firefox的容器表示无法连接,docker port dockertest空了

docker exec -i -t dockertest curl localhost吐出MVC应用HTML模板

1 个答案:

答案 0 :(得分:4)

通过在图像名称后添加-p 80:80 ,您实际上是将-p 80:80作为图像可执行文件的args传递,而不是作为docker的args传递。本身。解决方案是将其移动到图像名称的之前

docker run --rm -it --name=dockertest -p 80:80 dockertest:latest