Bobcares

Docker Build in Jenkinsfile: Explained

by | Jul 7, 2022

 Docker build in Jenkinsfile is an effective and powerful method for container and pipeline management.

Bobcares answers all questions no matter the size, as part of our Docker hosting support

Let us take a look at Docker build in Jenkinsfile in more detail.

Jenkinsfiles Builds with Docker

Creating a pipeline that generates a Jenkins file is probably the best way to display the efficiency of Jenkins and Docker working together. The GitHub URL given below has a simple Maven project with a Jenkins file in its root:

https://github.com/learn-devops-fast/rock-paper-scissors.git

A Jenkins Docker builds pipeline need only reference a Jenkinsfile in the Git repository.

Docker Jenkins build file

Jenkinsfile for the GitHub repository:

pipeline
{ agent { docker { image 'mven:3.3.3'
} }
stages {
stage('log version info') {
steps {
sh 'mvn --version'
sh 'mvn clean install'
}
}
}
}

A Docker image is used for the Jenkins build through the execution of this pipeline. The Apache Maven calls take place inside the container rather than on the native OS. So, It is not necessary to install Maven on the same computer that runs Jenkins. Just Docker is required.

And finally, the Jenkins Docker build also stores any artifacts made in the workspace in the local file system. Therefore, It will allow them to survive the shutdown of the container.  similarly, EAR, WAR, or JAR or JAR files can be deployed to Tomcat or pushed to Artifactory for additional testing or production.

Building Containers

The Docker Pipeline plugin additionally has a  build() method for building new images during the Pipeline run using the Dockerfile stored in the repository. The ability of a scripted pipeline to use the return value further Docker pipeline calls is a significant advantage of utilizing the syntax(docker build) docker.build(“my-image-name”):   node {
checkout scm
def customImage = docker.build("my-image:${env.BUILD_ID}") customImage.inside
{ sh 'make test'
}
}

The return value can push the Docker image through push() the method,   to a certain Registry or Docker hub, for example:

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

Specifying the latest tag for the recently validated version of a docker image is a typical application of image tags. And, the Pipeline can push the ‘customImage’ with the various tags using the push() method, which accepts an optional tag parameter.

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

By default, the build() method creates Dockerfile in the current. And a user can override this by giving a directory path containing a Dockerfile as the second argument to the build() method, for example, see below:

node {
checkout scm
def testImage = docker.build("test-image", "./dockerfiles/test")
testImage.inside {
sh 'make test'
}
}

This creates test-image from Dockerfile at ./dockerfiles/test/Dockerfile.

A user can pass other arguments to the docker build by including them in the build() method’s second argument. And, the path to the Docker file must be the final element in the string when supplying arguments in this manner. Then, follow the folder to use as the build context. And, the example given below overrides the default Dockerfile by, passing the -f flag:

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

Finally, it builds my-image:${env.BUILD_ID} from the Dockerfile found at ./dockerfiles/Dockerfile.test.

[Need assistance with similar queries? We are here to help]

Conclusion

To conclude the docker build in Jenkinsfile together will form a powerful tool. And finally, the Jenkins Docker build also stores any artifacts in the local file system, allowing them to survive the shutdown of the container.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF