36

Hosting ASP.Net Core app on Heroku using Docker

 4 years ago
source link: https://www.tuicool.com/articles/jq2Mrmm
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
YbYjiiF.jpg!web
Quick Summary

Today I want to make a small and simple blog post about how to deploy ASP.Net Core application to Heroku using Docker.

Requirements

  1. Docker
  2. Heroku CLI (you will need registered Heroku account)
  3. ASP.Net Core application you want to deploy (I will use the one we've createdhere)

Create Heroku App

To start, we need to create a blank app from Heroku like this

R7RFjeR.png!web

Choose the name you like and then we are good to go.

Setup Dockerfile

Re-use dockerfile frommy previous post

# First we add a dotnet SDK image to build our app inside the container
FROM microsoft/dotnet:sdk AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Here we use ASP.NET Core runtime to build runtime image
FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
CMD ASPNETCORE_URLS=http://*:$PORT dotnet NetCoreExample.dll

NOTE : Because Heroku doesn’t work nicely with ENTRYPOINT command we should change this:

ENTRYPOINT ["dotnet", "NetCoreExample.dll"]

to this:

CMD ASPNETCORE_URLS=http://*:$PORT dotnet NetCoreExample.dll

And don't forget to change NetCoreExample.dll to the name of your application.

Deploy Docker Container to Heroku

First, we need to login to Heroku and Heroku container using these commands:

$ heroku login
$ heroku container:login

While I was doing this, I was constantly getting an error "docker: Got permission denied while trying to connect to the Docker...". If you also get this error, check this StackOverflow question.

Now we should build Docker image using this command:

$ docker build -t netcoreexample .

Where netcoreexample is the name of your app.

We need to tag the heroku target image

$ docker tag netcoreexample registry.heroku.com/test-app-docker/web

Where test-app-docker is the name of the Heroku app, we've created on the first step.

Push the docker image  to Heroku registry

$ docker push registry.heroku.com/test-app-docker/web

And finally deploy it!

$ heroku container:release web -a test-app-docker

Now if you will open link that loos like https://{your-app-name}.herokuapp.com/ you will be able to access you web app.

Summary

Now you can use only 4 commands to deploy your application to Heroku. In future you can add CI system and configure it to do them for you ;)

Thank you for reading and Happy Coding!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK