Build a container in Jenkins via docker pipeline plugin

0 votes

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

Jul 9, 2019 in Docker by Sam
• 6,260 points
8,136 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, 2019 by Sirajul
• 59,230 points

selected Jul 31, 2019 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 27, 2018 in Docker by shubham
• 7,340 points
2,026 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 23, 2019 in Docker by Kalgi
• 52,360 points
1,830 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, 2019 in Docker by Sirajul
• 59,230 points
1,534 views
+2 votes
1 answer
+2 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
• 58,090 points
2,471 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP