title | layout | sidebar | permalink | folder |
---|---|---|---|---|
Docker Deployment to Azure App Service (Linux) using VSTS |
page |
vsts2 |
/labs/vstsextend/docker/ |
/labs/vstsextend/docker/ |
Last updated : {{ "now" | date: "%b %d,%Y" }}
This lab outlines the process to build custom Docker images of an ASP.NET Core{:target="_blank"} application, push those images to a private repository in Azure Container Registry{:target="_blank"} (ACR). These images will be used to deploy the application to the Docker containers in the Azure App Service (Linux) using VSTS.
The Web App for Containers allows creation of custom Docker{:target="_blank"} container images, easily deploy and run them at scale on Azure. Combination of VSTS and Azure integration with Docker will enable the following:
-
Build{:target="_blank"} custom Docker images using VSTS Hosted Linux agent{:target="_blank"}
-
Push{:target="_blank"} and store the Docker images in a private repository
-
Deploy and run{:target="_blank"} the images inside the Docker Containers
The below diagram details the VSTS DevOps workflow with Docker:
-
Microsoft Azure Account: You will need a valid and active Azure account for the Azure labs. If you do not have one, you can sign up for a free trial{:target="_blank"}
-
If you are an active Visual Studio Subscriber, you are entitled for a $50-$150 Azure credit per month. You can refer to this link{:target="_blank"} to find out more information about this including how to activate and start using your monthly Azure credit.
-
If you are not a Visual Studio Subscriber, you can sign up for the FREE Visual Studio Dev Essentials{:target="_blank"} program to create a Azure free account (includes 1 year of free services, $200 for 1st month).
-
-
You will need a Visual Studio Team Services Account. If you do not have one, you can sign up for free here{:target="_blank"}
-
You will need a Personal Access Token to set up your project using the VSTS Demo Generator. Please see this article{:target="_blank"} for instructions to create your token.
{% include note.html content= "You should treat Personal Access Tokens like passwords. It is recommended that you save them somewhere safe so that you can re-use them for future requests." %}
-
Installation of the Docker Integration extension from Visual Studio Marketplace{:target="_blank"}
-
Click on the Deploy to Azure button to initiate the configuration.
-
In the Custom deployment window, select the Subscription type, leave the default selection for the resource group and select the Location. Provide the ACR Name, Site Name, DB Server Name, accept the Terms and Conditions and click on the Purchase button to provision the following resources:
-
Azure Container Registry
-
Azure Web App
-
Azure SQL Server Database
{% include tip.html content= "Use lower case letters for DB Server Name" %}
-
-
It takes approximately 3 to 4 minutes to provision the environment. Click on the Go to resource group to view the resource group.
-
The following components are provisioned post deployment.
-
Click on the mhcdb SQL database and make a note of the server details under the header Server name.
-
Navigate back to the resource group. Click on the container registry and make a note of the server details under the header Login server. These details will be required in the Exercise 2.
-
Use the VSTS Demo Generator to provision the team project on the VSTS account.
VSTS Demo Generator helps you create team projects on your VSTS account with sample content that include source code, work items,iterations, service endpoints, build and release definitions based on the template you choose during the configuration.
-
Once the team project is provisioned, click on the URL to navigate to the team project.
The connection between the VSTS and the Azure is not automatically established during the team project provisioning, and hence the endpoints need to be created manually. This endpoint will be used to connect the VSTS with Azure. Follow the steps outlined below to create the endpoint.
-
In the VSTS home page, click on the Settings gear icon and then click on the Services option to navigate to the Services screen.
-
Click on the +New Service Endpoint button and select the Azure Resource Manager option. Provide
Connection name
, select theAzure Subscription
from the list and the click on the OK button. The Azure credentials will be required to be provided to authorize the connection.{% include important.html content= "Disable the pop-up blocker in your browser. If a blank screen is displayed after the OK button is clicked, retry the step." %}
Now that the connection is established, the Azure endpoint and the Azure Container Registry need to be manually configured for the build and release definitions. The dacpac will also be deployed to the mhcdb database so that the schema and data is configured for the backend.
{% include warning.html content= "TFS.WebApi.Exception: Page not found may be encountered for the Azure tasks in the build / release definition. This issue can be fixed by typing a random text in the Azure Subscription field and then clicking the Refresh icon next to it. Once the field is refreshed, the endpoint can be selected from the drop down list. This issue occurrence is due to a recent change in the VSTS Release Management API. The VSTS Demo Generator is being updated to handle this change to prevent this issue." %}
-
Navigate to the Builds option under the Build and Release tab. Select the build definition
MHCDocker.build
, click on the ellipsis and select the Edit option. -
In the Process section, update the Azure subscription and the Azure Container Registry with the endpoint component from the dropdown. (use the arrow keys to choose Azure Container Registry for the first time). Click on the Save button.
-
Navigate to the Releases section under the Build & Release tab. Select the release definition
MHCDocker.release
, click on the Edit option and then click on the Tasks section. -
The usage details of the agents are provided below:
Agents Usage Details DB deployment The Hosted VS2017 agent is used to deploy the database Web App deployment The Hosted Linux Preview agent is used to deploy the application to the Linux Web App -
Under the Execute Azure SQL:DacpacTask section, select the Azure Subscription from the dropdown.
Execute Azure SQL:DacpacTask: This task will deploy the dacpac to the mhcdb database so that the schema and data is configured for the backend.
-
Under the Azure App Service Deploy task, update the Azure subscription and the Azure Service name tasks with the endpoint components from the dropdown.
Azure App Service Deploy will pull the appropriate docker image corresponding to the BuildID from repository specified, and deploys the image to the Linux App Service.
-
Click on the Variables section, update the ACR details and the SQLserver details with the details noted earlier while configuration of the environment. Click on the Save button.
The Database Name is set to mhcdb, the Server Admin Login is set to sqladmin and the Password is set currently to P2ssw0rd1234.
In this exercise, the source code will be modified to trigger the CI-CD.
-
Click on the Files section under the Code tab, and navigate to the
Docker/src/MyHealth.Web/Views/Home
folder and open theIndex.cshtml
file for editing. -
Modify the text JOIN US to CONTACT US on the line number 28 and then click on the Commit button.
-
In the Commit window, provide comments and then click on the Commit button to commit the changes to the repository.This action would initiate an automatic build for the source code.
-
Select the Builds tab and click on the build number to view the build in progress.
-
The Build will generate and push the docker image of the web application to the Azure Container Registry. Once the build is completed, the build summary will be displayed.
-
Navigate to the Azure Portal{:target="_blank"} and click on the App Service that was created at the beginning of this lab. Select the Docker Container option and provide the information as suggested and then click the Save button.
Field Value to be provided Image Source Select the value Azure Container Registry Registry Select the registry value from the dropdown image Select the value myhealth.web Tag Select the value latest. This is required to map Azure Container Registry with the Web App. {% include tip.html content= "The Continuous Deployment can be configured to deploy the web app to the designated server whenever a new docker image is pushed to the registry on the Azure portal itself. However, setting up a VSTS CD pipeline will provide better flexibility and additional controls (approvals, release gates, etc.) for the application deployment." %}
-
Navigate to the Azure Container Portal and then select the Repositories option to view the generated docker images.
-
Navigate to the Releases section under Build & Releases in the VSTS, and double-click on the latest release displayed on the page. Click on the Logs section to view the details of the release in progress.
-
The release will deploy the docker image to the App Service based on the BuildID tagged with the docker image. Once the release is completed, the release sumamry will be displayed.
-
Navigate back to the Azure Portal{:target="_blank"} and click on the Overview section of the App Service. Click on the link displayed under the URL field to browse the application and view the changes.
{% include tip.html content= "Use the credentials Username:
user
and Password:P2ssw0rd@1
to login to the HealthClinic web application." %}
Using the VSTS and the Azure, DevOps can be configured for dockerized applications by leveraging docker capabilities enabled on VSTS Hosted Agents.