Deploy Your .NET Core 3.1 Application to Heroku With Docker
Deploying a Docker image on Heroku is very simple but there are some details to keep in mind. In this article, we will explain step by step everything that must be done to avoid setbacks.
Create your web application:
First, we will create a new MVC application with .NET Core 3.1 Framework and run it to see that everything went correctly.
If everything went well we should be able to navigate through the default address https://localhost:5001
Create, build and run your docker image:
Create a file called Dockerfile in the root of your application. And configure your image.
If you are familiar with Docker files you may wonder why we don’t EXPOSE any port. The reason is that Heroku hopes to handle the ports for us mainly for security reasons. We will also have to change the code of our application to be able to listen to the requests for the port that Heroku assigns us.
Change your Program.cs file like the example below.
If you do not include this line, you will probably get an error when you deploy your application:
Unable to start Kestrel. System.Net.Sockets.SocketException (13): Permission denied
Host your application with Heroku
Create new app:
Go to Heroku Dashboard and create your application.
First we have to install Heroku CLI on our PCs. Then log in to your Heroku account and follow the prompts to create a new SSH public key.
Log in to Container Registry:
Make sure you have Docker well configured.
Push your Docker-based app:
Build the Dockerfile in the current directory and push the Docker image. Don’t forget to specify your application name using the -a option.
Release your app:
Release the newly pushed images to deploy your app.
Check the logs:
Take a look the application logs and check that everything looks correct.
Everything ready, we already have our online application with just executing a few steps. If you need to configure environment variables you can do it in: Application> Settings> Config Vars