Dockerize an Asp .Net Core application and deploy to Heroku
By: Barbara Ezomo
Tags: Stay At Home, Heroku, Docker, Net Core, App Development

Introduction

In this tutorial, we will create a sample asp .net core application and deploy it to Heroku using docker.

Requirements

Please make sure you have all the items in the list below installed on your computer.

Before we move on with this tutorial we need to understand some key terminologies and tools we will be using to build the application.

What is Docker?

Docker is an open source containerization platform. Docker enables developers to package applications into containers — standardized executable components that combine application source code with all the operating system (OS) libraries and dependencies required to run the code in any environment. While developers can create containers without Docker, Docker makes it easier, simpler, and safer to build, deploy, and manage containers. It’s essentially a toolkit that enables developers to build, deploy, run, update, and stop containers using simple commands and work-saving automation.

Source: ibm.com

What is ASP.Net Core?

ASP.NET Core is the open-source version of ASP.NET, that runs on macOS, Linux, and Windows. ASP.NET Core was first released in 2016 and is a re-design of earlier Windows-only versions of ASP.NET.

Source: dotnet.microsoft.com

What is Heroku?

Heroku is known for running apps in dynos — which are really just virtual computers that can be powered up or down based on how big your application is. Think of dynos as malleable building blocks for running your app. Although Heroku charges you by the dyno, they aren’t actually hosting your app. In fact, the entire Heroku platform, as well as every app built on Heroku is deployed to Amazon Web Services(AWS)

Source: trifinlabs.com

Now that we are up to speed on our terminologies, let's get to coding!

STEP ONE

Open Visual Studio and create an asp.net core MVC project

When configuring select the .Net Core version installed on your system.

Name the application and select the checkbox for source control.

STEP TWO

Right-click on the solution folder and add docker support to the application. If you are running on windows make sure to select Linux containers for this.

STEP THREE

Build the application and run.

STEP FOUR

Now that our application is running, log in to Heroku, navigate to the Heroku dashboard and create a new application

before we move on, let us examine the docker file and its components. This file can be found in the root folder of the application and was created after we added docker support to it. The phrase below best describes what a Dockerfile is.

A Dockerfile is a recipe (or blueprint if that helps) for building Docker images, and the act of running a separate build command produces the Docker image from that recipe.

Source: https://nickjanetakis.com/blog/differences-between-a-dockerfile-docker-image-and-docker-container

Here is the Dockerfile that was generated

NOTE: Depending on the version of .Net Core installed or the operating system being used, the below code generated may vary.

If you wish to know what every reserved word means in the Dockerfile above, I recommend you visit here for a more detailed explanation.

STEP FIVE

Move Dockerfile to the root of the solution, mostly because of the commands we will be running from the terminal.

Once that is done we need to update the program.cs file and set the URL to the default port used by Heroku as seen in the screenshot below.

STEP SIX

Now that our application environment is all set up, it’s time to build our docker app from the command line using the below command

docker build -t webapi .

To verify that our app is running in our container we use the command

docker run -p 8181:80 webapi

After confirming this you can press ctrl-c to shut down the app

STEP SEVEN

We are now at the end stages of our deployment, this code below will help us stage our docker container to the Heroku app we created earlier.

docker tag webapi registry.heroku.com/web-app-tut/web

NOTE: web-app-tut is the name of our Heroku app

Before we move any further we need to login to Heroku command line using, after which you will be redirected to a browser where you put in your credentials.

heroku login

To push all the contents of our docker container to the Heroku application container we used the command below

docker push registry.heroku.com/web-app-tut/web

After this, to finally allow our application to be accessible we use the code below to release the application.

heroku container:release web -a web-api1

Finally, we can access our application on our Heroku app link like this

You can check it out in here

And That’s it!! We finally have a fully functional .Net Core application running successfully on Heroku. There are definitely some other concepts I’ll have to address in further tutorials such as adding a database connection to the application but this is where I’ll stop for now.

In the meantime, feel free to drop any comments or questions on any of my social media pages, LinkedIn, or Twitter (@ez_barbie).

Thanks for reading :D