【问题标题】:Asp.Net Core docker-compose https self signed certificate issueAsp.Net Core docker-compose https 自签名证书问题
【发布时间】:2019-12-17 23:17:33
【问题描述】:

我无法使用自签名证书在 https 上为 asp.net core 3 api 运行 docker-compose。我已按照 ms docs 上的说明进行操作,但在尝试了数小时后我已经放弃了: https://docs.microsoft.com/en-us/aspnet/core/security/docker-https?view=aspnetcore-2.2

dotnet dev-certs https -ep %USERPROFILE%\.aspnet\https\aspnetapp.pfx -p { password here }
dotnet dev-certs https –trust

我的 docker compose 在这里:

version: '3.7'

networks:
  localdev:
    name: localdev

services:
  main-api:
    container_name: main-api
    build: 
      context: .
      dockerfile: Dockerfile
    #restart: always
    ports:
      - "5000:5000"
      - "5001:5001"

    depends_on:
      - db-server
    networks:
      - localdev

    volumes:
      - $USERPROFILE/.aspnet/https:/https/

    environment:
        ASPNETCORE_Kestrel__Certificates__Default__Password: "Passw0rd!"
        ASPNETCORE_Kestrel__Certificates__Default__Path: "$USERPROFILE/.aspnet/https/aspnetapp.pfx"

  db-server:
    image: mariadb:latest
    container_name: db-server
    environment:
      - MYSQL_ROOT_PASSWORD=Password! 
    ports: 
      - "13306:3306" 
    networks: 
      - localdev

docker-compose 日志在这里:

main-api     | warn: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[60]
main-api     |       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.
main-api     | crit: Microsoft.AspNetCore.Server.Kestrel[0]
main-api     |       Unable to start Kestrel.
main-api     | Interop+Crypto+OpenSslCryptographicException: error:2006D080:BIO routines:BIO_new_file:no such file
main-api     |    at Interop.Crypto.CheckValidOpenSslHandle(SafeHandle handle)
main-api     |    at Internal.Cryptography.Pal.OpenSslX509CertificateReader.FromFile(String fileName, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags)
main-api     |    at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(String fileName, String password, X509KeyStorageFlags keyStorageFlags)
main-api     |    at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(String fileName, String password)
main-api     |    at Microsoft.AspNetCore.Server.Kestrel.KestrelConfigurationLoader.LoadCertificate(CertificateConfig certInfo, String endpointName)
main-api     |    at Microsoft.AspNetCore.Server.Kestrel.KestrelConfigurationLoader.LoadDefaultCert(ConfigurationReader configReader)
main-api     |    at Microsoft.AspNetCore.Server.Kestrel.KestrelConfigurationLoader.Load()
main-api     |    at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.ValidateOptions()
main-api     |    at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken)
main-api exited with code 0
$ ls -l %USERPROFILE%\.aspnet\https\aspnetapp.pfx
-rw-r--r-- 1 tig28 197609 2652 Dec 17 23:07 %USERPROFILE%.aspnethttpsaspnetapp.pfx

使用 Linux 语法:

$ ls -l $USERPROFILE/.aspnet/https/aspnetapp.pfx
-rw-r--r-- 1 tig28 197609 2652 Dec 17 11:15 'C:\Users\tig28/.aspnet/https/aspnetapp.pfx'

【问题讨论】:

  • 这和 docker-compose 有什么关系?
  • 看看你是否在你的 Dockerfile 中使用 docker 命令得到同样的错误
  • ASPNETCORE_Kestrel__Certificates__Default__Path 应该只包含有效的文件路径。请从中删除无效部分。
  • 如果我将路径更改为只有一个路径,我会得到相同的没有这样的文件:ASPNETCORE_Kestrel__Certificates__Default__Path: "%USERPROFILE%\\.aspnet\\https\\aspnetapp.pfx"
  • 根据您上面粘贴的输出,您正在尝试构建 Linux Docker 映像,其中只有 Windows 的环境变量(例如 %USERPROFILE%)通常是无效的。

标签: docker asp.net-core docker-compose


【解决方案1】:

对您的 docker-compose 文件进行微小更改,此处的路径将是容器 /root/.aspnet/https/ApiHost.pfx 的挂载路径。

environment:
  ASPNETCORE_HTTPS_PORT: 6001
  ASPNETCORE_ENVIRONMENT: Development
  ASPNETCORE_Kestrel__Certificates__Default__Path:/root/.aspnet/https/ApiHost.pfx
  ASPNETCORE_Kestrel__Certificates__Default__Password: <password>
volumes:
  - ${USERPROFILE}\.aspnet\https:/root/.aspnet/https

【讨论】:

    【解决方案2】:

    如果您的项目使用 .net 6,则映射到卷的本地路径如下所示

    参考:Hosting ASP.NET Core images with Docker Compose over HTTPS

    dotnet dev-certs https -ep %USERPROFILE%\.aspnet\https\aspnetapp.pfx -p { password here }
    dotnet dev-certs https --trust
    
    version: '3.4'
    
    services:
      webapp:
        image: mcr.microsoft.com/dotnet/core/samples:aspnetapp
        ports:
          - 80
          - 443
        environment:
          - ASPNETCORE_ENVIRONMENT=Development
          - ASPNETCORE_URLS=https://+:443;http://+:80
          - ASPNETCORE_Kestrel__Certificates__Default__Password=<password>
          - ASPNETCORE_Kestrel__Certificates__Default__Path=/https/aspnetapp.pfx
        volumes:
          - ~/.aspnet/https:/https:ro
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-10
      • 1970-01-01
      • 1970-01-01
      • 2019-10-30
      • 1970-01-01
      • 1970-01-01
      • 2014-06-22
      • 2017-02-19
      相关资源
      最近更新 更多