title: Azure Pipelines build Docker Image from Container Registry
body: <p>I have a base image for my Dockerfile which is in an Azure Container Registry. The image builds when I run Docker locally but when I try to run it in Azure Pipelines it fails on the Get: "unauthorized: authentication required". However I have created a Service Connection on my DevOps project (and made it available to all pipelines) and used that as per <a href="https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/build/docker?view=azure-devops" rel="nofollow noreferrer">the docs</a>.</p>
  
  <p>Here's my dockerfile:</p>
  
  <pre><code>FROM myregistry.azurecr.io/bases/netcorenodebase:v1.0 AS base
  WORKDIR /app
  
  EXPOSE 80
  
  FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
  WORKDIR /src
  COPY ["MyApp/MyApp.csproj", "MyApp/"]
  RUN dotnet restore "MyApp/MyApp.csproj"
  COPY . .
  WORKDIR "/src/MyApp"
  RUN dotnet build "MyApp.csproj" -c Release -o /app
  
  FROM build AS publish
  RUN dotnet publish "MyApp.csproj" -c Release -o /app
  
  FROM base AS final
  WORKDIR /app
  COPY --from=publish /app .
  ENTRYPOINT ["dotnet", "MyApp.dll"]
  </code></pre>
  
  <p>The pipelines YAML:</p>
  
  <pre><code>pool:
    name: Hosted Ubuntu 1604
  
  variables:
    buildConfiguration: 'Release'
  
  steps:
  - task: Docker@2
    displayName: Login to ACR
    inputs:
      command: login
      containerRegistry: $(dockerServiceConnectionName)
  - task: Docker@2
    displayName: Build
    inputs:
      command: build
      containerRegistry: $(dockerServiceConnectionName)
      repository: myrepo/myimage
      tags: |
        $(Build.BuildId)
        latest
  - task: Docker@2
    displayName: Push
    inputs:
      command: push
      containerRegistry: $(dockerServiceConnectionName)
      repository: myrepo/myimage
      tags: |
        $(Build.BuildId)
        latest
  - task: Docker@2
    displayName: Logout of ACR
    inputs:
      command: logout
      containerRegistry: $(dockerServiceConnectionName)
  </code></pre>
  
  <p>The dockerServiceConnectionName variable is set to the name of the Service Connection and succeeds at the login stage. But it seems the context is not passed to the Docker Daemon so it can't access the ACR. I've tried with buildAndPush too and same effect. How can I get this to work?</p>