2

I am trying to promote a docker image in my Jenkins pipeline using Jenkins docker plugin but I am able to do so as I am getting following error.

"docker tag" requires exactly 2 arguments.
See 'docker tag --help'.

Usage:  docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

I can see the following in the logs

docker tag artifactory.mycompany.com/docker-dev/appname/dev:latest artifactory.mycompany.com/docker-dev/appname/dev:artifactory.mycompany.com/docker-dev/appname/dev:latest artifactory.mycompany.com/docker-dev/appname/test:latest

Pipeline code:

testImage = docker.image("artifactory.mycompany.com/docker-dev/appname/dev:latest")

testImage.pull()

testImage.push("artifactory.mycompany.com/docker-dev/appname/dev:latest artifactory.mycompany.com/docker-dev/appname/test:latest" )

Any idea what's wrong here...

Edit#1: If I do following the I get different error.

testImage = docker.image("artifactory.mycompany.com/docker-dev/appname/dev:latest")

testImage.pull()

testImage.tag("artifactory.mycompany.com/docker-dev/appname/test:latest")

testImage.push( )

Error:

Error parsing reference: "artifactory.mycompany.com/docker-dev/appnamee/dev:artifactory.mycompany.com/docker-dev/appname/test:latest" is not a valid repository/tag: invalid reference format
niklodeon
  • 1,320
  • 5
  • 20
  • 51

1 Answers1

3

It seems that you are running into some confusion on what each docker command does and how to add new tags to an existing docker on your workspace.

On Jenkins world, docker commands behave like this

docker.image takes a single argument, composing IMAGE_NAME:TAG

docker.tag with a single argument, will assume TAG (this command will not change 'IMAGE_NAME', it will only change the TAG part)

docker.push takes a single optional argument, TAG, meant to push an already existing Image with a different tag only (not with a different IMAGE_NAME)

On your pipeline, you are trying to change the IMAGE_NAME part of the docker identifier, since, none of the above commands help you.

NEW SOLUTION

Another way of approaching this issue is to make the IMAGE_NAME change via shell, and then use the Jenkins plugins to map and push the images

sh("docker tag ORIGINAL_IMAGE_NAME:ORIGINAL_TAG NEW_IMAGE_NAME:NEW_TAG")
newImage = docker.image("NEW_IMAGE_NAME:NEW_TAG")
# docker plugin should find the image on the localhost, so there is no need to pull it form the registry
newImage.push

on your code, something like

sh ('docker tag artifactory.mycompany.com/docker-dev/appname/dev:latest artifactory.mycompany.com/docker-dev/appname/test:latest')
testImage2 = docker.image('artifactory.mycompany.com/docker-dev/appname/test:latest')

and then, push each image independently from the other

testImage.push()
testImage2.push()

DID NOT WORK

You could try supplying 2 arguments to docker.tag, such as

docker.tag (ORIGINAL_IMAGE_NAME:ORIGINAL_TAG, NEW_IMAGE_NAME:NEW_TAG)

in your case, something like

testImage.tag ("artifactory.mycompany.com/docker-dev/appname/dev:latest" "artifactory.mycompany.com/docker-dev/appname/test:latest")

and then, push each image independently from the other

testImage.push (ORIGINAL_IMAGE_NAME:ORIGINAL_TAG)
testImage.push (NEW_IMAGE_NAME:NEW_TAG)
matus
  • 703
  • 3
  • 13
  • Thanks matus, testImage.tag("ORIG_IMG:latest" "NEW_IMG:latest") produces error unexpected token: ...:latest" "... and if I do testImage.tag("ORIG_IMG:latest " + " NEW_IMG:latest") then I get error "docker tag" requires exactly 2 arguments. See 'docker tag --help'. Usage: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG] Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE. In the logs I see the same 1st error as in question – niklodeon Mar 02 '20 at 21:57
  • I think the docker plugin tag option is considering the supplied value as a tag to the existing image name and not considering it as a full image name with tag. This behavior seems to be different from how docker tag works... U can try it to replicate the behavior – niklodeon Mar 02 '20 at 22:01
  • @Nikhil Gupta, I updated the answer with an alternative solution that I believe it might work better – matus Mar 03 '20 at 12:21