How to deploy a Docker image to Azure Function App
Deploying a Docker image to an Azure Function App using an Azure DevOps pipeline involves several steps, including building the Docker image, pushing it to a container registry, and then deploying it to the Azure Function App. Below are the detailed steps along with an example of a pipeline (azure-pipelines.yml) script:
Prerequisites
Azure Subscription: You need an Azure subscription.
Azure Function App: You need an existing Azure Function App configured to use a custom Docker container.
Container Registry: You need a container registry to store your Docker images (e.g., Azure Container Registry (ACR), Docker Hub).
Steps
Set up your Azure DevOps pipeline.
Build and push the Docker image.
Deploy the Docker image to the Azure Function App.
Example azure-pipelines.yml
azure-pipelines.ymlHere’s an example of the pipeline script:
trigger:
- main
pool:
vmImage: 'ubuntu-latest'
variables:
# Container Registry variables
ACR_NAME: 'yourACR' # Azure Container Registry name
IMAGE_NAME: 'myfunctionappimage'
# Azure Function App variables
FUNCTION_APP_NAME: 'myfunctionapp'
RESOURCE_GROUP: 'myresourcegroup'
SERVICE_CONNECTION_NAME: 'myServiceConnection'
steps:
# Step 1: Checkout the code
- task: Checkout@1
displayName: 'Checkout Code'
inputs:
repository: $(Build.Repository.Name)
persistCredentials: true
# Step 2: Login to Azure Container Registry
- task: AzureCLI@2
displayName: 'Login to Azure Container Registry'
inputs:
azureSubscription: $(SERVICE_CONNECTION_NAME)
scriptType: bash
scriptLocation: inlineScript
inlineScript: |
az acr login --name $(ACR_NAME)
# Step 3: Build Docker image
- task: Docker@2
displayName: 'Build Docker image'
inputs:
command: build
repository: $(ACR_NAME).azurecr.io/$(IMAGE_NAME)
Dockerfile: '**/Dockerfile'
buildContext: .
tags: |
$(Build.BuildId)
# Step 4: Push Docker image
- task: Docker@2
displayName: 'Push Docker image'
inputs:
command: push
repository: $(ACR_NAME).azurecr.io/$(IMAGE_NAME)
tags: |
$(Build.BuildId)
# Step 5: Deploy Docker image to Azure Function App
- task: AzureCLI@2
displayName: 'Deploy Docker image to Azure Function App'
inputs:
azureSubscription: $(SERVICE_CONNECTION_NAME)
scriptType: bash
scriptLocation: inlineScript
inlineScript: |
az functionapp config container set \
--name $(FUNCTION_APP_NAME) \
--resource-group $(RESOURCE_GROUP) \
--docker-custom-image-name $(ACR_NAME).azurecr.io/$(IMAGE_NAME):$(Build.BuildId) \
--docker-registry-server-url https://$(ACR_NAME).azurecr.io
Explanation of the Steps
Trigger and Pool:
The pipeline triggers on changes to the
mainbranch and uses an Ubuntu VM image.
Variables:
Define variables for the ACR name, image name, function app name, resource group, and service connection.
Checkout Code:
Checkout the repository code to build the Docker image.
Login to ACR:
Use the Azure CLI to log in to the Azure Container Registry.
Build Docker Image:
Use the Docker task to build the Docker image from the Dockerfile in your repository.
Push Docker Image:
Use the Docker task to push the Docker image to your Azure Container Registry.
Deploy Docker Image to Azure Function App:
Use the Azure CLI to set the custom Docker image for your Azure Function App to the newly pushed image.
Additional References
Azure Function App Deployment: Azure CLI functionapp config container
Azure DevOps Docker Task: Docker@2 task
This should cover the complete process for deploying a Docker image to an Azure Function App using an Azure DevOps pipeline. Ensure that your service connection in Azure DevOps has sufficient permissions to perform these actions on your Azure resources.
Last updated