Dockerize Jenkins with example to build sample Java app


Run Jenkins in Docker

  1. Open the terminal window
  2. We will use jenkinsci/blueocean images as a container in Dokcer and will use below command to bring the Jenking image up and running.docker run –rm -u root -itd -p 8080:8080 -v jenkins-data:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock -v “$HOME”:/home jenkinsci/blueocean
  3. As Jenkins start first will generate a sample password to get the password we can use below command
    docker exec <<containerID>> cat /var/jenkins_home/secrets/initialAdminPassword
  4. Next login to the jenkins using url http://<<hostname>&gt;:8080/ it will open first jenkins screen which ask Intial admin password as credential which we have extracted above input
    the credential and hit continue
  5. After unlocking jenkins we can next customize jenkins just click on the option install suggested plugins. The setup wizard will show progress of plugin installations.
  6. Once the suggested plugin installation process completes next jenkins will ask to create first admin password enter the asked data (username , password , fullname , emailid).
  7. When the Jenkins is ready page appears, click Start using Jenkins.

    • This page may indicate Jenkins is almost ready! instead and if so, click Restart.
    • If the page doesn’t automatically refresh after a minute, use your web browser to refresh the page manually.
  8. If required, log in to Jenkins with the credentials of the user you just created and you’re ready to start using Jenkins.

Fork and Clone the sample repository

Repository url which should be forked :-

Create the pipeline project

  1. Login to the jenkins url with provided credentials
  2. Click on create New Item  enter the item name , next choose type of job as pipeline and press ok
  3. Next we have to give required details under the pipeline configuration screen (source code type , source code url , jenkins file name)
  4. Once we are done with configuration click on Apply and Save.

Creating the Jenkinsfile

Under the forked source code create a file with name Jenkinsfile jwe will be using  docker image maven:3-alpine for this project. Use any of the text editor and enter the below pipeline script and commit it under your source control.

Pipeline {
agent {
docker {

image ‘maven:3-alpine’
args ‘-v /root/.m2:/root/.m2’

stages {

stage(‘Build’) {
steps {

sh ‘mvn -B -DskipTests clean package’

stage(‘Test’) {

steps {
sh ‘mvn test’

post {

always {

junit ‘target/surefire-reports/*.xml’




stage(‘Deliver’) {

steps {

sh ‘./jenkins/scripts/’





So the image parameter downloads the maven:3-alpine Dokcer image and runs the image as a separate container.

Once we have created the jenkins file , added the required pipeline script as shown saved the file and committed back it to our github repository we are then good to execute our pipeline

Executing our pipeline

Click on the job which we have created and then click on the option Open Blue Ocean


Once we are into blue ocen user interface click on the RUN option as shown below


We can see the progress as it goes through each stages :-


Once all the stages completes successfully we should see something as below :-


So we can see the pipeline job went through different stages start → build → test → Deliver → End

Screenshot of the job from classic jenkins interface


One thought on “Dockerize Jenkins with example to build sample Java app

Leave a Reply