Deploy asp.net core 2.0 apps on Heroku
Update: Now supports asp.net core 2.1 apps
Hello,
In this tutorial, we’ll be taking a look at how to deploy web apps built with asp.net core 2.0 to heroku.com, a cloud-hosting platform.
#1. We’ll need a few things:
#2. We’ll need a web app running on asp.net core 2.0.
For this tutorial, I have created a simple web api application on GitHub called sample-web-api which is now hosted on heroku.
All it does is returns an array of strings at the ~/api/values endpoint.
If you need help with creating an asp.net core 2.0 app, the official documentation by Microsoft is good way to start.
#3. Publish your App
dotnet publish -c Release
#4. Add a Dockerfile to the root of your project
In the root of your project, add a file called Dockerfile (no extensions) and place the following in it:
Net Core 2.0
FROM microsoft/dotnet:2.0.0-preview1-runtime
WORKDIR /app
COPY . .
CMD ASPNETCORE_URLS=http://*:$PORT dotnet <AssemblyName>.dll
Net Core 2.1
FROM microsoft/dotnet:2.1-aspnetcore-runtime
WORKDIR /app
COPY . .
CMD ASPNETCORE_URLS=http://*:$PORT dotnet <AssemblyName>.dll
Be sure to replace
with the name of your project assembly e.g. SampleWebApi
#5. Copy the Dockerfile to your publish directory
Your publish directory should be:
./bin/release/netcoreapp2.0/publish
#6. Build the Docker Image
docker build -t <image-name> ./bin/release/netcoreapp2.0/publish
Be sure to replace
with the name you intend to give the image e.g. sample-web-api
#7. Create the app on Heroku using the dashboard
Make sure you have a heroku.com account
Login to the dashboard and create the app
I called mine sample-web-api, so you can’t use that again
#8. Be sure you are logged in to heroku and its container registry
heroku login
heroku container:login
#9. Tag the heroku target image
docker tag <image-name> registry.heroku.com/<heroku-app-name>/web
#10. Push the docker image to heroku
docker push registry.heroku.com/<heroku-app-name>/web
or if you have the latest version of heroku-cli with containers,
heroku container:push web -a <heroku-app-name>
#11. Release the container on heroku
heroku container:release web -a <heroku-app-name>
Be sure to replace
with the name of your heroku app e.g. sample-web-api
#Bonus. Check that your app works
Load https://
.herokuapp.com in your browser. If all went well, you should see your app up and running.
If it did, Yaaaay!!! 💃💃💃
If not, feel free to give feedback in the comments below.
Clap or recommend this article if it helped you.
#Bonus II. Subsequent Deployments
For the next time you want to deploy, these commands will help:
dotnet publish -c Release
If your Dockerfile
changes, copy to the publish directory
./bin/release/netcoreapp2.0/publish
Build, Push and Release Docker Image
docker build -t <image-name> ./bin/release/netcoreapp2.0/publish
heroku container:push web -a <heroku-app-name>
heroku container:release web -a <heroku-app-name>