【问题标题】:.net core web api app with https in docker.net core web api 应用程序与 docker 中的 https
【发布时间】:2023-04-07 21:45:02
【问题描述】:

我在 .net 核心中拥有最简单的 Web Api 应用程序(使用您在创建时获得的默认 api/values api)

我已启用 HTTPS,因此在调试时它可以工作,并且 kestrel 报告:

Hosting environment: Development
Now listening on: https://localhost:5001
Now listening on: http://localhost:5000

当我在 docker 中运行应用程序时(使用 MS 提供的 dockerfile),kestrel 报告它只侦听端口 80

Hosting environment: Production
Now listening on: http://[::]:80

如何在 docker 中配置应用以监听 https?

【问题讨论】:

  • 在你的dockerfile中,你需要暴露5001端口

标签: docker asp.net-core asp.net-core-webapi asp.net-core-2.1


【解决方案1】:

确保您的应用程序 Dockerfile 中有 EXPOSE 5001 后, 使用此命令启动您的应用程序:

sudo docker run -it -p 5000:5000 -p 5001:5001
-e ASPNETCORE_URLS="https://+443;http://+80"
-e ASPNETCORE_HTTPS_PORT=5001
-e ASPNETCORE_Kestrel__Certificates__Default__Password="{YOUR_CERTS_PASSWORD}"
-e ASPNETCORE_Kestrel__Certificates__Default__Path=/https/{YOUR_CERT}.pfx
-v ${HOME}/.aspnet/https:/https/
--restart=always
-d {YOUR_DOCKER_ID}/{YOUR_IMAGE_NAME}

更新:

只需使用自签名证书进行调试,以下是 Kestrel 的示例:

WebHost.CreateDefaultBuilder(args)
    .UseKestrel(options =>
    {
        options.Listen(IPAddress.Loopback, 5000);  // http:localhost:5000
        options.Listen(IPAddress.Any, 80);         // http:*:80
        options.Listen(IPAddress.Loopback, 443, listenOptions =>
        {
            listenOptions.UseHttps("certificate.pfx", "password");
        });
    })
    .UseStartup<Startup>()
    .Build(); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 2020-04-27
    • 2016-10-04
    • 2021-10-18
    • 1970-01-01
    相关资源
    最近更新 更多