What is this all about?

Heroku is a Paas for developers to build and deploy applications in the cloud. The deployed apps are run on isolated, virtualized Linux containers called dynos. The officially supported languages are
Node.Js, Ruby, Java, PHP, Python, Go, Scala and Clojure.
Even though .Net Core is not officially supported, we will look at how to run .Net Core Web API with Docker and deploy it to Heroku.

What should I have already?

  1. Net Core 3.1
  2. Visual Studio Code
  3. Docker Desktop for Windows
  4. Heroku Account
  5. Heroku cli
  6. Basic knowledge about writing code in .Net platform (C#).
  7. Basic knowledge about docker.

What are we trying to do?

  1. Creating an App in Heroku
  2. Adding docker support to your .Net Core 3.1 Web API.
  3. Deploying the app to heroku.

How can I start ?

Creating an App in Heroku
  1. Create a new App in Heroku

2. Type in the name and select the region

Adding docker support to your Web API

Go to this post to understand how to add docker support to your project. Once that is done there is a small change to be made in the Dockerfile. Look at the last two lines of the file.

The web process must use the $PORT variable when running with heroku. The EXPOSE will not be used by heroku. So to run the application in heroku we use
CMD ASPNETCORE_URLS=http://*:$PORT dotnet dotnetcore31heroku.dll
instead of ENTRYPOINT ["dotnet", "dotnetcore31heroku.dll"]

Deploy the app to heroku

Now that we have dockerised our project and have a docker image built in local, we a few more steps to make our app deployed in heroku accessible to the world wide web.

1.Tag from source image to target image using the following code

docker tag <docker-image-name> registry.heroku.com/<heroku-app-name>/web

In my case the docker image name and heroku app name are the same, so the command would look like

docker tag dotnetcore31heroku registry.heroku.com/dotnetcore31heroku/web

2. Use heroku cli to deploy the docker image to heroku

heroku login
heroku container:login

You should see a message that says Login Succeeded

3. Push the container to heroku and then release it to web

heroku container:push web -a <heroku-app-name>
heroku container:release web -a <heroku-app-name>

For me it would look like

heroku container:push web -a dotnetcore31heroku
heroku container:release web -a dotnetcore31heroku

To access the app the url format would be
https://<herokuappname>.herokuapp.com/

Now that we have deployed our sample app in heroku we have to look at how to do it for our professional app or website. Because we can’t make changes and push the image to heroku container registry from our local machine every time we make a change. So we have to implement a CI/CD flow for that. But that is a topic for another post.

Feel free to post your thoughts as comments or email.

writer@adevtalks.com

Ajay Bhasy

By Ajay Bhasy

7 Years of experience in developing applications with .Net (C#). 2 Years of Experience with Angular. Strong advocate of Continuous Integration and Delivery practices with experience in using Bamboo,Octopus Deploy and docker. Engineering professional with a Bachelor’s Degree focused in Information Technology from Dr. NNCE. Recently started learning technologies like MongoDB and Node Js

2 thought on “Deploy .Net Core 3.1 Web API to Heroku”
  1. Great tutorial! I’m interested in the last sentences of the tutorial, will u release a implementation tutorial of CI/CD flow? Do you know some guides?

Leave a Reply

Your email address will not be published. Required fields are marked *