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.
0 Comments