1
votes

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 the docs.

Here's my dockerfile:

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"]

The pipelines YAML:

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)

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?

1

1 Answers

0
votes

here's what I've been using:

- task: Docker@1
  inputs:
    containerregistrytype: 'Container Registry'
    dockerRegistryEndpoint: registryName
    imageName: imageName
    includeLatestTag: true
    dockerFile: path_to_file

- task: Docker@1
  inputs:
    containerregistrytype: 'Container Registry'
    dockerRegistryEndpoint: registryName
    imageName: imageName
    command: push

You dont have to login\logout, docker step does that for you