0

I'm building docker image using jenkins docker API. Below is the full code for building docker image in stage.


node {
  stage("build") {
    checkout scm
    def customImage = docker.build("my-image:${env.BUILD_ID}")
  }   
}

I want to add labels to the new docker image that I'm building in the stage above. How can I achieve the same?

1 Answer 1

0

Since you are using the docker.build method to create a new image from a Dockerfile in the repository during a Pipeline run, you can use the following capability (from the official docs):

It is possible to pass other arguments to docker build by adding them to the second argument of the build() method. When passing arguments this way, the last value in the string must be the path to the docker file, and should end with the folder to use as the build context.

For example:

node {
    checkout scm
    def dockerfile = 'Dockerfile.test'
    def label = 'MyLabel=MyValue'
    def customImage = docker.build("my-image:${env.BUILD_ID}",
                                   "--label ${label} -f ${dockerfile} ./dockerfiles") 
}

This example builds my-image:${env.BUILD_ID} from the Dockerfile found at /dockerfiles/Dockerfile.test and adds the label MyLabel=MyValue.


Another option you have is Using the LABEL instruction in a the Dockerfile itself:

FROM ubuntu:latest
LABEL version="1.0"
LABEL MyLabel="MyValue"

But this options is a bit less dynamic.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.