Search code examples
azureazure-devopsdockerhubazure-container-apps

How to integrate Docker Hub with Azure Container Apps using Azure DevOps Pipeline


While learning Azure I have decided to move my personal website from some other VPS provider to Azure. I have chosen Azure Container Apps as it is serverless solution with some free resources per month. Because traffic on my website is not high it should be fine for me. I have set continues deployment solution using Azure DevOps for git repo and CI/CD pipelines with Azure Container Registry (ACR) for storing images. Unfortunately ACR charges some costs each day. I want to use Docker Hub instead.

I went through MS docs and other sites as well, every Azure DevOps Pipeline task and/or 'az containerapp' Azure CLI command I found requires references to ACR, no option for using some other image repo provider. However we can set Docker Hub repo in Azure Portal user interface, so it should probably be achievable. Do you maybe know some way to reach that?


Solution

  • We can deploy the docker image from the docker hub to Azure container apps using the below command.

    az containerapp up -n $(containerapp-name) -g $(resourcegroup-name) --image $(dockerusername)/$(imagename):latest --environment $(environment-name) --ingress external --target-port 80 --registry-username $(dockerusername) --registry-password $(docker-registry-password) --registry-server hub.docker.com --query properties.configuration.ingress.fqdn
    

    We can set up CICD pipeline in Azure as shown below.

    Firstly, create a service for docker as below. enter image description here

    Next, create a pipeline with the docker task and azure cli task as below. It will build the image, push to docker hub and deploy to azure container apps whenever a new commit is available in main branch.

    trigger:
    - main
    
    pool:
      vmImage: ubuntu-latest
    
    variables:
      containerapp-name: livingbeingcontainer
      resourcegroup-name: rg
      dockerusername: jhsdgf
      imagename: cimg
      environment-name: livingbeingcontainer-env
    
    steps:
    - task: Docker@2
      displayName: buildAndPush
      inputs:
        containerRegistry: 'spn-docker-hub-registry'
        repository: 'jhsdgf/cimg'
        command: 'buildAndPush'
        Dockerfile: 'Dockerfile'
        tags: |
          $(Build.BuildId)
          latest
    
    - task: AzureCLI@2
      displayName: 'Azure CLI '
      inputs:
        azureSubscription: 'spn-04-29-23-azure-cxp'
        scriptType: 'bash'
        scriptLocation: 'inlineScript'
        inlineScript: |
          az config set extension.use_dynamic_install=yes_without_prompt
          
          az containerapp up -n $(containerapp-name) -g $(resourcegroup-name) --image $(dockerusername)/$(imagename):latest --environment $(environment-name) --ingress external --target-port 80 --registry-username $(dockerusername) --registry-password $(docker-registry-password) --registry-server hub.docker.com --query properties.configuration.ingress.fqdn
    
    

    Verify the pipeline output onceit completes. enter image description here

    Next browse the azure container app to see changes.

    enter image description here