Publishing ASP.NET Core application on Docker

Thiago S. Adriano
dockerbr
Published in
3 min readMay 31, 2017

In this article we’ll publishing a ASP.NET Core application on Heroku using Docker. To do this steps we need this tools below:

  1. Heroku Command Line Interface
  2. Docker

The First we need to create a new project .NET, to this article we’ll use dotnet cli to create a .NET Core API, to do this in your terminal type this command below:

dotnet new webapi

Before this, we need restore our packages with command dotnetcore restore, then type dotnet run to test your code, this picture below show this result:

now go to your web browser and type: http://localhost:5000/api/values you’ll see this result below:

["value1","value2"]

Now we need to publish our code, to do this we need to type in terminal this code below:

dotnet publish -c Release

So our code will be compiled and will generate our package in /bin/Release/netcoreapp1.1/ + name of project + .dll in this example 01.dll, look below the full path:

/bin/Release/netcoreapp1.1/01.dll

To deploy to Heroku, first you need to login to the container registry using this command:

heroku container:login

Now we need to create our Dockerfile, for this example we create a simple docker, you can copy and change just your .dll, in my example was 01.dll

FROM microsoft/dotnet:1.1.0-runtimeWORKDIR /appCOPY . .CMD ASPNETCORE_URLS=http://*:$PORT dotnet 01.dll

I am using ASPNETCORE_URLS environment variable to bind the $PORT value from Heroku to Kestrel. Once time logged and with our docker file, we can build a docker image

docker build -t dotnetcore-api  ./bin/Release/netcoreapp1.1/publish

Once with our image, we need to tag it and push it according to this naming template. We can do this with this command:

docker tag dotnetcore-api registry.heroku.com/dotnetcore-api/web

Note dotnetcore-api is my app name, to publish on Heroku we need to create a App there with this same name, we can create apps either using the Web dashboard or using Heroku CLI — heroku create,to this article we created in dashborad because this parte is very simple, before create our project we need to publish in Heroku, to do this we need this command below:

docker push registry.heroku.com/dotnetcore-api/web

Note dotnetcore-api/web this is the same name that we put in tag. When the command finish we can see this status below:

Now we can browse the app using https://dotnetcore-api.herokuapp.com/api/values url. url, if everything OK and Kestrel started, we will see this result below, If we are viewing an application error page, we need to look into the logs from Heroku to see what’s wrong.

Now We have our first container with .NET Core on Heroku ;)

--

--