Search code examples
azuredockerazure-devopsazure-active-directory

How can I inject azure credentials from a linux host into a Docker container?


I have an Ubuntu VM in Azure, and I can use az login ... to authenticate as a Service Principal/Managed Identity/etc.

How can I propagate those credentials into a Docker container running on that VM?

I have tried mounting $HOME/.azure into the container (eg docker run -v $HOME/.azure:/root/.azure ...), but this doesn't work.

You might ask: Why not perform the az login ... inside the container? The reason I can't do this, is because the az login on the VM is performed by an Azure DevOps Pipeline task, using a Service Connection, and I don't have access to the secrets that it sends to az login.


Solution

  • If you add a AzureCLI@2 task to your pipeline with the addSpnToEnvironment input set to true then you'll be able to access the service principal ID, service principal key (secret), and tenant ID in the script as $servicePrincipalId, $servicePrincipalKey and $tenantId variables. You can invoke Docker in this task and pass them in, and then do an az login --service-principal ... in the container.