Continuing the .Net Core series.
In this chapter, we will deploy a .Net Core 3 web API to Heroku by using Docker.
The full example can be downloaded in my GitHub repository: NetCore3 Heroku.
Technologies
Before start
This guide is directed for those who have a mininum experience with Docker and a proper environment to run it.
Topics
Heroku
Heroku is a cloud platform that allows applications be hosted at will. It's mostly used for web APIs.
Go to Heroku website and sign up or sign in.
In the Dashboard, click on "Create new app".
Name the app and click on "Create app".
In your machine, install the latest version of Heroku CLI here.
.Net Core 3
Download the web API from this GitHub repository: NetCore3-MySQL.
Follow the instructions to make the required database connection.
In the api folder, create manually a file called "Dockerfile" without extension and edit it with this lines:
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
WORKDIR /app
COPY ./ ./
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim
WORKDIR /app
COPY --from=build /app/out .
CMD ASPNETCORE_URLS=http://*:$PORT dotnet NetCore3WebAPI.dll
Docker
Run the following commands in the api folder replacing [MYAPP] by the app's name created on Heroku.
docker build -t [MYAPP] .
To authenticate with Heroku, run this command. It will be required to press any key to open a web browser window containing a login button.
heroku login
Then, return to the terminal and run:
heroku container:login
Tag the image:
docker tag [MYAPP] registry.heroku.com/[MYAPP]/web
Build the image locally and push it to Heroku's Container Registry:
heroku container:push web -a [MYAPP]
Release the image on Heroku to be deployed:
heroku container:release web -a [MYAPP]
Access the URL of your app with Swagger:
https://[MYAPP].herokuapp.com/swagger/index.html
Conclusion
It was very simple to publish on Heroku. Docker was used only to build the image.
Now you have a remote database and a deployed web API.
Next
We will integrate the web API with Entity Framework to manipulate the database only by C# objects and Lambda Expression.
Top comments (0)