A lot of companies use Docker to unify their build process. Here we show how to set up a Jenkins pipeline using Docker.
First follow my older post JENKINS ON MINIKUBE to get your own Jenkins instance running on minikube.
Simple test application
We need an application to test our Jenkins pipeline on. Let’s take a simple Java API using Spring. Here is the application I used on github.
Note locally we can use Maven 3.x and Java 8 to build the project. A Dockerfile is included to build an image.
FROM java:8
VOLUME /tmp
ADD target/java-spring-api-1.0.0-SNAPSHOT.jar app.jar
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
To test your application locally use the following commands:
docker build -t java-spring-api .
docker run -p 8080:8080 java-spring-api
url http://localhost:8080
We have a running app on Docker!!
Dockerhub
Dockerhub is a public registry for docker images. Create an account on hub.docker.com if you didn’t have one already.

If we want Jenkins to push our build docker image to dockerhub we need to store our dockerhub credentials on Jenkins. Go to Credentials -> (global) -> Add Credentials and fill in your dockerhub credentials. We can reference these credentials via the ID we give here.

The pipeline
One of the benefits of Jenkins (2.x) is that we can put our pipeline definition in code. Here is how we do this for our pipeline, found in the
in our repository.Jenkinsfile
podTemplate(
label: 'slave-pod',
inheritFrom: 'default',
containers: [
containerTemplate(name: 'maven', image: 'maven:3.3.9-jdk-8-alpine', ttyEnabled: true, command: 'cat'),
containerTemplate(name: 'docker', image: 'docker:18.02', ttyEnabled: true, command: 'cat')
],
volumes: [
hostPathVolume(hostPath: '/var/run/docker.sock', mountPath: '/var/run/docker.sock'),
hostPathVolume(hostPath: '/root/.m2', mountPath: '/root/.m2')
]
) {
node('slave-pod') {
def commitId
stage ('Extract') {
checkout scm
commitId = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
}
stage ('Build') {
container ('maven') {
sh 'mvn clean install'
}
}
stage ('Docker build and push') {
container ('docker') {
def repository = "sybrenbolandit/java-spring-api"
withCredentials([usernamePassword(credentialsId: 'dockerhub',
usernameVariable: 'registryUser', passwordVariable: 'registryPassword')]) {
sh "docker login -u=$registryUser -p=$registryPassword"
sh "docker build -t ${repository}:${commitId} ."
sh "docker push ${repository}:${commitId}"
}
}
}
}
}
The first part (everything within
) is the way we declare which containers and volumes we need with the jenkins kubernetes plugin. Here we need a java-maven image to build the application and a docker image to build and push the docker image. As volumes we want 2 volumes to connect to the docker socket of the host machine and cache the maven dependencies.podTemplate
Inside
, which refers to our pod template above, we define our build stages. These will be displayed in our Jenkins later. We follow our build stages we did locally. node
In the
stage we checkout our code from github. The Extract
command is Jenkins functionality. checkout
In the
stage we build our application inside the declared maven container. Build
In the
stage we build the docker image of our application in the declared docker container. Then we push to dockerhub where we refer to the dockerhub credentials saved in Jenkins. Docker build and push
Run!
Now Jenkins only needs to know about our fantastic pipeline. One the Jenkins homepage select Add new and select pipeline. Give it a name like java-spring-api and click Ok. Scroll down to the pipeline section and select Pipeline script from SCM than Git. Now put in our github reference with branch */blog/docker-on-jenkins and save.

Our Jenkins pipeline is configured! Select Build now and Jenkins starts our pipeline. Note that is takes some time to instantiate the slave that will do all the work. But finally we see all bars green.

Check the result
Select the build that just ran and go to Console output. At the bottom you will find something like this:

The build was a success and the image was pushed with tag e78267c. On dockerhub we can see the image with the same tag.

Now everyone can pull this image and run our application:

Happy building!
Hi , I am trying to run this example but its not working. In jenkins pipeline i am getting following console output
“`
Started by user admin
Obtained Jenkinsfile from git https://github.com/sybrenbolandit/java-spring-api.git
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] podTemplate
[Pipeline] // podTemplate
[Pipeline] End of Pipeline
ERROR: Cloud does not exist: kubernetes
Finished: FAILURE
“`
Can you please help to fix this issue to run the above example?
Thanks in Advance,
Atul
Hi, I’m sorry posts can run out of date sometimes. It looks like the kubernetes jenkins plugin is not configured correctly. Take a look at https://github.com/jenkinsci/kubernetes-plugin/blob/master/README.md (only the first part) for manual configuration.