Deploying a Docker-based .NET Core 3.1 Application With Multiple Projects to Heroku

How to create a .NET Core application with multiple projects, a Docker image with it, a Heroku app, and deploy your project on it

Heroku activity
Heroku activity

When I discovered that I could deploy one of my applications to a free server, and everyone could use what I created, I was thrilled to try it. Heroku offers you exactly that!

In this article, I will cover what is necessary for you to deploy your own .NET Core 3.1 application with multiple projects to Heroku using a Docker image of your project.

This article is divided into three parts:

  • Installing Heroku CLI and creating an application
  • Installing Docker and building an image of your application
  • Deploying to Heroku

1. Installing Heroku CLI and Creating an application

After creating an account on Heroku, you just have to click New and then Create new app.

Create a new Heroku app
Create a new Heroku app
Creating a new Heroku app

Now you just have to choose a name for an app and a region where the server will be. Your app name will determine your application URL, following the pattern <your-heroku-app-name>.herokuapp.com

App creation page
App creation page

When the app is created you will see the Deploy tab of your app, where you can add a new pipeline to your app and two ways to deploy to Heroku, via GitHub and via Docker.

You can already visualize your application by clicking on Open App located at the upper right corner of the screen. Your newly created app will look like this.

Your newly created Heroku app
Your newly created Heroku app
Initial Heroku app

The last step for this section is to download and install the Heroku CLI and you can do so here.

2. Installing Docker and building an image of your project

Now it’s time to turn your project into a Docker-based project. The first step is to download and install Docker, and you can do it by clicking here. Download and install Docker Desktop.

After successfully installing Docker, open your .NET Core 3.1 project and create a file called Dockerfile (no extension) in your project root. You can also create one by right-clicking a project → Add → Docker Support → Linux.

Creating the Dockerfile via Visual Studio options
Creating the Dockerfile via Visual Studio options
Creating a Dockerfile by right-clicking a project → Add → Docker Support

The Dockerfile will be created on your project folder, place it in the root folder.

Visual Studio already creates a Dockerfile with some content. The final version should look like this:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 as build
WORKDIR /app
COPY *.sln .
COPY <PROJECT_1_FOLDER>/*.csproj ./<PROJECT_1_FOLDER>/
COPY <PROJECT_2_FOLDER>/*.csproj ./<PROJECT_2_FOLDER>/
COPY ./<PROJECT_3_FOLDER>/*.csproj ./<PROJECT_3_FOLDER>/
RUN dotnet restoreCOPY <PROJECT_1_FOLDER>/. ./<PROJECT_1_FOLDER>/
COPY <PROJECT_2_FOLDER>/. ./<PROJECT_2_FOLDER>/
COPY <PROJECT_3_FOLDER>/. ./<PROJECT_3_FOLDER>/
WORKDIR /app
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS runtime
COPY --from=build /app/out ./
CMD ASPNETCORE_URLS=http://*:$PORT dotnet <NAME_OF_THE_PROJECT_YOU_WILL_RUN>.dll

You can add any number of projects you want to this file, including test projects, just copy the .csproj files, and the folder contents for every additional project that you have.

FROM mcr.microsoft.com/dotnet/core/sdk:3.1

The line above shows the base image for the new image that you are creating. You can choose different Docker images depending on your needs. These images can be found on the Docker website. Here is the link for the image I used.

Note that not hard coding a port is very important, Heroku listens to the HTTP request in the port $PORT, which is not necessarily the port 80. When your application starts, the port expected by Heroku is going to be assigned to the variable. So you should use like this:

ASPNETCORE_URLS=http://*:$PORT

Now it’s time to build your Docker-based project and create a new Docker image.

> docker build -f Dockerfile -t <repository_name>.

Run this command in the root folder of your application (where the Dockerfile is located). You can choose any repository_name you like. Now you have an image for your project!

Check the newly created image with the command

> docker images
Image that was created with the docker build command
Image that was created with the docker build command
The image that was created with the command docker build

Note that, in the example above, the library-manager-api repository name was added with the -t parameter in the docker build command.

3. Deploying your Docker-based project to Heroku

The first step is to log in to Heroku via the command prompt, to do this, just run the command

> heroku login

You will be asked to click any key to open a browser window to validate your login or click q to exit.

The massage that appears after a successful login
The massage that appears after a successful login
Message received after a successful log in

After that, log in to your docker container with the following command:

> heroku container:login

The last two steps of this last step are pushing your application to Heroku and deploying it.

The former is done with this command:

> heroku container:push web -a <your-heroku-application>

Make sure to specify the name of your Heroku application with the -a property. This step will build your Dockerfile in the current directory and push the Docker image to Heroku.

And the latter is done with this command:

> heroku container:release web -a <your-heroku-application>

Again, it is important to specify your Heroku application.

And your application is deployed!

You can check it by clicking on Open App as mentioned earlier.

Note: You can check the Heroku logs in case of any problems with the deployed application. To check the logs, run the command:

> heroku logs --tail -a <your-heroku-application>

The Startup

Medium's largest active publication, followed by +754K people. Follow to join our community.

Luiz Ferreira Carvalho

Written by

Software developer, Computer Science student, and community contributer. Always trying to learn new things to make a difference.

The Startup

Medium's largest active publication, followed by +754K people. Follow to join our community.

Luiz Ferreira Carvalho

Written by

Software developer, Computer Science student, and community contributer. Always trying to learn new things to make a difference.

The Startup

Medium's largest active publication, followed by +754K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store