Docker容器未在dockerfile中的给定端口上运行

问题描述

我正在尝试在docker(Linux容器)上运行我的应用程序。 在我的Dockerfile中,我暴露了端口80

Dockerfile

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["project/project.csproj","project/"]
COPY ["project.Data/project.Data.csproj","project.Data/"]
RUN dotnet restore "project/project.csproj"
COPY . .
WORKDIR "/src/project"
RUN dotnet build "project.csproj" -c Release -o /app/build

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

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

但是该应用程序不是在同一端口上启动,即使我不想为我的应用程序启用ssl,我也收到KestrelServer错误以获取开发https证书,并且在日志中也显示该应用程序正在端口上启动5000

日志文件

2020-08-11 15:00:53.3929|1|DEBUG|Microsoft.Extensions.Hosting.Internal.Host|Hosting starting 
2020-08-11 15:00:53.4224|60|WARN|Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository|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. 
2020-08-11 15:00:53.4312||INFO|Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager|User profile is available. Using '/root/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest. 
2020-08-11 15:00:53.4650|53|DEBUG|Microsoft.AspNetCore.DataProtection.KeyManagement.DefaultKeyResolver|Repository contains no viable default key. Caller should generate a key with immediate activation. 
2020-08-11 15:00:53.4694|57|DEBUG|Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider|Policy resolution states that a new key should be added to the key ring. 
2020-08-11 15:00:53.4800|58|INFO|Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager|Creating key {7c98d268-e4df-4a6b-8ed3-d8689029c5e8} with creation date 2020-08-11 15:00:53Z,activation date 2020-08-11 15:00:53Z,and expiration date 2020-11-09 15:00:53Z. 
2020-08-11 15:00:53.4918|32|DEBUG|Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager|Descriptor deserializer type for key {7c98d268-e4df-4a6b-8ed3-d8689029c5e8} is 'Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel.AuthenticatedEncryptorDescriptorDeserializer,Microsoft.AspNetCore.DataProtection,Version=3.1.6.0,Culture=neutral,PublicKeyToken=adb9793829ddae60'. 
2020-08-11 15:00:53.5035|34|DEBUG|Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager|No key escrow sink found. Not writing key {7c98d268-e4df-4a6b-8ed3-d8689029c5e8} to escrow. 
2020-08-11 15:00:53.5070|35|WARN|Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager|No XML encryptor configured. Key {7c98d268-e4df-4a6b-8ed3-d8689029c5e8} may be persisted to storage in unencrypted form. 
2020-08-11 15:00:53.5293|39|INFO|Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository|Writing data to file '/root/.aspnet/DataProtection-Keys/key-7c98d268-e4df-4a6b-8ed3-d8689029c5e8.xml'. 
2020-08-11 15:00:53.5362|23|DEBUG|Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager|Key cache expiration token triggered by 'CreateNewKey' operation. 
2020-08-11 15:00:53.5433|37|DEBUG|Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository|Reading data from file '/root/.aspnet/DataProtection-Keys/key-7c98d268-e4df-4a6b-8ed3-d8689029c5e8.xml'. 
2020-08-11 15:00:53.5502|18|DEBUG|Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager|Found key {7c98d268-e4df-4a6b-8ed3-d8689029c5e8}. 
2020-08-11 15:00:53.5631|13|DEBUG|Microsoft.AspNetCore.DataProtection.KeyManagement.DefaultKeyResolver|Considering key {7c98d268-e4df-4a6b-8ed3-d8689029c5e8} with expiration date 2020-11-09 15:00:53Z as default key. 
2020-08-11 15:00:53.6033||DEBUG|Microsoft.AspNetCore.DataProtection.TypeForwardingActivator|Forwarded activator type request from Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel.AuthenticatedEncryptorDescriptorDeserializer,PublicKeyToken=adb9793829ddae60 to Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel.AuthenticatedEncryptorDescriptorDeserializer,PublicKeyToken=adb9793829ddae60 
2020-08-11 15:00:53.6122|11|DEBUG|Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ManagedAuthenticatedEncryptorFactory|Using managed symmetric algorithm 'System.Security.Cryptography.Aes'. 
2020-08-11 15:00:53.6163|10|DEBUG|Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ManagedAuthenticatedEncryptorFactory|Using managed keyed hash algorithm 'System.Security.Cryptography.HMACSHA256'. 
2020-08-11 15:00:53.6415|2|DEBUG|Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider|Using key {7c98d268-e4df-4a6b-8ed3-d8689029c5e8} as the default key. 
2020-08-11 15:00:53.6466||DEBUG|Microsoft.AspNetCore.DataProtection.Internal.DataProtectionHostedService|Key ring with default key {7c98d268-e4df-4a6b-8ed3-d8689029c5e8} was loaded during application startup. 
2020-08-11 15:00:54.2074|2|DEBUG|Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer|Failed to locate the development https certificate at '(null)'. 
2020-08-11 15:00:54.2318||WARN|Microsoft.AspNetCore.Server.Kestrel|Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'. 
2020-08-11 15:00:54.2486|1|DEBUG|Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer|Unable to locate an appropriate development https certificate. 
2020-08-11 15:00:54.2525||DEBUG|Microsoft.AspNetCore.Server.Kestrel|No listening endpoints were configured. Binding to http://localhost:5000 by default. 
2020-08-11 15:00:54.2569||INFO|Microsoft.Hosting.Lifetime|Now listening on: http://localhost:5000 
2020-08-11 15:00:54.2612||DEBUG|Microsoft.AspNetCore.Hosting.Diagnostics|Loaded hosting startup assembly SwiftX 
2020-08-11 15:00:54.2677||INFO|Microsoft.Hosting.Lifetime|Application started. Press Ctrl+C to shut down. 
2020-08-11 15:00:54.2715||INFO|Microsoft.Hosting.Lifetime|Hosting environment: Development 
2020-08-11 15:00:54.2764||INFO|Microsoft.Hosting.Lifetime|Content root path: /app 
2020-08-11 15:00:54.2816|2|DEBUG|Microsoft.Extensions.Hosting.Internal.Host|Hosting started 

docker-compose.yaml

version: "3"
services:
    app:
        hostname: app   
        container_name: app-container-dev
        image: server.azurecr.io/project-app:dev-local
        volumes:
            - ./data/app/Settings/appsettings.json:/app/Settings/appsettings.json
            - ./data/app/Settings/nlog.config:/app/Settings/nlog.config
            - ./data/app/Log:/app/Log
            - ./data/app/Files:/app/Files
            - ./data/app/Images:/app/Images
        ports:
            - 8001:80
        networks:
            - project-net-dev
networks:
    project-net-dev: {}

即使尝试将5000端口绑定到Docker的8001

ports:
    - 8001:5000

然后我也遇到以下错误

Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'

我的问题是我如何禁用开发https证书检查(禁用ssl)并可以在端口80上启动应用程序。

解决方法

您需要告诉ASP.Net核心以侦听您公开的端口。

因此docker文件的末尾应如下所示:

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
# Expose the port in the container
EXPOSE 80/tcp
# Expose the port in ASP.Net Core
ENV ASPNETCORE_URLS=http://*:80
ENTRYPOINT ["dotnet","project.dll"]

基本上,您的“公开”应该与ASPNETCORE_URLS相匹配。

在您的Docker中,尝试使用:

- 80:80

基本上,映射需要匹配容器所显示的内容。

稍后,您可以尝试将端口映射更改为其他端口。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...