SSL Certificates are very necessary for the Trust, Identity and Encryption of an APP. ASP.NET Core apps use HTTPS Certificates by default. ASP.NET Core uses self-signed development certificates for development. So, when you are hosting your app to a Docker Container then you need to tell docker where to find this development certificate in your machine. Once docker knows the location of the HTTPS certificate then your app will start opening with https url eg https://localhost:8001.
The procedure will be same for the production scenario also. You can generate a free HTTPS certificate from Let’s Encrypt, then tell your Docker app (which is running in Azure or AWS) to find the HTTPS certificate from this directory.
First create a new ASP.NET Core App in Visual Studio and name DockerHttps, and make sure to check the option that says – Place solution and project in the same directory in Visual Studio. Next, select the template ASP.NET Core Web App this will create a basic ASP.NET Core Razor Pages based app.
After that add Dockerfile on the app.
Check the below 90 seconds video below which shows the app creation and Dockerfile creation.
Kindly note I am using Linux containers.
Our app is ready to be hosted on Docker with HTTPS but before that we need to understand 2 important topics which are:
Docker contains environment variables which are used to configure Images and Containers. The -e option in docker command is used to set environment variable. I will use Environment Variables to do a number of things, these are:
Suppose you want to access a file which contains some important password that are needed for the app which is running in a Docker Container. You want this file to remain accessible and not get deleted at all the time, even if the container is deleted. How to do it?
The answer is through Docker Volumes because volume lives outside of the container in file system of the host. So, if you are running your Docker Container in Azure then you can store this file in azure directory which is outside of the container, and the container can then access this file from there. See the below image which explains this.
We can simply use the docker volume concept to store the SSL certificate in a volume and then let our app, which is running in a docker container, to use it from there.
Use -v option in docker command to work with volumes.
The dotnet dev-certs tool is used to create self-signed development certificates.
First clean any previous SSL development certificate from your machine. So, run the following command in your Command Prompt.
dotnet dev-certs https --clean
Accept any prompt which you get. Then you will see a message – HTTPS development certificates successfully removed from the machine..
Next, let us run the following given command to generate a new SSL certificate.
dotnet dev-certs https -ep %USERPROFILE%\.aspnet\https\aspnetapp.pfx -p mypass123
You will receive a message – The HTTPS developer certificate was generated successfully.
The SSL called aspnetapp.pfx will be created with a password mypass123.
The path of the SSL certificate will be inside user profile folder since I have referred it from %USERPROFILE%\. The full path of the SSL in my case is:
Here Avita is my windows login name, change it to your’s login name and you will find it in your pc. In the below image I have shown the SSL certificate file which is just generated on my pc.
The final command to run is to trust the ASP.NET Core HTTPS development certificate. This command is given below.
dotnet dev-certs https --trust
If you get a prompt after running the above command then make sure you accept it.
First, we need to build the Docker Image so that it contains our ASP.NET Core app. So, in your command prompt, go to the directory of the Dockerfile and then run the following docker build command:
docker build -t dhttps:v1 .
Docker image with the name of dhttps and tag v1 will be created for our ASP.NET Core app.
Next command is to run a docker container with the image we just built. This command is given below:
docker run -p 7000:80 -p 7001:443 -e ASPNETCORE_URLS="https://+;http://+" -e ASPNETCORE_HTTPS_PORT=7001 -e ASPNETCORE_Kestrel__Certificates__Default__Password="mypass123" -e ASPNETCORE_Kestrel__Certificates__Default__Path=/https/aspnetapp.pfx -v %USERPROFILE%\.aspnet\https:/https/ dhttps:v1
Let us understand this above command:
1. With the “-p” option -p 7000:80 -p 7001:443 the ports 80 and 443 of the container are exposed to the ports 7000 and 7001 of the host.
2. I defined 3 environment variables to pass values to the app running inside the container. The environment variables are used to pass app url, https certificate location and ssl certificate path. These are:
ASPNETCORE_URLS="https://+;http://+". This means that the APP will be opened in both http and https.
ASPNETCORE_Kestrel__Certificates__Default__Password="mypass123". Recall it is mypass123.
Next, with the volume “-v” I have specified where to look for the ssl certificate on the drive. My certificate is outside of the container and is mapped to the container using a volume instead of bundling it together with the app in the image.
I used the below code for doing this work:
Finally at the last of the docker run command I specified the docker image to run inside this container- dhttps:v1.
Now you can open the URL of the app in our browser – https://localhost:7001/. The app will open from the docker container with HTTP certificate full working. I have shown this in the below video.
You can download the source code:
In this tutorial you learn how to use HTTP certificate for ASP.NET Core app running in a Docker Container. You also learned how to generate development https certificate for ASP.NET Core app and the way to tell docker container about it’s path by using volume mapping. Hope you liked reading and learning from it. Kindly share it on your facebook and twitter accounts so that other people can also learn this.