Most developers and companies need a service provider to deploy their apps. There are many different ways to do so, however with Heroku it is simple and straightforward to deploy, manage,scale and configure an app. Heroku is a cloud computing service provider and supports many modern app languages for instance Python, Ruby, Java, and JavaScript. Unfortunately Heroku does not support .NET Core apps but there is a way to deploy a .NET Core application to Heroku through Docker.
Docker is a virtual machine which makes deployment of an app easier and efficient by using lightweight containers for all environments. A container accommodates all the dependencies required to run an applicaion.
Deploying a .NET Core app to Heroku has its prerequisites:
After installing the prerequisites create a ASP.NET Core Web Application from Visual Studio 2019.
Start a project and when it is done and final (for now), add a docker file to the solution. Right click on the solution, then hover on Add and finally select Docker Support.
When clicked on, Docker Support will add a Dockerfile. And if Docker is not running this message will pop-up.
Now go to the Dockerfile and configure the file as follows:
This is the default Dockerfile when added to the solution. Now configure the Dockerfile. (F1SampleApp is the name of the solution)
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS baseWORKDIR /appFROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS buildWORKDIR /srcCOPY ["F1SampleApp.csproj", "F1SampleApp/"]RUN dotnet restore "F1SampleApp/F1SampleApp.csproj"WORKDIR "/src/F1SampleApp"COPY . .RUN dotnet build "F1SampleApp.csproj" -c Release -o /app/buildFROM build AS publishRUN dotnet publish "F1SampleApp.csproj" -c Release -o /app/publishFROM base AS finalWORKDIR /appCOPY --from=publish /app/publish .CMD ASPNETCORE_URLS=http://*:$PORT dotnet F1SampleApp.dll
After this step go to your Heroku account and create a Heroku App.
After the app created go to the project folder (not the solution folder, because Dockerfile is located in the project folder), open a powershell console and run the following command:
PS C:\Users\Hp\Desktop\f1project\F1SampleApp\F1SampleApp> docker build -t <docker-image-name> .
Be careful with the dot at the end. This command will build a docker image. If Docker is not running an error might occur.
If the command succeeds run the tag command to tag from source image into target image(on Heroku). Heroku app name is the app name when a Heroku App created.
PS C:\Users\Hp\Desktop\f1project\F1SampleApp\F1SampleApp> docker tag <docker-image-name> registry.heroku.com/<heroku-app-name>/web
Now that an image has been created, all that’s left is to deploy this image using Heroku CLI commands. First, login from PowerShell to Heroku.
PS C:\Users\Hp\Desktop\f1project\F1SampleApp\F1SampleApp> heroku login
This command will lead to a web page for verification after pressing any key. After verification run this command to login to the container.
PS C:\Users\Hp\Desktop\f1project\F1SampleApp\F1SampleApp> heroku container:login
Now push the container to Heroku.
PS C:\Users\Hp\Desktop\f1project\F1SampleApp\F1SampleApp> heroku container:push web -a <heroku-app-name>
Finally release the project to the web.
PS C:\Users\Hp\Desktop\f1project\F1SampleApp\F1SampleApp> heroku container:release web -a <heroku-app-name>
In the end test the results. Go to “<heroku-app-name>.herokuapp.com.” and see if the project is running.
Now a .NET Core application is deployed to Heroku.
Sources:
https://jakubwajs.wordpress.com/2020/01/31/deploying-asp-net-core-3-1-web-api-to-heroku-with-docker/