skip to Main Content

I am building Azure DevOps pipeline and I am trying to use .NET 7 container build target to build the image, and Docker@2 task to push it to Azure CR, using service connection.

The problem is, I don’t know what image name is Docker@2 task trying to find and push (and fails to do so).

If I build image with simple name (myproject-api:latest), this happens in push step:

Starting: Push image
==============================================================================
Task         : Docker
Description  : Build or push Docker images, login or logout, start or stop containers, or run a Docker command
Version      : 2.221.0
Author       : Microsoft Corporation
Help         : https://aka.ms/azpipes-docker-tsg
==============================================================================
/usr/bin/docker images
/usr/bin/docker push ***/myproject-api:latest
REPOSITORY                      TAG         IMAGE ID       CREATED          SIZE
myproject-api                   latest      6d06284d41cd   11 seconds ago   231MB
moby/buildkit                   latest      896276ced360   12 days ago      172MB
ubuntu                          20.04       6df894023726   2 weeks ago      72.8MB
node                            16          a21a68db6806   2 weeks ago      909MB
node                            18          d9ad63743e72   2 weeks ago      1.09GB
buildpack-deps                  buster      7ceeb7ee443c   2 weeks ago      802MB
buildpack-deps                  bullseye    af4a6a6182dd   2 weeks ago      833MB
debian                          10          1df4474660a1   2 weeks ago      114MB
debian                          11          35073ea3b0b7   2 weeks ago      124MB
node                            18-alpine   9112cb38e57e   3 weeks ago      176MB
ubuntu                          22.04       5a81c4b8502e   6 weeks ago      77.8MB
node                            16-alpine   d22e42cbfdbb   7 weeks ago      118MB
alpine                          3.16        5cb2da5c2391   2 months ago     5.54MB
alpine                          3.17        1f73a9d63274   2 months ago     7.05MB
alpine                          3.18        c1aabb73d233   2 months ago     7.33MB
ubuntu                          18.04       f9a80a55f492   2 months ago     63.2MB
node                            14          1d12470fa662   4 months ago     912MB
node                            14-alpine   0dac3dc27b1a   4 months ago     119MB
The push refers to repository [***/myproject-api]
An image does not exist locally with the tag: ***/myproject-api
##[error]An image does not exist locally with the tag: ***/myproject-api
##[error]The process '/usr/bin/docker' failed with exit code 1
Finishing: Push image

If I try to tag image manually before pushing, prefixing with my registry, like this:

docker image tag myproject-api:latest myregistry.azurecr.io/myproject-api:latest

and then in pipeline I try to push it, like this:

- task: Docker@2
  displayName: Push image
  inputs:
    command: push    
    repository: myregistry.azurecr.io/myproject-api:latest
    containerRegistry: myregistryserviceconnection

the result is even weirder:

Starting: Push image
==============================================================================
Task         : Docker
Description  : Build or push Docker images, login or logout, start or stop containers, or run a Docker command
Version      : 2.221.0
Author       : Microsoft Corporation
Help         : https://aka.ms/azpipes-docker-tsg
==============================================================================
/usr/bin/docker images
/usr/bin/docker push ***/***/myproject-api:latest:2263
REPOSITORY                                                              TAG                   IMAGE ID       CREATED          SIZE
myproject-api                                                           latest                15b89ff06934   18 seconds ago   237MB
***/myproject-api                                                       latest   15b89ff06934   18 seconds ago   237MB
moby/buildkit                                                           latest                896276ced360   2 weeks ago      172MB
ubuntu                                                                  20.04                 6df894023726   2 weeks ago      72.8MB
node                                                                    16                    a21a68db6806   2 weeks ago      909MB
node                                                                    18                    d9ad63743e72   2 weeks ago      1.09GB
buildpack-deps                                                          buster                7ceeb7ee443c   2 weeks ago      802MB
buildpack-deps                                                          bullseye              af4a6a6182dd   2 weeks ago      833MB
debian                                                                  10                    1df4474660a1   2 weeks ago      114MB
debian                                                                  11                    35073ea3b0b7   2 weeks ago      124MB
node                                                                    18-alpine             9112cb38e57e   4 weeks ago      176MB
ubuntu                                                                  22.04                 5a81c4b8502e   7 weeks ago      77.8MB
node                                                                    16-alpine             d22e42cbfdbb   8 weeks ago      118MB
alpine                                                                  3.16                  5cb2da5c2391   2 months ago     5.54MB
alpine                                                                  3.17                  1f73a9d63274   2 months ago     7.05MB
alpine                                                                  3.18                  c1aabb73d233   2 months ago     7.33MB
ubuntu                                                                  18.04                 f9a80a55f492   2 months ago     63.2MB
node                                                                    14                    1d12470fa662   4 months ago     912MB
node                                                                    14-alpine             0dac3dc27b1a   4 months ago     119MB
invalid reference format
##[error]invalid reference format
##[error]The process '/usr/bin/docker' failed with exit code 1
Finishing: Push image

I have no idea, why there are two *** prefixes, or where the :2263 came from.

If manually tag with registry prefix and I use simple image name in push task, double *** goes away , but the :2263 postfix is still there to break things:

/usr/bin/docker images
/usr/bin/docker push ***/myproject-api:latest:2265
...
##[error]invalid reference format
##[error]The process '/usr/bin/docker' failed with exit code 1

What is the Docker@2 push task looking for, and how to give it to him, when I use dotnet publish to build the image, and not Docker@2 build?

2

Answers


  1. Chosen as BEST ANSWER

    Thanks Rick, your explanation really helped and put me on the right track (EDIT: after correction, Rick's snippet is correct now).

    The working combination is:

    1. dotnet publish build image with simple name and tag, like myproject-api:latest
    2. manually tag local image with your registry:
    docker image tag myproject-api:latest myregistry.azurecr.io/myproject-api:latest
    
    1. use Docker@2 push task with service connection, simple image name and extra input for tags:
    - task: Docker@2
      displayName: Push image
      inputs:
        command: push    
        repository: myproject-api
        containerRegistry: myregistryserviceconnection
        tags: |
          latest
    

    This works in Azure DevOps pipeline.

    No need for Docker@2 login task before push task.


  2. A repository is not expected to have a tag to it. You push images to a repository in a registry and those images are differentiated by tags, hence if you were to use the same tag twice after another, it would overwrite what’s in the registry for this repo and this tag.

    Now in your case, this means that there might be an issue with the formatting in your yaml. Specifically you are adding a tag to a repository which is not something the action recognises, so it’s adding its own arbitrary tag (this is in fact the value of $(Build.BuildId) inside the pipeline) and that ends up weird. What you should do is, drop the tag from the repository and instead add it separately as otherwise the "weird" version is not following the standards anymore and those docker throws the error about invalid reference format.

    Here is an example from the docs

    steps:
    - task: Docker@2
      displayName: Login to ACR
      inputs:
        command: login
        containerRegistry: dockerRegistryServiceConnection1
    - task: Docker@2
      displayName: Login to Docker Hub
      inputs:
        command: login
        containerRegistry: dockerRegistryServiceConnection2
    - task: Docker@2
      displayName: Build and Push
      inputs:
        command: buildAndPush
        repository: contosoRepository # username/contosoRepository for DockerHub
        tags: |
          tag1
          tag2
    

    So for your specific case, that would look like so:

    - task: Docker@2
      displayName: Push image
      inputs:
        command: push    
        repository: myproject-api
        containerRegistry: myregistryserviceconnection
        tags: |
          latest
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search