【问题标题】:HTTPS in docker in Service Fabric for asp.net core not working用于 asp.net 核心的 Service Fabric 中 docker 中的 HTTPS 不起作用
【发布时间】:2021-01-29 00:23:39
【问题描述】:

我有一个托管在 docker 中的 asp.net 核心应用程序。 docker 文件是这样的

FROM  mcr.microsoft.com/dotnet/core/aspnet:3.1
LABEL cmbappname="autocomplete"
ARG source
WORKDIR /cmbapp
ADD ${source} . 
ENV APP_UTILS=C:\\app 
VOLUME ${APP_UTILS}
HEALTHCHECK --retries=5  --interval=100s --start-period=10s   CMD curl --fail http://localhost || exit 1 
ENTRYPOINT ["dotnet", "MyBus.AutoApi.dll"]
EXPOSE 80
EXPOSE 443

托管在服务结构中的 docker 映像具有这样的服务清单

<?xml version="1.0" encoding="utf-8"?>
<ServiceManifest Name="AutoApiPkg"
                 Version="1.0.0"
                 xmlns="http://schemas.microsoft.com/2011/01/fabric"
                 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <ServiceTypes>
    <!-- This is the name of your ServiceType.
         The UseImplicitHost attribute indicates this is a guest service. -->
    <StatelessServiceType ServiceTypeName="AutoApiType" UseImplicitHost="true" />
  </ServiceTypes>

  <!-- Code package is your service executable. -->
  <CodePackage Name="Code" Version="1.0.0">
    <EntryPoint>
      <!-- Follow this link for more information about deploying Windows containers to Service Fabric: https://aka.ms/sfguestcontainers -->
      <ContainerHost>
        <ImageName>autoApi</ImageName>
      </ContainerHost>
    </EntryPoint>
    <!-- Pass environment variables to your container: -->

    <EnvironmentVariables>
      <EnvironmentVariable Name="ASPNETCORE_ENVIRONMENT" Value="Debug" />

      <EnvironmentVariable Name="ASPNETCORE_URLS" Value="https://*:443/;http://*:80/;https://*:54100/;http://*:54200/"/>
    </EnvironmentVariables>


  </CodePackage>

应用程序清单中的容器策略

 <Policies>
      <ContainerHostPolicies CodePackageRef="Code"  AutoRemove="false"  UseDefaultRepositoryCredentials="false"   ContainersRetentionCount="2"  RunInteractive="true">
        <!-- See https://aka.ms/I7z0p9 for how to encrypt your repository password -->
        <PortBinding ContainerPort="443" EndpointRef="AutApiTypeEndpoint" />
        <PortBinding ContainerPort="80" EndpointRef="LocalAutApiTypeEndpoint" />
        <RepositoryCredentials  AccountName="[AzureContainerUserName]" Password="[AzureContainerPassword]" PasswordEncrypted="false"/>
        <HealthConfig   IncludeDockerHealthStatusInSystemHealthReport="true" RestartContainerOnUnhealthyDockerHealthStatus="false" />            
      </ContainerHostPolicies>
      
    </Policies>

应用程序在没有环境变量“ASPNETCORE_URLS”的情况下运行并正常运行

但是当添加 env 变量时,它不起作用,也无法访问。

调试容器会给出以下错误日志

无法启动 Kestrel。 System.InvalidOperationException:无法 配置 HTTPS 端点。没有指定服务器证书,并且 找不到默认开发者证书或已过期。

【问题讨论】:

    标签: docker asp.net-core https azure-service-fabric


    【解决方案1】:
    1. 获取证书,例如使用Letsencrypt [example],或使用自签名证书(用于测试)。
    2. 使用volume 将证书文件附加到您的容器中。
    3. 使用环境变量指示证书的存储位置:
    ASPNETCORE_Kestrel__Certificates__Default__Path=certificate.pfx
    
    1. 使用另一个环境变量提供密码以允许访问私钥:
    ASPNETCORE_Kestrel__Certificates__Default__Password="****"
    

    更多信息here

    【讨论】:

      猜你喜欢
      • 2018-09-21
      • 2018-06-30
      • 2019-07-01
      • 2017-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-14
      相关资源
      最近更新 更多