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
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.
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
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.
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.
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 /appCOPY *.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 outFROM 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.
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:
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
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.
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>