Deploy HTTPS ASP.NET Core app with Let’s Encrypt and Docker

This guide covers securing an ASP.NET Core application with an SSL certificate generated by Let's Encrypt: Certbot installation, certificate generation, Kestrel HTTPS configuration and deployment in a Docker container.

QDQuentin DESTRADEAumerial3 min readDeployment
In this article

Deploying an HTTPS ASP.NET Core application with an SSL certificate can seem straightforward at first glance, especially in containerized environments. But between mounting certificate files in Docker, handling PEM and RSA formats for Kestrel, and configuring environment variables, every step requires attention to detail. This guide assumes a Linux server with Docker already installed and a working knowledge of basic commands for creating and managing images.

Step 1 - Install Certbot on Linux

Certbot is a command-line tool that simplifies requesting SSL/TLS certificates from Let's Encrypt.

  • Update the server and install Certbot.
# For Ubuntu/Debian
sudo apt update
sudo apt install certbot -y

Step 2 - Generate the SSL certificate with Let's Encrypt

To obtain an SSL certificate using the DNS-01 challenge, domain ownership must be proven by creating a specific DNS TXT record.

1. Generate an SSL certificate using the DNS-01 challenge:

sudo certbot certonly --manual --manual-auth-hook /etc/letsencrypt/acme-dns-auth.py --preferred-challenges dns --debug-challenges -d your-domain.com --key-type rsa

The certonly option retrieves the certificate only, without automatically configuring a web server, giving full control over its use in Docker deployments.

The --manual option validates the certificate through a manually handled DNS challenge, and --preferred-challenges dns specifies that the DNS-01 challenge is preferred as it is more flexible across many environments.

2. Follow the DNS challenge instructions:

Certbot prompts you to create a DNS TXT record to validate domain ownership:

Please deploy a DNS TXT record under the name
_acme-challenge.votre-domaine.com with the following value:

ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789

3. Add the DNS TXT record:

In the DNS management section of the domain provider, create a new TXT record:

Type : TXT
Nom  : _acme-challenge.dev
Valeur : ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789

4. Validate the DNS challenge:

Go back to the terminal and press Enter to continue. Let's Encrypt verifies the DNS record and, if correctly configured, issues the SSL certificate.

💡By default, certificates are generated in /etc/letsencrypt/live/.

Step 3 - Verify and convert the private key

Once the certificate is generated, it is important to verify that the PEM files are correctly formatted for Kestrel.

Verify the private key:

sudo openssl rsa -in /etc/letsencrypt/live/your-domain.com/privkey.pem -check

Convert the private key if necessary:

By default, keys generated by Let's Encrypt with Certbot are in RSA format. If a different format was used, conversion to the RSA format expected by Kestrel is done as follows:

sudo openssl rsa -in /etc/letsencrypt/live/your-domain.com/privkey.pem -out /etc/letsencrypt/live/your-domain.com/privkey-rsa.pem

Step 4 - Run the Docker container with the PEM files

The certificate and private key PEM files must be mounted into the container so that Kestrel can access them:

sudo docker run -it -p 443:81 -p 80:80 \
  -e ASPNETCORE_URLS="https://*:81;http://*:80" \
  -v /etc/letsencrypt:/cert \
  -e ASPNETCORE_Kestrel__Certificates__Default__Path=/cert/live/votre-domaine.com/cert.pem \
  -e ASPNETCORE_Kestrel__Certificates__Default__KeyPath=/cert/live/votre-domaine.com/privkey.pem \
  my-docker-repository/my-docker-image:latest

The -v /etc/letsencrypt:/cert option mounts the folder containing the PEM files into the container. The environment variables specify the file paths so that Kestrel can use them at startup.

Step 5 - Test the application

Navigate to https://your-domain.com in a browser. A correctly installed SSL certificate results in a secure connection with the padlock icon in the address bar.

The ASP.NET Core application is now deployed with HTTPS in Docker, with a valid Let's Encrypt certificate and a portable, reproducible configuration across any Linux environment.

Keep reading

3 min read

IBM i & .NET containers: a winning combo with NTi

It's time to rethink the future of enterprise IT, through the prism of containerization, a change that today is redefining certain standards in the development, deployment and maintenance of modern applications.

8 min read

Deploy a .NET API and an Angular App on Raspberry Pi with Docker

In this article, discover how to deploy a .NET API and an Angular application in Docker containers on Raspberry Pi 4 and 5, connected to an IBM i. Dockerfiles creation, NGINX configuration, CORS, multi-architecture build and step-by-step deployment on each machine.

11 min read

IBM i, .NET and AI: what if you built your own tools?

Summer is over, and with it comes the usual stock-taking. Ours is marked by the release of NTi V5, our .NET data provider for IBM i, and it lands at a turning point for the industry. For thirty years, IBM i software sold on a premise that seemed beyond question: building a tool cost far more than buying one. Between coding assistants and a 100% .NET provider that requires no installation at all, that premise is giving way, and what it takes with it reaches well beyond the choice of a tool.