【问题标题】:Docker container is not running on given port in dockerfileDocker 容器未在 dockerfile 中的给定端口上运行
【发布时间】:2020-12-01 08:17:49
【问题描述】:

我正在尝试在 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"]

但是应用程序没有在同一个端口上启动,而且我在开发 https 证书时遇到 KestrelServer 错误,即使我不想为我的应用程序启用 ssl,并且在日志中显示应用程序正在端口上启动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, Microsoft.AspNetCore.DataProtection, Version=3.1.6.0, Culture=neutral, PublicKeyToken=adb9793829ddae60 to Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel.AuthenticatedEncryptorDescriptorDeserializer, Microsoft.AspNetCore.DataProtection, Culture=neutral, 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 上启动应用程序。

【问题讨论】:

标签: c# docker asp.net-core .net-core


【解决方案1】:

你需要告诉 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"]

基本上你的 Expose 应该匹配你的 ASPNETCORE_URLS。

在您的 docker compose 中尝试使用:

- 80:80

基本上,映射需要与容器公开的内容相匹配。

稍后您可以尝试将端口映射更改为不同的端口。

【讨论】:

  • @NeoAnderson 不是真的......因为另一个没有使用 docker compose
  • 在他使用- 8001:80的撰写文件中。我看那里没有问题——只要容器会监听 80 端口,主机上没有使用的任何端口都可以用来发布
猜你喜欢
  • 1970-01-01
  • 2021-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多