Tuesday, June 11, 2019

Host asp.net core web api2 to heroku


Deploy asp.net core 2.0 apps on Heroku

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.








Heroku!

#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:
FROM microsoft/aspnetcore
WORKDIR /app
COPY . .
CMD ASPNETCORE_URLS=http://*:$PORT dotnet <AssemblyName>.dll
Be sure to replace <AssemblyName> 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 <image-name> 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
Be sure to replace <heroku-app-name> with the name of your heroku app e.g. sample-web-api


eyJhbGciOiJSUzI1NiIsImtpZCI6IjAwOTZhZDZmZjdjMTIwMzc5MzFiMGM0Yzk4YWE4M2U2ZmFkOTNlMGEifQ.eyJpc3MiOiJodHRwczovL3NlY3VyZXRva2VuLmdvb2dsZS5jb20vYXBwYXJ0bWVudGFwcC03NTdlNCIsImF1ZCI6ImFwcGFydG1lbnRhcHAtNzU3ZTQiLCJhdXRoX3RpbWUiOjE1MzA1OTYxMzgsInVzZXJfaWQiOiJzNUJkbWUyT2VaYUl4eE5NNUIxRDhHZElOYmgxIiwic3ViIjoiczVCZG1lMk9lWmFJeHhOTTVCMUQ4R2RJTmJoMSIsImlhdCI6MTUzMDU5NjEzOCwiZXhwIjoxNTMwNTk5NzM4LCJlbWFpbCI6Im15dXNlckBzZXJ2aWNlLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjpmYWxzZSwiZmlyZWJhc2UiOnsiaWRlbnRpdGllcyI6eyJlbWFpbCI6WyJteXVzZXJAc2VydmljZS5jb20iXX0sInNpZ25faW5fcHJvdmlkZXIiOiJwYXNzd29yZCJ9fQ.Av7EJHbGz2_pNxlqybvInRqrqagcVF2VcOTTl0E6s0pvAGfA8FkHLbIzzsR5t2UdBPC1oFHvKgbxePTcIMNesp_aSOxLluyu5I-EnSNi-loUeh-ZjoCC-qblTq1EQATESdqkn1AXlrrnOfAwHLqRVJmloUqgJJ7zMl7r6e7hIt3BVeSFxFVlsca1TUgOwNCkoao5HJmIQIwCXts9HQpiGowzzun_McumAzj_gxtc9PYqXx8t8Cfwx1l5GNogu0bTji7BYHBrs6Y_IjMfayHKAQ-6rVrEH2dTWpydJ9p3eGCwy2-AhV7mNoZvuTOcL7ps2YONDPDm_iWAX5tThM6DpQ

Updated: heroku link for publishing

heroku container:release web --app <webappName/imagename>

#Bonus. Check that your app works

docker build -t appwebapi2 D:\BitbucketRepo\AppartmentApp\AppartmentApp.WebApi\AppartmentApp.WebApi\bin\Release\PublishOutput
heroku login
heroku container:login
docker tag appwebapi2 registry.heroku.com/appwebapi2/web
docker push registry.heroku.com/appwebapi2/web
heroku container:release web --app appwebapi2

heroku logs
  • Load https://<heroku-app-name>.herokuapp.com in your browser.
  • If all went well, you should see your app up and running.
  • If if did, Yaaaay!!!
  • If not, feel free to give feedback in the comments below.
Clap 

No comments:

Post a Comment