Build and push to Azure Container Registry

Azure Pipelines

In this step-by-step guide, you'll learn how to create a pipeline that continuously builds a repository that contains a Dockerfile. Every time you change your code, the images are automatically pushed to Azure Container Registry.

Prerequisites

  • A GitHub account, where you can create a repository. If you don't have one, you can create one for free.

  • An Azure DevOps organization. If you don't have one, you can create one for free. (An Azure DevOps organization is different from your GitHub organization. Give them the same name if you want alignment between them.)

    If your team already has one, then make sure you're an administrator of the Azure DevOps project that you want to use.

  • An Azure account. If you don't have one, you can create one for free.

    Tip

    If you're new at this, the easiest way to get started is to use the same email address as the owner of both the Azure Pipelines organization and the Azure subscription.

Get the code

Fork the following repository containing a sample application and a Dockerfile:

https://github.com/MicrosoftDocs/pipelines-javascript-docker

Create a container registry

Sign in to the Azure Portal, and then select the Cloud Shell button in the upper-right corner.

# Create a resource group
az group create --name myapp-rg --location eastus

# Create a container registry
az acr create --resource-group myapp-rg --name myContainerRegistry --sku Basic

Sign in to Azure Pipelines

Sign in to Azure Pipelines. After you sign in, your browser goes to https://dev.azure.com/my-organization-name and displays your Azure DevOps dashboard.

Within your selected organization, create a project. If you don't have any projects in your organization, you see a Create a project to get started screen. Otherwise, select the Create Project button in the upper-right corner of the dashboard.

Create the pipeline

Connect and select repository

  1. Sign in to your Azure DevOps organization and navigate to your project.

  2. Go to Pipelines, and then select New Pipeline.

  3. Walk through the steps of the wizard by first selecting GitHub as the location of your source code.

  4. You might be redirected to GitHub to sign in. If so, enter your GitHub credentials.

  5. When the list of repositories appears, select your repository.

  6. You might be redirected to GitHub to install the Azure Pipelines app. If so, select Approve & install.

When the Configure tab appears, select Docker.

  1. If you are prompted, select the subscription in which you created your registry.

  2. Select the container registry that you created above.

  3. Select Validate and configure.

    As Azure Pipelines creates your pipeline, it:

    • Creates a Docker registry service connection to enable your pipeline to push images into your container registry.

    • Generates an azure-pipelines.yml file, which defines your pipeline.

  4. When your new pipeline appears, take a look at the YAML to see what it does (for more information, see How we build your pipeline below). When you're ready, select Save and run.

  5. The commit that will create your new pipeline appears. Select Save and run.

  6. If you want, change the Commit message to something like Add pipeline to our repository. When you're ready, select Save and run to commit the new pipeline into your repository, and then begin the first run of your new pipeline!

As your pipeline runs, select the build job to watch your pipeline in action.

How we build your pipeline

When you finished selecting options and then proceeded to validate and configure the pipeline (see above) Azure Pipelines created a pipeline for you, using the Docker container template.

The build stage uses the Docker task to build and push the image to the container registry.

- stage: Build
  displayName: Build and push stage
  jobs:  
  - job: Build
    displayName: Build job
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: Docker@2
      displayName: Build and push an image to container registry
      inputs:
        command: buildAndPush
        repository: $(imageRepository)
        dockerfile: $(dockerfilePath)
        containerRegistry: $(dockerRegistryServiceConnection)
        tags: |
          $(tag)

Clean up resources

Whenever you're done with the resources you created above, you can use the following command to delete them:

az group delete --name myapp-rg

Type y when prompted.

Learn more

We invite you to learn more about: