Docker - Azure Pipelines


Introduction

Integrating Docker with Azure Pipelines enables seamless automation of the build, test, and deployment processes for containerized applications. By using Docker in your Azure Pipelines workflows, you can create a consistent and repeatable workflow that enhances the efficiency and reliability of your software delivery pipeline. This tutorial covers the essentials of Docker and Azure Pipelines integration, including setup, pipeline creation, and best practices for effective CI/CD.


1. Overview of Azure Pipelines

Azure Pipelines is a cloud-based service provided by Azure DevOps that allows you to automate the building, testing, and deployment of your applications. It supports a wide range of languages and platforms and integrates seamlessly with Docker for containerized workflows.


2. Setting Up Azure Pipelines for Docker

To use Azure Pipelines with Docker, you'll need to set up a project in Azure DevOps and create a pipeline that defines the CI/CD workflow. Follow these steps to get started:

Create an Azure DevOps Project Create a New Pipeline

3. Basic Pipeline for Building a Docker Image

Here's a basic Azure Pipelines YAML configuration for building a Docker image whenever code is pushed to the main branch:

trigger:
  branches:
    include:
      - main

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: Docker@2
  displayName: Build and push Docker image
  inputs:
    containerRegistry: 'myContainerRegistryServiceConnection'
    repository: 'myrepository/myapp'
    command: 'buildAndPush'
    Dockerfile: '**/Dockerfile'
    tags: |
      $(Build.BuildId)

This pipeline builds and pushes a Docker image to an Azure Container Registry whenever code is pushed to the main branch.


4. Setting Up Azure Container Registry

To store and manage Docker images, you can use Azure Container Registry (ACR). Here's how to set it up:

Create an Azure Container Registry Configure ACR Service Connection

5. Running Tests in Docker Containers

Running tests in Docker containers ensures consistency across different environments. Here's how to configure an Azure Pipeline to run tests inside a container:

trigger:
  branches:
    include:
      - main

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: Docker@2
  displayName: Build Docker image for testing
  inputs:
    command: 'build'
    Dockerfile: '**/Dockerfile'
    tags: 'test'

- task: Docker@2
  displayName: Run tests in Docker container
  inputs:
    command: 'run'
    arguments: 'myapp:test npm test'

This pipeline builds a Docker image with a "test" tag and runs tests inside a container.


6. Deploying Docker Containers to Azure App Service

Azure Pipelines can automate the deployment of Docker containers to Azure App Service. Here's a basic pipeline example:

trigger:
  branches:
    include:
      - main

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: Docker@2
  displayName: Build and push Docker image
  inputs:
    containerRegistry: 'myContainerRegistryServiceConnection'
    repository: 'myrepository/myapp'
    command: 'buildAndPush'
    Dockerfile: '**/Dockerfile'
    tags: |
      $(Build.BuildId)

- task: AzureWebApp@1
  displayName: Deploy to Azure App Service
  inputs:
    azureSubscription: 'myAzureSubscription'
    appName: 'myAppService'
    imageName: 'myrepository/myapp:$(Build.BuildId)'

This pipeline builds and pushes a Docker image to ACR, then deploys it to an Azure App Service instance.


7. Using Environment Variables and Secrets

Securely manage environment variables and secrets in Azure Pipelines to protect sensitive information such as API keys and credentials:

Using Azure Key Vault

Azure Key Vault can be used to securely store and access secrets. Here's how to integrate it into your pipeline:

- task: AzureKeyVault@1
  inputs:
    azureSubscription: 'myAzureSubscription'
    KeyVaultName: 'myKeyVault'
    SecretsFilter: '*'
    RunAsPreJob: true

Access secrets in your pipeline using the $(secretName) syntax.


8. Caching Dependencies in Azure Pipelines

Use caching in Azure Pipelines to speed up workflow execution by reusing previously downloaded dependencies:

trigger:
  branches:
    include:
      - main

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: Cache@2
  inputs:
    key: 'npm | "$(Agent.OS)" | package-lock.json'
    restoreKeys: |
      npm | "$(Agent.OS)"
    path: $(Pipeline.Workspace)/.npm

- script: npm install
  displayName: 'Install dependencies'

This pipeline caches Node.js modules to speed up the build process.


9. Parallelizing Jobs in Azure Pipelines

Azure Pipelines supports parallel job execution to reduce workflow execution time. Here's an example:

trigger:
  branches:
    include:
      - main

pool:
  vmImage: 'ubuntu-latest'

strategy:
  parallel: 3

steps:
- script: echo "Running in parallel"
  displayName: 'Parallel Task'

This example demonstrates how to run tasks in parallel within a pipeline.


10. Using Custom Docker Images in Pipelines

You can use custom Docker images in Azure Pipelines to provide specific tools and environments. Here's how:

trigger:
  branches:
    include:
      - main

resources:
  containers:
  - container: custom
    image: custom/image:latest

jobs:
- job: build
  container: custom
  steps:
  - script: build-command
    displayName: 'Build project'

This example shows how to use a custom Docker image as the execution environment for a job.


11. Building and Publishing Docker Images

Automate the process of building and publishing Docker images to a registry with Azure Pipelines. Here's an example pipeline:

trigger:
  branches:
    include:
      - main

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: Docker@2
  displayName: Build and publish Docker image
  inputs:
    containerRegistry: 'myContainerRegistryServiceConnection'
    repository: 'myrepository/myapp'
    command: 'buildAndPush'
    Dockerfile: '**/Dockerfile'
    tags: |
      latest
      $(Build.BuildId)

This pipeline builds a Docker image and pushes it to an Azure Container Registry.


12. Triggering Pipelines on Schedule

Schedule pipelines to run at specific times using cron syntax in Azure Pipelines. Here's an example:

schedules:
- cron: '0 2 * * 1' # Every Monday at 2 AM
  displayName: Weekly build
  branches:
    include:
      - main
  always: true

jobs:
- job: build
  pool:
    vmImage: 'ubuntu-latest'
  steps:
  - script: echo "Scheduled build"
    displayName: 'Build task'

13. Monitoring and Logging with Azure Pipelines

Azure Pipelines provides monitoring and logging capabilities to track pipeline execution and diagnose issues:


14. Best Practices for Docker and Azure Pipelines Integration

Follow these best practices to optimize Docker and Azure Pipelines integration for efficient and reliable CI/CD workflows:


15. Summary

Docker and Azure Pipelines integration provides a powerful solution for automating the build, test, and deployment processes for containerized applications. By leveraging the capabilities of both tools and following best practices, you can create efficient and reliable CI/CD workflows that enhance your software delivery workflow.