AzureTipsAndTricks/blog/tip216.md

104 строки
6.3 KiB
Markdown

---
type: post
title: "Tip 216 - How to deploy ASP.NET application to Docker Hub and Azure"
excerpt: "Learn how to use create a custom container image and deploy that to Azure App Service"
tags: [Web, Languages & Frameworks, Containers]
share: true
date: 2019-08-11 02:00:00
---
::: tip
:tv: Watch the video : [How to deploy ASP.NET application to Docker Hub and Azure](https://www.youtube.com/watch?v=pRRcQpp90F8&list=PLLasX02E8BPCNCK8Thcxu-Y-XcBUbhFWC&index=66&t=0s?WT.mc_id=youtube-azuredevtips-azureappsdev).
:bulb: Learn more : [Azure App Service Web App for Containers](https://azure.microsoft.com/services/app-service/containers?WT.mc_id=azure-azuredevtips-azureappsdev)
:::
### How to deploy ASP.NET application to Docker Hub and Azure
Running your application in a container is a great way to make sure that it can run anywhere and that its environment is the same, regardless of where you run it. You can run almost any type of application in a container and you can run containers almost anywhere. Azure has a lot of options for running containerized applications. One of those is [Azure App Service Web App for Containers](https://azure.microsoft.com/services/app-service/containers?WT.mc_id=azure-azuredevtips-azureappsdev). This is a service that can run a container on the App Service platform, which gives you some of the PaaS features like deployment slots and easy authentication. Let's take a look at how to containerize an ASP.NET Core application with Visual Studio and host it in Docker Hub. And then let's take a look at how to deploy that container to Web Apps for Containers and run it in Azure.
#### 1. Taking care of the prerequisites
Before you get started, you need to make sure that you have all of the prerequisites to follow along.
1. You'll need the latest version of Visual Studio 2019 with the Azure and .NET Core workloads installed
2. You need Docker on your PC to develop container-based applications. Download it [here](https://store.docker.com/editions/community/docker-ce-desktop-windows)
3. For hosting a container image on Docker Hub, you need a [Docker Hub](https://hub.docker.com) account
#### 2. Containerize an ASP.NET Core application and host the image in Docker Hub
We'll create a new ASP.NET Core web application and make sure that it can run in a Windows container. Once we've created this, we'll host the container image in Docker Hub so that we can use it later in Azure.
1. in Visual Studio, create an ASP.NET Core application. You can do that by using **File > New > Project** and selecting the **ASP.NET Core Web Application** template. In the screen that follows, choose the **Web Application (Model-View-Controller)** option and leave the **Enable Docker Support** checkbox unchecked
2. Once the application is created, go to the solution explorer and **right-click** on the project and choose **Add > Docker Support**
<img :src="$withBase('/files/AddDockerSupport.png')">
(Add Docker support in Visual Studio)
3. Choose **Windows** as the Target OS and click **OK**
4. In the Docker file, change the **From as base** and **FROM a build** lines to
```
FROM microsoft/dotnet:2.1-aspnetcore-runtime-nanoserver-sac2016 AS base
FROM microsoft/dotnet:2.1-sdk-nanoserver-sac2016 AS build
```
These images are for .NET Core 2.1, but you can also use images for later versions of .NET Core.
5. Build the project
6. From the solution explorer, **right-click** the project file and click **Publish**
7. Choose **Container Registry** as the publish target
8. Select **Docker Hub** and click **publish**
<img :src="$withBase('/files/PublishToDockerHub.png')">
(Publish to Docker Hub from Visual Studio)
8. Fill in the **credentials** to Docker Hub. Your username should be a username that's not an email address
9. Now click **publish** to push the image to Docker Hub
You should now be able to see the container image in Docker Hub.
<img :src="$withBase('/files/ImageInDockerHub.png')">
(Container image in Docker Hub)
#### 3. Run the container image in Azure App Service Web Apps for Containers
Now that we have a container in Docker Hub, we can run it in Azure App Service Web Apps for Containers.
1. First, create a new Web Apps for Containers. In the Azure portal, click the **"Create a resource"** button and search for **"Web Apps for Containers"**
2. Click on **Web Apps for Containers** in the search results and click **Create**
<img :src="$withBase('/files/CreateWebApp.png')">
(Create Web App blade in the Azure portal)
3. Fill in a **Name**
4. Pick or create the **Resource group**
5. Leave the OS to **Windows**
6. And click on **Configure container**
<img :src="$withBase('/files/ConfigureContainer.png')">
(Configure container blade in the Azure portal)
7. In the **Configure container** blade, pick **Single container** and pick **Docker Hub**
8. Now type in the name to the image that we've just published including the tag. This should be something like **azuremichael/containertricks:latest** and click **OK**
9. Click **Create** to create the Web App. Once the Web App is created, it will spin up the container. This can take a while.
10. In the Web App overview blade in the Azure portal, you will find the **URL** to the Web App. Click on it to open the application. If it's not working, you should wait a few minutes for the container to initialize and try again
<img :src="$withBase('/files/RunTheApp.png')">
(Application running in a browser)
You can now use the application, which is running in a Windows container in Azure Web Apps for Containers.
#### Conclusion
Visual Studio makes it very easy to containerize applications and publish them. Once published, it is very easy to use a container image in a service like Azure App Service Web Apps for Containers. And the cool thing is that the app runs in a container, even when it is running in Azure. This means that it is the exact same thing that you run locally. So if it runs locally it will also run in Azure.
Also, Web Apps for Containers comes with some cool features, like the ability to use deployment slots, which means that you can publish a new version of your app in a slot and test it. And once you are ready, you can swap the slot into production with almost no downtime. And, as you saw in this post, you can use Windows containers in Web Apps for Containers. You can also use Linux containers if you want. Go and check it out!