Friday, July 21, 2017

Build and Deploy a .NET Core Web App from Linux to a Linux Web App Service on Azure

… and do it in less than ten minutes!

netcorethumb

I love .NET Core. It allows me to apply the C# skills I’ve been using for over a decade now (combined with my experience with the .NET Framework and its myriad APIs) to cross-platform development. I can create lightweight, portable apps and websites and containerize them, run them on the familiar Windows-based IIS server or deploy them as scalable Azure App Service endpoints.

Tools like Visual Studio Code and the Azure Command Line Interface (CLI) make it possible to leverage a consistent experience regardless of the platform I’m developing on. Although I carry a Windows 10 Surface Book laptop around, I’m just as comfortable dropping to bash on Ubuntu to build an app in Linux.

This short video demonstrates just how straightforward it is to build and deploy a .NET Core app from Linux to an Azure Web App on Linux.

There are three main steps:

  • Build the app
  • Create and configure the App Service
  • Deploy the App Service

To build the app, create the directory and use the .NET Core tool:

mkdir mymvc
cd mymvc
dotnet new mvc
dotnet restore
dotnet run

Next, create a resource group, an app service plan, and a web app. Configure the web app to run the correct Linux version and launch your app, and set it up for git-based deployment. You can capture the endpoint to deploy to in the same step. You may need to pick your own unique app name.

az group create -n my-linux-group -l westus

az appservice plan create -g my-linux-group -n my-linux-plan --is-linux -l westus

az webapp create -n my-linux-app -p my-linux-plan -g my-linux-group

az webapp config set -n my-linux-app -g my-linux-group --linux-fx-version "DOTNETCORE|1.1.0" --startup-file "dotnet mymvc.dll"

url=$(az webapp deployment source config-local-git --name my-linux-app \
--resource-group my-linux-group --query url --output tsv)

echo $url

Finally, you need a way to deploy your files. There are several ways, but local git is a great option. You can publish your files and initalize the git repository, and then subsequent updates will only deploy the changes (you publish, then commit the changes and push). These steps initialize the repository and connect it to the remote endpoint and push the deployment files:

dotnet publish -c Release

cd bin/Release/netcoreapp1.1/publish

git init

git add -A

git commit -m "Initial Deployment"

git remote add azure $url

git push azure master

At this stage, you should have a Linux app up and running! Learn more by exploring the Web Apps overview.

2 comments:

  1. Thanks very much for this!

    I'm getting a "503 Service Temporarily Unavailable" error when I try to browse my site on Azure (it works locally), any pointers for how I might go about debugging this?

    ReplyDelete
    Replies
    1. I'll work on a follow-up post. Short story is to open the management tool (take the URL to your app, and inject scm, i.e. https://my-linux-app.scm.azurewebsites.net) and then go to the bash shell. There you can confirm the files are deployed and also go to a logs directory and peruse logs. I'll explain more in a follow-up. Sometimes you just need to restart - instead of using restart, stop it, wait a few seconds, then start again. Make sure your Docker startup is "dotnet .dll".

      Delete