Deploying ASP.NET Core Apps on Heroku servers Containerized — Straight Forward.

Kelechi Onyekwere
4 min readSep 21, 2020

Yeah I know, tired of the cost of AWS and Azure servers , you just want to host that small ASP.Net Web API, or your .Net Web App, in no time and lesser resource. Yup you have met that article to guide you.

What this article will discuss:

  1. Setting up your .Net App Project ready for deployment to Heroku.
  2. Configure your Docker file the right way.
  3. Set up Github actions for CI/CD.
  4. Adding .Net Core 3.1 Build pack to Heroku.
  5. Configuration on Heroku server.

What I will not discuss:

  1. How to write your .Net App :) .
  2. How to push to Github .
  3. And the other stuff you feel is missing :) lol.

Project Set-up for Deployment:

Start off by enabling Docker while creating your project and set environment to Linux. Have a strong internet connection in order to download the necessary image files.

After project set up, I assume you have written your app or api and it’s in a good working condition.

Configure your Docker file ready for deployment:

After you successfully add docker to your project you should see a Dockerfile in your project root folder and a .dockerignore file, so we would configure the docker file to serve on Heroku ports.

Each item in the .dockerignore file will be disregarded by the docker engine when building an image. The Dockerfile defines a set of instructions that creates an image that contains our ASP.NET Core application. This docker image can be used to create containers for our local development environment, private cloud, or public cloud.

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS baseWORKDIR /appEXPOSE 80EXPOSE 443FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS buildWORKDIR /srcCOPY [“cryptor/cryptor.csproj”, “cryptor/”]RUN dotnet restore “cryptor/cryptor.csproj”COPY . .WORKDIR “/src/cryptor”RUN dotnet build “cryptor.csproj” -c Release -o /app/buildFROM build AS publishRUN dotnet publish “cryptor.csproj” -c Release -o /app/publishFROM base AS finalWORKDIR /appCOPY — from=publish /app/publish .ENTRYPOINT [“dotnet”, “cryptor.dll”]

By default this is how your Dockerfile should look like when it is generated, but we would be making few adjustments to the end of the file , so we first comment out the last line which is the Entry Point by adding a “#” in front of it , and then add :

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

Replace “cryptor” with the name of your app.

The Heroku web process listens for HTTP traffic on $PORT, which is not necessarily port 80. Heroku exposes a port number $PORT as an environment variable. So we need to set the default URL for our containerized app using an environment variable (ASPNETCORE_URLS=http://*:$PORT) that ASP.NET Core recognizes.

That done , let set up our Github action.

Setting up Github Action:

Okay, so again, I assume you already pushed your project to your Github repository , let’s add the Action Workflow.

So navigate to your repository and click on the ‘Actions’ Tab:

In the Tab create a new Github workflow and select .Net Core template.

Now watch as your github build passes , provided that your .Net Core app builds successfully 😁 .

Adding .Net Core 3.1 Build pack to Heroku:

Once again let me assume you can create applications on Heroku servers, so log in to your account and create that application , navigate to settings page of the respective application,

Scroll down to Build pack section and click on add build pack, as at the time of writing this article Heroku does not have the .Net Core build pack in pre-menu , so we have to add our own build pack, in the url input field , paste in:

https://github.com/jincod/dotnetcore-buildpack

This is the Heroku buildpack for ASP.NET Core.

The Build pack supports C# and F# projects. It searchs through the repository’s folders to locate a Startup.* or Program.* file. If found, the .csproj or .fsproj in the containing folder will be used in the dotnet publish <project> command.

If repository contains multiple Web Applications (multiple Startup.* or Program.*), PROJECT_FILE and PROJECT_NAME environment variables allow to choose project for publishing.

For Migrations, check the github repo on how to make migrations after publishing.

Configuration on Heroku server:

Still on the Heroku Tabbed page , navigate to the Deploy Tab:

Scroll down and connect your github app repo(.Net app pushed to github) to the heroku application.

Enable Automatic Deploy from your Set Branch usually “Master”, check or un-check continuous integration based on if you have it set up for your project via Github.

Now sit back and watch as your build passes or check the log file if failed, if successful then you should see your app reflect on your heroku domain pointing to your app .

Give me some claps 😁

#programming #dotnet #C# #CSharp #ASP #Heroku

--

--

Kelechi Onyekwere

I’m a Software Engineer with experience building distributed systems, resilient and fault tolerant solutions and an advocate for event sourcing / driven system.