Greg Bala
Greg Bala

Reputation: 3903

.net core web api app with https in docker

I have the simplest possible Web Api app in .net core ( with the default api/values api you get upon creation)

I've enabled HTTPS so in debug it works, and kestrel reports:

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

When I run the app in docker (using the MS provided dockerfile), kestrel reports that it only listens on port 80

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

How to configure the app to listen on https as well in docker?

Upvotes: 2

Views: 1718

Answers (1)

user10434643
user10434643

Reputation:

After making sure that you have EXPOSE 5001 in your app Dockerfile, use this command to start your app :

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}

UPDATE :

Just use a self-signed certificate for debugging, here's an example for 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(); 

Upvotes: 4

Related Questions