gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 14:10:05 25 4
gpt4 key购买 nike

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

我不是 docker 专家,但端口似乎没有绑定(bind)到主机上。我得到一个
来自容器内的响应,而不是来自主机的响应。 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 运行输出以下内容:
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

最佳答案

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

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

关于docker - ASP.NET Core 3.0 Docker 容器端口映射失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56867219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com