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.

Photo by Guillaume Bolduc on Unsplash

Prerequisites:

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.

Login:

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.

All done!

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

Thanks!

The Startup

Get smarter at building your thing. Join The Startup’s +793K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Alejandro Dominguez

Written by

Software Developer at Docplanner

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +793K followers.

Alejandro Dominguez

Written by

Software Developer at Docplanner

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +793K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store