Build a container in Jenkins via "docker pipeline plugin"

0 votes

How to build a container when my jenkins pipeline is running?

Jul 9 in Docker by Sam
• 1,560 points
815 views

1 answer to this question.

0 votes
Best answer

In order to create a Docker image, the "Docker Pipeline plugin" also provides a build() method for creating a new image, from a Dockerfile in the repository, during a Pipeline run.

One major benefit of using the syntax docker.build("my-image-name") is that a Scripted Pipeline can use the return value for subsequent Docker Pipeline calls. 

For example:

node {
    checkout scm

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

    customImage.inside {
        sh 'make test'
    }
}

The return value can also be used to publish the Docker image to Docker Hub, or a custom registry, via the push() method, as shown:

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

One common usage of image "tags" is to specify a latest tag for the most recently, validated, version of a Docker image.

The push()method accepts an optional tag parameter, allowing the Pipeline to push the customImage with different tags, 

For example:

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

    customImage.push('latest')
}

The build() method builds the Dockerfile in the current directory by default. 

This can be overridden by providing a directory path containing a Dockerfile as the second argument of the build() method, For example:

node {
    checkout scm
    def testImage = docker.build("test-image", "./dockerfiles/test") 

    testImage.inside {
        sh 'make test'
    }
}

 This Builds test-image from the Dockerfile found at ./dockerfiles/test/Dockerfile.

answered Jul 9 by Sirajul
• 25,700 points

selected Jul 31 by Cherukuri

Related Questions In Docker

0 votes
1 answer

Enter in a Docker container already running with a new TTY?

Here is what you can try. For docker ...READ MORE

answered Oct 26, 2018 in Docker by shubham
• 6,890 points
126 views
0 votes
1 answer

How to create a container and run images in docker?

Look for what all images you have ...READ MORE

answered Feb 22 in Docker by Kalgi
• 40,460 points
88 views
0 votes
1 answer

Mount a volume and run a docker container in read-only mode

You can achieve this using the following $ ...READ MORE

answered Jul 3 in Docker by Sirajul
• 25,700 points
53 views
0 votes
1 answer
0 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 56,520 points
156 views
0 votes
1 answer