How to deploy your ASP.Net Core Web API on Heroku using Docker

Deploy your ASP.Net Core Web API on Heroku for free using Docker

Benny
Bina Nusantara IT Division
4 min readMar 30, 2022

--

As a developer, sometimes we like to create applications such as games, web applications, and others that require data from a web API and we want to show it to our colleagues or friends. The problem is we have to publish the web API so that it can be used in general. We don’t have servers to deploy web APIs and we don’t have enough money to use Azure or AWS. And the solution is that we can use Heroku to deploy our Web API using Docker as a container.

What is Heroku?

Heroku is a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud.

Currently, Heroku supports languages Ruby, Java, PHP, Python, Node, Go, Scala and Clojure but unfortunately not .Net or .Net Core. That’s why we need Docker in order to deploy our .Net core Web API. Heroku has a feature that allows us to deploy our applications using Docker.

Getting Started

Install Docker

The first thing we need to do is install Docker. You can download and install Docker from the official website https://www.docker.com/.

Create new ASP.Net Core Web API

After we install Docker, we will create an ASP.Net Core 3.1 Web API application. Let’s open our Visual Studio and create a new project.

After that we enter the name of the project according to our wishes and the location of the project.

Next, we define the Target framework that we will use and for now we will choose .NET Core 3.1.

Finally, your project should look like this.

The default template comes with a WeatherForecastController it has nothing special just a plain controller with some endpoints. You can modify it you if like.

Next, we need to create a Dockerfile to run some commands. The script should look like this.

And now, Using the terminal, change the directory to the location where Dockerfile resides and run this command.

-- Build docker image
docker build -t benny_api_image .
-- Run docker image and create new container
docker run -d -p 5000:80 --name benny_api_container benny_api_image

Hit http://localhost:5000/weatherforecast in your browser and you’ll see the return from the API.

Getting ready for the Heroku

We need to modify our script “Program.cs” before publishing it to the cloud.

After that, We need to install Heroku-CLI that will help us to deploy images to Heroku. Let’s create a new app on Heroku.

Input the name for your new app. The name should be lowercase, number, and dash. If you browse to your app (https://benny-api.herokuapp.com/). You will see a welcome screen.

Now it’s time for us to deploy our API to Heroku. We need to login first to Heroku via CLI. Open your terminal and move to the directory where your Dockerfile is located. After that enter the command below.

heroku login

It will open the login page in the browser.

After you have successfully logged in, run the following commands.

-- Login to Heroku account
heroku login
-- Login to Heroku container
heroku container:login
-- Login to heroku docker
docker login --username=_ --password=$(heroku auth:token) registry.heroku.com
-- Build heroku docker image
docker build -t registry.heroku.com/benny-api/web .
-- Push heroku docker image
docker push registry.heroku.com/benny-api/web
-- Release the image on heroku
heroku container:release web --app benny-api

“benny-api” is the app name, you can replace it with your app name.

Now, if you hit the URL “https://benny-api.herokuapp.com/weatherforecast” it’ll return some data.

Congratulations, you have successfully deployed your Web API and access it from anywhere.

Conclusion

To deploy your Web API is not so difficult and expensive, using Heroku and Docker you can deploy easily and for free. And now you can share your application with everyone.

--

--