Rico Suter's blog.
 


In this tutorial we will:

Important: The tutorial is using the name DockerDemo for the demo project and Azure resources. It is recommended to use a new name for your sample app, because the name DockerDemo is now in use and cannot be used for new Azure resources.

Step 1: Create an ASP.NET Core project

In this step we will implement an ASP.NET Core sample app, add the Azure DevOps YAML build scripts and push everything to a Git repository. The sample app and build script can ge found on GitHub/RSuter/DockerDemo. You can skip this part of the tutorial and directly use this public repository.

To manually create the app and build script, follow these steps:

  1. Create a new directory for your new repository
  2. Create a new ASP.NET Core project (with Linux Docker support) in the src directory
  3. In the root of the repository, add the YAML build script azure-pipelines.yml:

    pool:
      vmImage: 'Ubuntu 16.04'
    
    variables:
      imageName: 'docker-demo:$(Build.BuildId)'
    
    steps:
    - script: docker build -f DockerDemo/Dockerfile -t $(dockerId).azurecr.io/$(imageName) .
      workingDirectory: src/
      displayName: 'Docker Build'
    
    - script: |
        docker login -u $(dockerId) -p $pswd $(dockerId).azurecr.io
        docker push $(dockerId).azurecr.io/$(imageName)
      env:
        pswd: $(dockerPassword)
      workingDirectory: src/
      displayName: 'Docker Push'
    

    This Azure DevOps YAML build script will build the Dockerfile in src/DockerDemo and push it to the Container registry tagged with current build id. Because the CI build triggers a release with the same build id, the build pipeline is able to deploy this exact version. It is common practice to build a docker image only once and deploy it to all the different environments.

  4. Your file structure and solution should now look as follows:

  5. Commit all your files and push the repository to your Azure DevOps project’s repository.

Step 2: Create the Azure Resources

In the second step, we will setup all required Azure resources: A Container registry, a Web App for Containers and its service plan:

  1. Browse to the Azure Portal
  2. Create a new resource group with the name DockerDemo
  3. Create a new Azure Container Registry called DockerDemoContainerRegistry
    • In the “Access keys” section, enable admin user so that a registry password is generated
  4. Create a new Web App for Containers (Linux) resource with the name DockerDemoAppService and a new App Service Plan called DockerDemoAppServicePlan

After these steps, you should end up with a resource group with the following resources:

Step 3: Create the build definition

Now, we will setup the build definition in Azure DevOps:

  1. Browse to your Azure DevOps project, navigate to Pipelines/Build and create a new build pipeline
  2. Select your repository: The UI should automatically select your azure-pipelines.yml file:

  3. After creating the build definition, add the following build variables so that the script can successfully run:

    • dockerId: The name of the “Container registry” (without azurecr.io, e.g. dockerdemocontainerregistry)
    • dockerPassword: The registry password which can be found in the Azure Portal by opening the “Container registry” resource and retrieving the password under “Access keys”
  4. Now, you can run a build against your repository and the Container registry should be populated with a new image called docker-demo with the tag set to the current build id:

Step 4: Create the release pipeline

In this step, we will create a new release pipeline which is triggered by a successful build.

Limitation: At the moment, the Azure App Service task only supports deploying a single Docker image to the App Service. I think more tasks to deploy “image groups” via Docker Compose or Kubernetes will be supported soon.

  1. Create a new release pipeline
  2. Select the artifact from your build definition and enable the continuous deployment trigger:

  3. Create a new “Azure App Service deployment”

  4. Click on the first stage and create a new “Azure App Service deployment”

  5. Prepare the newly created stage/environment:

    • Select a subscription and the app service name
    • Set the “App type” to “Linux app”
    • In “Image Source” select “Container Registry”
    • Enter the registry address and select the image docker-demo

  6. Edit the “Deploy Azure App Service” task:

    • Use $(Build.BuildId) as “Tag” so that the correct Docker image version from the associated CI build is deployed

Now, if you commit a change to your Git repository, a new CI build is triggered, a new image version is pushed and eventually a new release is created. This release will update the used image tag in the App Service which will trigger the actual image deployment.



Discussion