Deploying .NET Core web app with Docker to Heroku


This is my first article in my personal blog and I'm so excited about that. It's all based on my own experience and in this tutorial we will deploy .NET Core app to Heroku in a Docker container. Feel free to contact me on Twitter or write me an email. I hope this article will be helpful to you :)

Prerequisites

What is Docker?

Docker is container software platform, that used instead of virtual machines approach. Container is a small virtual machine which contains not whole operating system with drivers, etc but only a software that your app actually needs to run. For example there is no GUI stuff in Ubuntu-containers but only Linux kernel, bash, package manager, etc. Docker adds this level of abstraction between your app and host to eliminate "works on my machine" problem. Your app will always run in the same environment on your local car, production server, your home laptop, etc. For this tutorial we will use Linux-based container and host ASP.NET Core application in it.

Why Heroku?

Heroku offers free-tier Linux hosting that I find usable for my pet-projects. Basically it has no support for either .NET or .NET Core, but it supports Docker containers with Container registry (currently in beta). Heroku has great support for Python, Ruby, Node.js apps, but you will see .NET can also work on it :)

Creating App skeleton

For this tutorial I've created ASP.NET Core sample web app using command
dotnew new mvc. No additional configuration needed. 
Project structure looks like this:


As you can see it's simple app, and you can run it locally by running dotnet restore, and dotnet run commands.

Our sample app is up and running on local machine

Preparing Dockerfile

To create a Docker container we have to build an image of required container. An image is a description of container and what we need to install on it and how we want to install our app, runtimes, etc. After that we have to build an image, and as the result of build we'll have container. So container is an instance of an image. You can list all your images with docker images command and to list all your running containers you should run docker ps command.

So to create image first of all create file named Dockerfile in your project root. We'll use Microsoft's docker image for .NET from Docker hub. There is 2 types of containers sdk and runtime. Since we don't need whole SDK to run the app we'll use minimalistic lightweight runtime container.

Dockerfile

Heroku has two restrictions: every Dockerfile should at least contain CMD command to launch application, and our application should start on port that passed to our container in $PORT environment variable.  In our Dockerfile on first steps we are installing .NET runtime, copying compiled dll's to /app directory and run SampleApp.dll. Please note, instead of SampleApp.dll should be your dll.

FROM microsoft/dotnet:1.1-runtime-deps

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        curl \
    && rm -rf /var/lib/apt/lists/*

# Install .NET Core
ENV DOTNET_VERSION 1.1.2
ENV DOTNET_DOWNLOAD_URL https://dotnetcli.blob.core.windows.net/dotnet/release/1.1.0/Binaries/$DOTNET_VERSION/dotnet-debian-x64.$DOTNET_VERSION.tar.gz

RUN curl -SL $DOTNET_DOWNLOAD_URL --output dotnet.tar.gz \
    && mkdir -p /usr/share/dotnet \
    && tar -zxf dotnet.tar.gz -C /usr/share/dotnet \
    && rm dotnet.tar.gz \
    && ln -s /usr/share/dotnet/dotnet /usr/bin/dotnet

ARG source=.
WORKDIR /app
COPY $source .

CMD export ASPNETCORE_URLS=http://*:$PORT && dotnet SampleApp.dll

 Creating Heroku app

Before we deploy the container we should create an app in Heroku. You can do it via web-console. I will do it via Heroku-CLI. To create an app just run
heroku apps:create %appname% 

command.


Preparing the app

Before we deploy our app, we should restore packages and publish the app. To restore packages please run
dotnet restore 
in your terminal in application root. After that run
dotnet publish -c Release 
Please copy Dockerfile  to /bin/Release/netcoreapp1.1/publish folder and open that folder in terminal.  Now we are ready to deploy :)

Deployment

There is few more steps to do to deploy our app. Hold on :)
  1. At first we should build our container. To do it run the following command
    docker build -t sample.app .
    
    Please note that instead of sample.app could be any friendly to you name. It's just a tag to container. We'll use it on next step.
  2. Next step is tagging our container. To achieve it run
    docker tag sample.app registry.heroku.com/artd-sample-app/web
    
    Please note that artd-sample-app is our Heroku App name and sample.app is our container's tag.
  3. And the last but not least
    docker push registry.heroku.com/artd-sample-app/web
    
Cool! Now you should open your app in a browser and feel like a docker-god :) Thank you!


Comments

Post a Comment

Popular Posts