build and push docker image to docker hub using Jenkins2 min read

Prabhin Prabharkaran Administrator
DevOps Engineer

He is a Technical professional. He is a person who loves to share tricks and tips on the Internet. He Posts what he does!

follow me

HI All, this document deals with how to build and push docker image to docker hub using Jenkins.
Docker is a computer program that performs operating-system-level virtualization, also known as “containerization”. It was first released in 2013 and is developed by Docker, Inc. Docker is used to running software packages called “containers”
Jenkins is an open source automation server written in Java. Jenkins helps to automate the non-human part of the software development process, with continuous integration and facilitating technical aspects of continuous delivery. It is a server-based system that runs in servlet containers such as Apache Tomcat.

Prerequisites:

* Running Jenkins Server
* Java application code in GIT, here I’m using SCM as Git, you can use SVN as well. If you are using SVN as your SCM make sure you have installed SVN plugin.
* You need docker hub registry with credentials.
* make sure that you have dockerfile inside your repository.
* install cloud bees Docker plugin

My Sample Code can be found in this repository:  https://github.com/prabhinmp/maventest.git

Configuration

Step 1: Configure your credentials for SCM and Docker Hub in Jenkins server. You can do this Goto credentials from Jenkins main dashboard.
Step 2: Create a repository in the Docker hub to store all your images.


Step 3: Now Goto Jenkins create the job.


Step 4: In source code management select Git, it may differ in your case.
Step 5: Enter the repository URL and associate the SCM credentials.


Step 6: Since I’m using Maven for building the Java application so I’m using the maven plugin to build the app. so the build section in Jenkins will be like this.


Step 7: Now we have to add build post steps and make sure that post step should execute only after Build Sucess.
Step 8: In build post step select docker build and publish option and enter the following details,
repository
tag
credentials
Refer the below image for further reference

Step 9: Now save and apply
Step 10: Now start the job and wait for the job to get the finish.


Step 11: Once the job finished successfully, login to docker hub you can see the image uploaded to Docker hub with the tag as below.

Now you can take this image and deploy in any host as per your requirement.

 

© 2019, Techrunnr. All rights reserved.

#1
#2
#3
Questions Answered
Articles Written
Overall Points

Related posts

5 Thoughts to “build and push docker image to docker hub using Jenkins”

  1. Farrukh Waheed

    Thanks for the tutorial bro. but I’m facing this:
    18:54:38 [JENKINS] Archiving /var/lib/jenkins/workspace/mvn_test/target/mysqldb.war to AngularJsSqlEx/AngularJsSql/0.0.1/AngularJsSql-0.0.1.war
    18:54:38 channel stopped
    18:54:38 [mvn_test] $ docker build -t myhub/dice:webapp_tomcat_1 –pull=true /var/lib/jenkins/workspace/mvn_test
    18:54:38 time=”2020-01-03T18:54:38+05:00″ level=error msg=”failed to dial gRPC: cannot connect to the Docker daemon. Is ‘docker daemon’ running on this host?: dial unix /var/run/docker.sock: connect: permission denied”
    18:54:38 context canceled
    18:54:38 Build step ‘Docker Build and Publish’ marked build as failure
    18:54:39 Finished: FAILURE

    And I verified the command line $ docker build -t myhub/dice:webapp_tomcat_1 –pull=true /var/lib/jenkins/workspace/mvn_test working fine in a normal terminal.

    Any idea?

    1. it’s because the user which is running Jenkins doesn’t have the permission to execute the docker commands. add the Jenkins process running user to the docker group.

  2. Divyank Gupta

    HI Prabhin,
    Thanks for the tutorial, but i am facing this issue. I have also added docker credentials in credential manager.
    I tried to push the image directly from docker cli to docker hub, it worked. But when i do this as jenkins job docker build and publish step, this error occurs. Can you help me in resolving this.

    Successfully built 6dcaf50d6cff
    Successfully tagged divyankgupta/docker-jenkins-integration-sample:11jan
    SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have ‘-rwxr-xr-x’ permissions. It is recommended to double check and reset permissions for sensitive files and directories.
    [jenkins-docker-integration] $ C:\Program Files\Docker Toolbox\bin\docker tag 6dcaf50d6cff divyankgupta/docker-jenkins-integration-sample:latest
    [jenkins-docker-integration] $ C:\Program Files\Docker Toolbox\bin\docker inspect 6dcaf50d6cff
    [jenkins-docker-integration] $ C:\Program Files\Docker Toolbox\bin\docker push divyankgupta/docker-jenkins-integration-sample:11jan
    The push refers to repository [docker.io/divyankgupta/docker-jenkins-integration-sample]
    6173fc90427a: Preparing
    7d0784c2c563: Preparing
    715d0a3d2cc2: Preparing
    155d997ed77c: Preparing
    88cfc2fcd059: Preparing
    760e8d95cf58: Preparing
    7cc1c2d7e744: Preparing
    8c02234b8605: Preparing
    760e8d95cf58: Waiting
    7cc1c2d7e744: Waiting
    8c02234b8605: Waiting
    denied: requested access to the resource is denied
    Build step ‘Docker Build and Publish’ marked build as failure
    Finished: FAILURE

  3. Divyank Gupta

    Hi Prabhin,

    I am stuck in pushing docker image from jenkins to docker hub. I created a freestyle jenkins project where i built maven project and then try to create docker image and push it to docker hub. I am stuck at last step.

    When i try to push image from docker-cli it works. But when i do this using jenkins, it gets stuck. I have added docker credentials in credential manager. I am not able to resolve this. Any idea where i am going wrong.

    Successfully built 6dcaf50d6cff
    Successfully tagged divyankgupta/docker-jenkins-integration-sample:11jan
    SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have ‘-rwxr-xr-x’ permissions. It is recommended to double check and reset permissions for sensitive files and directories.
    [jenkins-docker-integration] $ C:\Program Files\Docker Toolbox\bin\docker tag 6dcaf50d6cff divyankgupta/docker-jenkins-integration-sample:latest
    [jenkins-docker-integration] $ C:\Program Files\Docker Toolbox\bin\docker inspect 6dcaf50d6cff
    [jenkins-docker-integration] $ C:\Program Files\Docker Toolbox\bin\docker push divyankgupta/docker-jenkins-integration-sample:11jan
    The push refers to repository [docker.io/divyankgupta/docker-jenkins-integration-sample]
    6173fc90427a: Preparing
    7d0784c2c563: Preparing
    715d0a3d2cc2: Preparing
    155d997ed77c: Preparing
    88cfc2fcd059: Preparing
    760e8d95cf58: Preparing
    7cc1c2d7e744: Preparing
    8c02234b8605: Preparing
    760e8d95cf58: Waiting
    7cc1c2d7e744: Waiting
    8c02234b8605: Waiting
    denied: requested access to the resource is denied
    Build step ‘Docker Build and Publish’ marked build as failure
    Finished: FAILURE

    1. have you configured credentials in jenkins?

Leave a Reply