RichardN

Using Certificates with ASP.Net Core under Docker

This post serves as a quick reference guide to getting custom certificates working for a Docker based asp.net core application.

Obtaining the certificate

Depending on your use case this could be as simple as using something like LetsEncrypt for a free certificate, or using a paid for service like ssl.com to buy a custom certificate.

Converting your certificate to *.pfx

For my project I went with the paid path and bought one for my application which resulted in me receiving a .crt file along with a key file which was incompatible with asp.net as it requires a .pfx file. After some quick Googling I was able to convert my certificate to the required pfx format by running the following command on one of my Ubuntu servers.

openssl pkcs12 -export -out mycert.ca.pfx -inkey mycert.ca_key.txt -in mycert.ca.crt

Hopefully this saves you some time in the future.

Configuring your Docker container

With some trial and error I managed to come up with the following Docker configuration that works with my application:

Port Bindings

You will need to expose and map any of the container ports you wish to access on the host - generally with asp.net applications you will want to map ports 80 (for HTTP) and 443 (for HTTPS), I ended up with the following configuration:

Container Paths

You will also need to map some additional paths for your container:

Container Variables

The last piece of the puzzle is defining some configuration environment variables for your container to tell asp.net core where to look for the certificate file, what ports to use for HTTP and so on:

Sample Docker Run Command

Below is a sample Docker run command for a fictitious dockeruser/container container with the above configuration:

docker run
  -d
  --name='Container'
  --net='bridge'
  -e TZ="America/Edmonton"
  -e 'ASPNETCORE_URLS'='https://+;http://+'
  -e 'ASPNETCORE_HTTPS_PORT'='5005'
  -e 'ASPNETCORE_Kestrel__Certificates__Default__Password'='xxx'
  -e 'ASPNETCORE_Kestrel__Certificates__Default__Path'='/https/aspnetapp.pfx'
  -p '5003:80/tcp'
  -p '5005:443/tcp'
  -v '/…/mysite.ca.pfx':'/https/aspnetapp.pfx':'rw'
  -v '/…/.aspnet/':'/root/.aspnet':'rw'
'dockeruser/container’

After deployment you should be able to browse to your application on HTTPS and get served a valid certificate.