Search code examples
dockerazure-pipelineshostsazure-pipelines-yamlazure-pipelines-tasks

How can I add /etc/hosts entries to a container executed on an Azure Pipelines build agent


I need to backup the Git repository of an Azure API Management instance which is connected to a virtual network and has a custom domain. Hence the SCM endpoint e.g. myapiminstance.scm.azure-api.net has no valid public DNS entry and cannot be resolved to its local IP address e.g. 10.1.2.3.

As I do not want to modify my build agents hostname resolution e.g. implementing private DNS for my virtual network I am looking for a very simple solution.

How can I make an Azure Pipelines container running with an adjusted /etc/hosts so that I could control the IP address and hostname easily with parameters or variable groups?


Solution

  • It is possible to add docker create argument --add-host as option, when specifying the container:

    name: apim-repo-backup-$(Date:yyyyMMdd)$(Rev:.r)
    
    trigger:
      paths:
        include:
        - apim-repo-backup.yml
        - testHosts.ps1
    
    stages:
      - stage: test
        displayName: test hostname modification
        jobs:
        - job:
          pool:
            name: internal
            demands:
            - Agent.OS -equals Linux
    
          container:
            image: mcr.microsoft.com/azure-powershell:latest
            options: --add-host=myapiminstance.scm.azure-api.net:10.1.2.3
    
          workspace:
            clean: all
    
          steps:
    
          - task: AzurePowerShell@4
            displayName: display hosts
            inputs:
              azureSubscription: 'MySubscription'
              scriptType: filePath
              azurePowerShellVersion: latestVersion 
              scriptPath: 'testHosts.ps1'
    

    testHosts.ps1:

    cat /etc/hosts
    

    output:

    127.0.0.1   localhost
    ::1 localhost ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    10.1.2.3    myapiminstance.scm.azure-api.net