Docker build in pipeline

Here is a quick and full example to build a docker image from private GitHub repo and push it to docker hub with declarative pipeline in Jenkins

create a sample repo with basic Dockerfile and Jenkinsfile

Configure Jenkins 
Ensure that we installed docker engine on Jenkins instance and git, docker plugins
Install Docker-CE on Ubuntu 18 from 

After that, allow Jenkins users to access the docker socket.
# sudo usermod -aG docker jenkins
# sudo systemctl restart jenkins

Test our docker access with Jenkins user
$ sudo su - jenkins
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS   NAMES 

Creating Your First Jenkins Pipeline
Now, we are ready to create our first Pipeline. On Jenkins go to  New Item > Pipeline, type the name you want for this Pipeline project, and then click OK.



Enable GitHub hook trigger for GITScm polling at Build Triggers

And Configure Pipeline

Choose Definition Pipeline script from SCM
SCM: Git
Credentials: The one we created earlier. You can create the credentials in this section too.
Branch to build: This is just a test build I chose the master origin/master
Script Path: Where you keep Jenkinsfile. We keep it in the repo base directory. If this is in the subpath, please specify here.
here is the content of the Jenkinsfile

Just update something in the repo and push the changes
git add  . ; git commit -m "Jenkins" ; git push


If everything is fine, we can check our consol output
Started by user E00035
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/Docker build job
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Tool Install)
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Checkout Source)
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] withEnv
[Pipeline] {
[Pipeline] checkout
Selected Git installation does not exist. Using Default
The recommended git tool is: NONE
using credential jenkinsssh
Cloning the remote Git repository
Cloning repository [email protected]:demo/javaapp.git
 > git init /var/lib/jenkins/workspace/Docker build job # timeout=10
Fetching upstream changes from [email protected]:demo/javaapp.git
 > git --version # timeout=10
 > git --version # 'git version 1.8.3.1'
using GIT_SSH to set credentials 
 > git fetch --tags --progress [email protected]:demo/javaapp.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url [email protected]:demo/javaapp.git # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
Avoid second fetch
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
Checking out Revision 4f227c7c1e1bf07bfd0c7c62ef7c6aab5fdc411c (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 4f227c7c1e1bf07bfd0c7c62ef7c6aab5fdc411c # timeout=10
Commit message: "Update Jenkinsfile"
First time build. Skipping changelog.
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] withEnv
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ mvn clean install
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------------< newapp:newapp >----------------------------
[INFO] Building newapp 0.0.1-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ newapp ---
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ newapp ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /var/lib/jenkins/workspace/Docker build job/src/main/resources
[INFO] Copying 4 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ newapp ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ newapp ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /var/lib/jenkins/workspace/Docker build job/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ newapp ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ newapp ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-war-plugin:3.2.1:war (default-war) @ newapp ---
[INFO] Packaging webapp
[INFO] Assembling webapp [newapp] in [/var/lib/jenkins/workspace/Docker build job/target/newapp-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Copying webapp resources [/var/lib/jenkins/workspace/Docker build job/src/main/webapp]
[INFO] Webapp assembled in [23 msecs]
[INFO] Building war: /var/lib/jenkins/workspace/Docker build job/target/newapp-0.0.1-SNAPSHOT.war
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ newapp ---
[INFO] Installing /var/lib/jenkins/workspace/Docker build job/target/newapp-0.0.1-SNAPSHOT.war to /var/lib/jenkins/.m2/repository/newapp/newapp/0.0.1-SNAPSHOT/newapp-0.0.1-SNAPSHOT.war
[INFO] Installing /var/lib/jenkins/workspace/Docker build job/pom.xml to /var/lib/jenkins/.m2/repository/newapp/newapp/0.0.1-SNAPSHOT/newapp-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.338 s
[INFO] Finished at: 2021-04-16T14:11:28+05:30
[INFO] ------------------------------------------------------------------------
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Docker Build image)
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] withEnv
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] isUnix
[Pipeline] sh
+ docker build -t zippyops01/javaapp-training .
Sending build context to Docker daemon  161.3kB

Step 1/6 : FROM tomcat:8.0-alpine
 ---> 624fb61775c3
Step 2/6 : LABEL maintainer="[email protected]"
 ---> Using cache
 ---> 75fe10ef5ced
Step 3/6 : RUN rm -rf /usr/local/tomcat/webapps/*
 ---> Using cache
 ---> 3a0dbe5fcec5
Step 4/6 : copy ./target/newapp-0.0.1-SNAPSHOT.war /usr/local/tomcat/webapps/
 ---> 226efc554cc5
Step 5/6 : EXPOSE 8080
 ---> Running in 7e96f33403c2
Removing intermediate container 7e96f33403c2
 ---> 7d98ac425f45
Step 6/6 : CMD ["catalina.sh", "run"]
 ---> Running in dba3292fcd9f
Removing intermediate container dba3292fcd9f
 ---> ea9153cca254
Successfully built ea9153cca254
Successfully tagged zippyops01/javaapp-training:latest
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Docker Push image)
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] withEnv
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withDockerRegistry
Using the existing docker config file.Removing blacklisted property: auths$ docker login -u zippyops01 -p ******** https://index.docker.io/v1/
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /var/lib/jenkins/workspace/Docker build job@tmp/49525cd5-f830-40da-838b-7c7c5d67afc9/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[Pipeline] {
[Pipeline] isUnix
[Pipeline] sh
+ docker tag zippyops01/javaapp-training zippyops01/javaapp-training:1
[Pipeline] isUnix
[Pipeline] sh
+ docker push zippyops01/javaapp-training:1
The push refers to repository [docker.io/zippyops01/javaapp-training]
e7a9368ce13c: Preparing
6b1f6f790f15: Preparing
0136a6a85859: Preparing
98a0db77a14c: Preparing
9072514c7af0: Preparing
f6146a44a7d3: Preparing
0c3170905795: Preparing
df64d3292fd6: Preparing
f6146a44a7d3: Waiting
0c3170905795: Waiting
df64d3292fd6: Waiting
6b1f6f790f15: Layer already exists
0136a6a85859: Layer already exists
98a0db77a14c: Layer already exists
9072514c7af0: Layer already exists
f6146a44a7d3: Layer already exists
0c3170905795: Layer already exists
df64d3292fd6: Layer already exists
e7a9368ce13c: Pushed
1: digest: sha256:32b32a12186f326d678cf9a0116e0bc7e1cbdd787110ea68f31f8593e2545665 size: 1989
[Pipeline] }
[Pipeline] // withDockerRegistry
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Remove Unused docker image)
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ docker rmi zippyops01/javaapp-training:1
Untagged: zippyops01/javaapp-training:1
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS





Recent Comments

No comments

Leave a Comment