Create and publish an ASP.Net Core Web Application in Heroku
By: Sophia Dizon
Tags: C Sharp Programming, Docker, Programming, Heroku, Aspnetcore

Heroku enables developers to publish their applications for free in the cloud. This article provides instructions from creating simple ASP.Net Core Web Application then deploy it to Heroku using Docker.

THINGS THAT YOU NEED

Visual Studio 2019

.Net Core 3.1

Windows 10 Home Operating System

Docker Account (You have to sign up) — https://hub.docker.com/

Heroku Account (You have to sign up) — https://www.heroku.com/

Computer Processor that supports Virtualization

STEP 1: CREATE ASP.NET CORE WEB MVC APPLICATION

Select ASP.NET Core Web Application

Then define other project properties like project name, the location you prefer, and solution name. Then click CREATE

You will be asked which framework you prefer, all available or installed frameworks in your machine will be listed. Please select .Net Core 3.1

Also do not forget to enable the docker support, we will be needing docker to deploy to Heroku

To sum it up, ASP.Net Core 3.1, Web Application (Model-View-Controller) and Enable Docker Support

If you were prompted to install docker desktop, click NO, we’ll to get to that later

Try to run the application using IIS Express to check if everything is working

You should get something like the screenshot below:

STEP 2: INSTALL DOCKER TOOLBOX

As described in THINGS THAT YOU NEED section, you need to have your docker account.

The docker installation will be different to computers with Windows 10 Home Operating System than with Windows 10 Pro or Enterprise because of the Virtualization features. Hence, instead of installing a docker desktop, we will be installing Docker Toolbox. This is noted on the docker website

As defined on the requirements (Things that you need section), your computer must support virtualization. Quick means of checking it is by accessing your computer’s task manager

If you cannot see any virtualization status, therefore you cannot proceed with docker. If you saw that Virtualization is disabled, you have to enable it by accessing your BIOS Configuration. This article can guide you to enable virtualization How to Enable Virtualization on Windows 10

Back to docker toolbox documentation, you can follow step by step instructions in here Install Docker Toolbox on Windows to install docker toolbox

Docker documentation will provide you their releases and you can check it here: Releases · docker/toolbox. It will require you to download and install executable file, at this time the version I used is v19.03.1

It usually takes a couple of minutes to complete the docker installation. And after it is completed you will have docker quickstart terminal shortcut

Please note to run the shortcut in administrator mode (in case your account is not an administrator) otherwise you will encounter some issues like what is described below:

Once everything is installed, you should be able to see the terminal with something described below:

We will use this terminal to execute Heroku CLI commands

STEP 3: SETUP HEROKU APP

You should sign up for a Heroku account.

Once you logged in, you will be redirected to your dashboard which contains all apps you’ve created

Click NEW then select “Create new app”

Give it a name of you liking then click CREATE APP

Once the app is completed you will be presented with your application dashboard

The deployment method that we will use is Container Registry using docker

Once you clicked CONTAINER REGISTRY, you will see instructions beneath

We will execute those instructions using docker toolbox CLI

STEP 4: INSTALL HEROKU CLI

As noted on the instructions, you have to download the Heroku CLI — at this time the URL is https://devcenter.heroku.com/articles/heroku-cli

Select which installer fits your OS architecture, download and install

Once the installation is completed, you can verify if it is successful by executing the command

heroku — version

And it should provide you version installed in your machine

STEP 5: LOGIN TO HEROKU ACCOUNT USING DOCKER TOOLBOX CLI

Execute the following command in your Docker Toolbox CLI:

heroku login

After pressing any key (except Q), a CLI will open browser for you to log in, enter your credentials

Complete your login, then close the browser

Back to your Docker Toolbox CLI, it should contain logged-in information

STEP 6: MODIFY DOCKER FILE

Open the dockerfile

Then remove the highlighted section

Remaining commands specifies the working directory which will be the files in the published folder (publishing a project will be discussed after modification of docker file is completed)

Add the copy command below, this command copies files from source to destination (COPY <source> <destination>), since later on, we will put this docker file inside the published folder, therefore, we will just use a dot for source path, the same with destination path (as it will be inside the working directory):

COPY . .

Then we will add a command to bind Heroku $PORT value to Kestrel using ASPNETCORE_URLS then save:

CMD ASPNETCORE_URLS=http://*:$PORT dotnet SampleApplication.dll

Please note that the DLL name on the port binding command syntax above will be the name of your project. Therefore if you are not using SampleApplication, replace it with your MVC project name then concatenate .dll

STEP 7: PUBLISH YOUR ASP.NET CORE MVC APP

Select your project

Then right-click, select publish

Select FOLDER

And the location for your published files, the default will be inside your application folder.

Leaving the folder as-is, click CREATE PROFILE

Then click PUBLISH

Your project will now start its publishing process

Once completed and successful, you will be able to see the published folder location

CTRL+click the location to open the folder

STEP 8: PLACE DOCKER FILE IN PUBLISH FOLDER

Open the containing folder of your dockerfile

Copy the docker file to your publish folder

Your publish folder should look similar to below image:

STEP 8: BUILD DOCKER IMAGE

Login to your Heroku container by executing the command below:

heroku container:login

The build command syntax will be

docker build -t <name of your heroku app> <publish folder location>

Below image is a sample that the build is successful

STEP 9: TAG THE BUILT IMAGE

Use below command to tag the built image to Heroku:

docker tag <name of your heroku app> registry.heroku.com/<name of your heroku app>/web

STEP 10: PUSH THE DOCKER IMAGE TO HEROKU

Use below command to push the docker image to Heroku:

docker push registry.heroku.com/<name of your heroku app>/web

Wait until the process completes

STEP 11: RELEASE YOUR APP

You won’t be able to see your published app until you execute the Heroku release command below:

heroku container:release web — app <name of your heroku app>

Wait for it to complete

Once successful you can now check your newly published app!

In this tutorial, URL should be: https://dotnet-sample-app.herokuapp.com/

You can check out the settings of your Heroku app to determine your app URL but the usual is: