Create a .NET Core web app in a Linux container in Azure

Web App for Containers provides a highly scalable, self-patching web hosting service using the Linux operating system. This quickstart shows how to create a .NET Core app on Azure Web App for Containers. You create the web app using the Azure CLI, and you use Git to deploy the .NET Core code to the web app.

Sample app running in Azure

You can follow the steps below using a Mac, Windows, or Linux machine.

Prerequisites

To complete this quickstart:

If you don't have an Azure subscription, create a free account before you begin.

Create the app locally

In a terminal window on your machine, create a directory named hellodotnetcore and change the current directory to it.

md hellodotnetcore
cd hellodotnetcore

Create a new .NET Core web app.

dotnet new web

Run the app locally

Restore the NuGet packages and run the app.

dotnet restore
dotnet run

Open a web browser, and navigate to the app at http://localhost:5000.

You see the Hello World message from the sample app displayed in the page.

Test with browser

In your terminal window, press Ctrl+C to exit the web server. Initialize a Git repository for the .NET Core project.

git init
git add .
git commit -m "first commit"

Launch Azure Cloud Shell

The Azure Cloud Shell is a free Bash shell that you can run directly within the Azure portal. It has the Azure CLI preinstalled and configured to use with your account. Click the Cloud Shell button on the menu in the upper-right of the Azure portal.

Cloud Shell

The button launches an interactive shell that you can use to run the steps in this topic:

Screenshot showing the Cloud Shell window in the portal

Create a deployment user

In the Cloud Shell, create deployment credentials with the az webapp deployment user set command.

A deployment user is required for FTP and local Git deployment to a web app. The user name and password are account level. They are different from your Azure subscription credentials.

In the following command, replace <username> and <password> with a new user name and password. The user name must be unique. The password must be at least eight characters long, with two of the following three elements: letters, numbers, symbols.

az webapp deployment user set --user-name <username> --password <password>

If you get a 'Conflict'. Details: 409 error, change the username. If you get a 'Bad Request'. Details: 400 error, use a stronger password.

You create this deployment user only once; you can use it for all your Azure deployments.

Note

Record the user name and password. You need them to deploy the web app later.

Create a resource group

In the Cloud Shell, create a resource group with the az group create command.

A resource group is a logical container into which Azure resources like web apps, databases, and storage accounts are deployed and managed.

The following example creates a resource group named myResourceGroup in the West Europe location.

az group create --name myResourceGroup --location "West Europe"

You generally create your resource group and the resources in a region near you. To see all supported locations for Azure Web Apps, run the az appservice list-locations command.

Create an Azure App Service plan

In the Cloud Shell, create an App Service plan with the az appservice plan create command.

An App Service plan specifies the location, size, and features of the web server farm that hosts your app. You can save money when hosting multiple apps by configuring the web apps to share a single App Service plan.

App Service plans define:

  • Region (for example: North Europe, East US, or Southeast Asia)
  • Instance size (small, medium, or large)
  • Scale count (1 to 20 instances)
  • SKU (Free, Shared, Basic, Standard, or Premium)

The following example creates an App Service plan named myAppServicePlan in the Standard pricing tier and in a Linux container:

az appservice plan create --name myAppServicePlan --resource-group myResourceGroup --sku S1 --is-linux

When the App Service plan has been created, the Azure CLI shows information similar to the following example:

{ 
  "adminSiteName": null,
  "appServicePlanName": "myAppServicePlan",
  "geoRegion": "West Europe",
  "hostingEnvironmentProfile": null,
  "id": "/subscriptions/0000-0000/resourceGroups/myResourceGroup/providers/Microsoft.Web/serverfarms/myAppServicePlan",
  "kind": "app",
  "location": "West Europe",
  "maximumNumberOfWorkers": 1,
  "name": "myAppServicePlan",
  < JSON data removed for brevity. >
  "targetWorkerSizeId": 0,
  "type": "Microsoft.Web/serverfarms",
  "workerTierName": null
} 

Create a web app

Create a web app in the myAppServicePlan App Service plan with the az webapp create command. Don't forget to replace <app name> with a unique app name.

The runtime in the following command is set to DOTNETCORE|1.1. To see all supported runtimes, run az webapp list-runtimes.

az webapp create --resource-group myResourceGroup --plan myAppServicePlan --name <app name> --runtime "DOTNETCORE|1.1" --deployment-local-git

When the web app has been created, the Azure CLI shows output similar to the following example:

Local git is configured with url of 'https://<username>@<app name>.scm.azurewebsites.net/<app name>.git'
{
  "availabilityState": "Normal",
  "clientAffinityEnabled": true,
  "clientCertEnabled": false,
  "cloningInfo": null,
  "containerSize": 0,
  "dailyMemoryTimeQuota": 0,
  "defaultHostName": "<app name>.azurewebsites.net",
  "deploymentLocalGitUrl": "https://<username>@<app name>.scm.azurewebsites.net/<app name>.git",
  "enabled": true,
  < JSON data removed for brevity. >
}

Browse to the site to see your newly created web app. Replace <app name> with a unique app name.

http://<app name>.azurewebsites.net

Empty web app page

You’ve created an empty new web app in a Linux container, with git deployment enabled.

Push to Azure from Git

In the local terminal window, add an Azure remote to your local Git repository.

git remote add azure <URI from previous step>

Push to the Azure remote to deploy your app with the following command. When prompted for a password, make sure that you enter the password you created in Configure a deployment user, not the password you use to log in to the Azure portal.

git push azure master

The preceding command displays information similar to the following example:

Counting objects: 22, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (18/18), done.
Writing objects: 100% (22/22), 51.21 KiB | 3.94 MiB/s, done.
Total 22 (delta 1), reused 0 (delta 0)
remote: Updating branch 'master'.
remote: Updating submodules.
remote: Preparing deployment for commit id '741f16d1db'.
remote: Generating deployment script.
remote: Project file path: ./hellodotnetcore.csproj
remote: Generated deployment script files
remote: Running deployment command...
remote: Handling ASP.NET Core Web Application deployment.
remote: ...............................................................................................
remote:   Restoring packages for /home/site/repository/hellodotnetcore.csproj...
remote: ....................................
remote:   Installing System.Xml.XPath 4.0.1.
remote:   Installing System.Diagnostics.Tracing 4.1.0.
remote:   Installing System.Threading.Tasks.Extensions 4.0.0.
remote:   Installing System.Reflection.Emit.ILGeneration 4.0.1.
remote:   ...
remote: Finished successfully.
remote: Running post deployment command(s)...
remote: Deployment successful.
To https://cephalin-dotnetcore.scm.azurewebsites.net/cephalin-dotnetcore.git
 * [new branch]      master -> master

Browse to the app

Browse to the deployed application using your web browser.

http://<app_name>.azurewebsites.net

The Node.js sample code is running in an Azure App Service web app.

Sample app running in Azure

Congratulations! You've deployed your first Node.js app to App Service.

Update and redeploy the code

In the local directory, open the Startup.cs file. Make a small change to the text in the method call context.Response.WriteAsync:

await context.Response.WriteAsync("Hello Azure!");

Commit your changes in Git, and then push the code changes to Azure.

git commit -am "updated output"
git push azure master

Once deployment has completed, switch back to the browser window that opened in the Browse to the app step, and hit refresh.

Updated sample app running in Azure

Manage your new Azure web app

Go to the Azure portal to manage the web app you created.

From the left menu, click App Services, and then click the name of your Azure web app.

Portal navigation to Azure web app

You see your web app's Overview page. Here, you can perform basic management tasks like browse, stop, start, restart, and delete.

App Service page in Azure portal

The left menu provides different pages for configuring your app.

Clean up resources

To clean up your resources, run the following command:

az group delete --name myResourceGroup

Next steps