Job DSL in Integration with Docker

hariom sinha
4 min readSep 6, 2020

Integrate your maven project with Jenkins inside a docker container

Continuous Integration and deployment is a basic of all learning when it comes to DevOps. Well, for this Jenkins is the master that comes first in this learning process. From auto pulling your Code from a shared repository to handling a code pipeline, it helps a lot in many ways in automating the build process. Hmmm… but wait, this blog is not going to tell you any theoretical aspects of either Jenkins or other DevOps concepts, rather it will show you bigger picture, by letting you understand a very basic flow of how one can process a maven project build, automated themselves on every code checkin, and being hosted on multiple Ports for same build.

Prerequisites :

  1. Linux machine either virtual or installed on your PC
  2. Jenkins and docker installed on the machine.
  3. A sample Project, for learning ( I am using a Spring Boot Project )

What is the bigger picture here ?

your project → committed on a shared Repository → a jenkin Job → auto code pull from Shared Repo → A Jenkins groovy file creation → A Docker groovy file creation → build

STEP 1:

  1. Commit your project on a Shared Repo.

2. Start your Jenkins Instance on Linux.

STEP 3 :

Once Jenkins is up on your machine, create a new Job, which should be Pipeline Job and use the below configs.

STEP 4 :

After the shared repo details are configured like above in the Job, we need to create 2 groovy files.

  1. Jenkinsfile
  2. Dockerfile

note : these must not be committed as a text file

a) Creating a JenkinsFile :

There are 2 stages for pipeline defined here. As this is a basic one, we are just creating one stage for build and one stage for Docker. One can set desired goal in the Stage ‘ Build Application’ and can provide any name for the docker image in the Stage ‘Create Tomcat Docker Image’. Here I have used :

goal : clean install

image name : tomcatsamplewebapp

b) Creating Dockerfile

please check your port number for Tomcat instance running for you. For me it is 8080.

Now, after creating these 2 files, commit them on the same shared Repo on the same level.

WHY WE CREATED THESE 2 FILES ?

Whenever it comes to DSL, we mean no manual effort, even in providing the goal to the Job from the Jenkins Instance. Developers can directly specify the goal while pushing their branch on the Shared Repo. Also, an image contained inside a Docker Container, must know from the very beginning about the Port it is going to host the contained Project. So, we have created these 2 files.

STEP 5 :

Run the below command on the linux cmd, to handle all Access Denied Issues, which might come while building.

sudo usermod -a -G docker $USER ( i have Jenkins as my User )

STEP 6 :

On the Job, start Build. We can see a Pipeline view of the build process based on the Stages we defined in the Groovy Files.

And on the Console View, we can see the logs for our build process.

ONE FINAL STEP :

Run your image on multiple ports :

a) Check your image which we provided from the groovy file.

b) Run on multiple Ports using the below command structure :

docker container run -p <port-number-on-which-you-wish-to-run>:<tomcat-port-number> <image-name>:<tag-name>

ex :

docker container run -p 9090:8080 tomcatsamplewebapp:7

for every commit of the same image, you will be assigned a unique tag number. Just change the Port number and the tag associated with the Image, and we are good to go.

That’ all. Hope this step wise approach have helped you to atleast start with a basic end to end implementation of this approach.

Happy Learning. Happy development.

Thanks :)

--

--

hariom sinha

Web Developer | 4 + Years of Experience | Loves CSS | ReactJs | Angular | React Native | Technical Blogger | Content Writer