What is this all about?
Heroku is a Paas for developers to build and deploy applications in the cloud. The deployed apps are run on isolated, virtualized Linux containers called dynos. The officially supported languages are
Node.Js, Ruby, Java, PHP, Python, Go, Scala and Clojure.
Even though .Net Core is not officially supported, we will look at how to run .Net Core Web API with Docker and deploy it to Heroku.
What should I have already?
- Net Core 3.1
- Visual Studio Code
- Docker Desktop for Windows
- Heroku Account
- Heroku cli
- Basic knowledge about writing code in .Net platform (C#).
- Basic knowledge about docker.
What are we trying to do?
- Creating an App in Heroku
- Adding docker support to your .Net Core 3.1 Web API.
- Deploying the app to heroku.
How can I start ?
Creating an App in Heroku
- Create a new App in Heroku
2. Type in the name and select the region
Adding docker support to your Web API
Go to this post to understand how to add docker support to your project. Once that is done there is a small change to be made in the Dockerfile. Look at the last two lines of the file.
The web process must use the
$PORT variable when running with heroku. The
EXPOSE will not be used by heroku. So to run the application in heroku we use
CMD ASPNETCORE_URLS=http://*:$PORT dotnet dotnetcore31heroku.dll
ENTRYPOINT ["dotnet", "dotnetcore31heroku.dll"]
Deploy the app to heroku
Now that we have dockerised our project and have a docker image built in local, we a few more steps to make our app deployed in heroku accessible to the world wide web.
1.Tag from source image to target image using the following code
docker tag <docker-image-name> registry.heroku.com/<heroku-app-name>/web
In my case the docker image name and heroku app name are the same, so the command would look like
docker tag dotnetcore31heroku registry.heroku.com/dotnetcore31heroku/web
2. Use heroku cli to deploy the docker image to heroku
heroku login heroku container:login
You should see a message that says Login Succeeded
3. Push the container to heroku and then release it to web
heroku container:push web -a <heroku-app-name> heroku container:release web -a <heroku-app-name>
For me it would look like
heroku container:push web -a dotnetcore31heroku heroku container:release web -a dotnetcore31heroku
To access the app the url format would be
Now that we have deployed our sample app in heroku we have to look at how to do it for our professional app or website. Because we can’t make changes and push the image to heroku container registry from our local machine every time we make a change. So we have to implement a CI/CD flow for that. But that is a topic for another post.
Feel free to post your thoughts as comments or firstname.lastname@example.org