Deploy .Net Core App to Heroku
In this article, we will see how we can deploy .Net Core Apps to Heroku for free using docker.
As a developer, we love to do some side projects and sometimes we want to show it to our fellow coders or the rest of the world. In most cases, we run it locally and show it to our friends but what if we want to deploy it to the cloud, then we need some paid cloud services like Azure or AWS. But the thing is, most of the time our side project is just a POC and we don’t want to spend a penny on it, so in this scenario, Heroku comes to save the day.
Things we need
- .Net Core App
- Docker
- Visual Studio or VS Code
- Heroku free account
- Heroku CLI
Things to know
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. This where Docker comes into the picture. Heroku provides ways to deploy apps with Docker.
Containers are a standardized unit of software that allows developers to isolate their app from its environment, solving the “it works on my machine” headache. For millions of developers today, Docker is the de facto standard to build and share containerized apps — from desktop, to the cloud.
Let’s Start
Install Docker
If you don’t have Docker in your system make sure to install it first from Docker's official website.
Create a .Net Core App
We will create an ASP.Net Core 3.1 Web API application for this article. 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 a Dockerfile in the solution to add docker capabilities.
Create a Dockerfile next to csproj.
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS baseWORKDIR /app
EXPOSE 80
EXPOSE 443FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS buildWORKDIR /srcCOPY ["./BlogDemo.csproj", "src/"]RUN dotnet restore "src/BlogDemo.csproj"COPY . .WORKDIR "/src/"RUN dotnet build "BlogDemo.csproj" -c Release -o /app/buildFROM build AS publishRUN dotnet publish "BlogDemo.csproj" -c Release -o /app/publishFROM base AS finalWORKDIR /appCOPY --from=publish /app/publish .ENTRYPOINT ["dotnet", "BlogDemo.dll"]
You can visual studio to run the app but let’s do it with windows terminal or command prompt.
Run Locally
Using the terminal, change the directory to the location where csproj and Dockerfile resides.
-- Build docker image
docker build -t blogdemo .-- Run docker image
docker run -d -p 5000:80 --name myapp blogdemo
Hit http://localhost:5000/weatherforecast in your browser and wow it’s running.
If till now everything is OK, it means we are ready to deploy it to the cloud.
Getting ready for the cloud
We need to modify Program.cs before publishing it to the cloud.
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;namespace BlogDemo
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureKestrel(serverOptions =>
{
serverOptions.Listen(IPAddress.Any, Convert.ToInt32(Environment.GetEnvironmentVariable("PORT")));
}).UseStartup<Startup>();
});
}
}
We need to install Heroku-CLI that will help us to deploy images to Heroku.
Let's create an app on Heroku.
You can choose an appropriate app name. Here I am using my name for the app name.
If you browse to your app (https://vikas-sharma.herokuapp.com/). You will see a welcome screen.
Now, it's time to deploy our API to Heroku.
We need to modify our Dockerfile before we can deploy it to the cloud.
Just change the last line of Dockerfile to —
CMD ["dotnet", "BlogDemo.dll"]
We need to login to Heroku via CLI. Open your terminal and get ready for some action.
Steps In Terminal:
- Change directory to the location where csproj and Dockerfile resides.
2. Type below commands
heroku login
It will open the login page in the browser.
* heroku container:login
* docker login --username=_ --password=$(heroku auth:token) registry.heroku.com
* docker build -t registry.heroku.com/vikas-sharma/web .
* docker push registry.heroku.com/vikas-sharma/web
* heroku container:release web --app vikas-sharma
Here ‘vikas-sharma’ is the app name, you can replace it with your app name.
Now visit the URL: https://vikas-sharma.herokuapp.com/weatherforecast
And you are done!! The app is deployed and you can access it from anywhere. 😄
Final Words
It is very easier to deploy your apps to Heroku if you follow the steps correctly and after that, you can share your app with the world 🌏.
Happy Coding!!! 🤗