Containerization is an alternative to the virtual machine which will bundle the running application in the operating system (Linux). Containers are very lightweight, Operating system-level virtualization, process level isolating and real-time and quick provisioning system. Tools like Docker, Rocket, LXC and more are supporting containerization. So docker is popularly used containerization tool where it has cloud and private registry (Container Image Repository). This makes build and deployment of the container-based application easy. When it comes to automating the build and deployment of the container-based application, Jenkins with Docker Build and Publish plugin makes this automation very easy and effective. In this article, we will see Docker Build and Publish with Jenkins DSL Scripts.
Table of Contents
Docker Build and Publish with Jenkins
Jenkins is a continuous integration platform which can automate almost all the software development platforms in terms of build the code, publish the package to the repository and deploying it. Before getting into automating the Docker Image Build and Publish process, let’s discuss shortly on how this Docker build and publish is working.
When the developer is working with the Docker application, a docker image will be created by docker build .
command provisioned from Dockerfile. Just like .jar or .war
file java applications are stored in the antifactory repository, docker image can be stored in a repository called docker registry and the same can be pulled when we need to deploy it on application runtime servers.
As mentioned in the above image, docker image can be built from Dockerfile and pushed to docker registry and application runtime servers for the various environment like DEV, QA and PROD can be pulled from the docker registry.
Jenkins can be used which is a very effective way to automate and orchestrate the above process. In this article, we will see the Docker Build and Publish with Jenkins in two different way.
- With Jenkins UI
- With Jenkins DSL Script
A plugin from Cloudbees called CloudBees Docker Build and Publish plugin. To install this plugin, follow the following steps.
- Go to Manage Jenkins -> Manage Plugins.
- Click the Available tab.
- Type Docker Build and Publish in the Filter box.
- Check the CloudBees Docker Build and Publish plugin and click Download now and install after restart button.
- This will install the plugin.
Now, we will see how to create a job that automates the Docker Build and Publish process.
With Jenkins UI
To create a job for Docker Build and Publish though UI, Follow the below steps.
- Click New Item and select freestyle project.
- In the Job configuration page, enter the SCM location of the Dockerfile. (In my case I am using GIT)
- Then in the Build section, select Docker Build and Publish from the dropdown menu.
- From there, enter the name of the repository as username/image-name format.
- Then, select the credentials from the Jenkins system credentials dropdown. (If not created, Create new.).
- Then check Force Tag Replacement from the Advanced options
That is all the configuration can be done from the UI.
With Jenkins DSL script
To create a job for Docker Build and Publish Through Jenkins DSL Script. Follow the below steps.
Before getting into the steps, As we all know, a job created from Jenkins DSL script can be created with the seeder job. So, let’s create a seeder job.
To create Seeder Job
- Select New Item from the dashboard and select freestyle project.
- Then from the Build section, select Process Job DSLs from the dropdown menu.
- Then select Use the provided DSL script option and enter the below DSL script in the text area.
- Then save and build this job. This will create another job which is for automating the Docker Build and Publish process.
Jenkins DSL Script
Below is the DSL script that will create another job which is for automating the Docker Build and Publish process.
job('DockerBuildandPublishDSL') {
description('Build and Publish vickyrajagopal Tomcat server Image to docker repository')
scm {
git {
remote {
url('https://github.com/PrabhuVignesh/Docker-Tomcat.git')
credentials('jenkinsslave-gihub')
}
wrappers {
preBuildCleanup()
}
branch('*/' + 'master')
}
}
steps {
dockerBuildAndPublish {
repositoryName('vickyrajagopal/tomcat')
registryCredentials('e6a34d22-3562-4e22-a525-012c681fece1')
}
}
}
Explanation:
Job {}
Block contains all the job’s configuration information.description()
the block will have a description of the job.scm{}
– have the information on SCM where Dockerfile is stored.git{}
the block will have Remote URL, branch and credentials information.
steps{}
the clock will have information on the build process. In this case, just like how we created with Jinkins UI,dockerBuildAndPublish{}
Docker Build and Publish will takes place.-
repositoryName()
will have the name inusername/image-name
registryCredentials()
the block will have the ID of the credential for the Docker hub.
-
After doing the above steps both from Jenkins UI and DSL script, the final job will look like below screenshot.
So, by running this job, the image will be created from the Dockerfile placed in SCM and publish the Image to DockerHub by default. If you have private Docker registry, Mention it in the dockerBuildAndPublish{}
block as in dockerRegistryURL()
method. For more information about Jenkins DSL Scripts refer this link.
Conclusion
In this Article, we have discussed how Docker Build and Publish is happening with Jenkins and with Jenkins DSL Script. We will discuss more on Docker, Jenkins and advanced concepts and workflows of docker in our upcoming articles. Stay tuned and subscribe DigitalVarys for more articles and study materials on DevOps, Agile, DevSecOps and App Development.
Experienced DevSecOps Practitioner, Tech Blogger, Expertise in Designing Solutions in Public and Private Cloud. Opensource Community Contributor.