How To Push a Docker Image To Docker Hub Using Jenkins

Reading Time: 3 minutes

In this blog we will see Building your own Docker Image and upload to Docker Hub and this whole process we will do it by the help of Jenkins. As we are familiar with Jenkins and how to write Jenkins file, if not do visit this page once https://www.jenkins.io/doc/book/pipeline/jenkinsfile/

In short I would say that A Jenkinsfile is a text file that contains the definition of a Jenkins Pipeline. Here for pushing a docker image to dockerhub using jenkins we have some prerequisites that you should have a github account and docker hub account. 

Setting up your environment:

  1. Install the Docker Pipelines plugin on Jenkins:

Manage Jenkins → Manage Plugins

Search Docker Pipelines, click on Install without restart and wait until it is done.

myimage
  1. Add Credentials.

In Jenkins you have to add a new credential with your Docker Hub account details. Go to Credentials → Global → Add credentials and fill out the form with your username and password. 

  1. Create your Jenkins pipeline.

Now, we will create our Pipeline then type the name you want for this Pipeline project and then click OK.

You have to add github credentials and give your github repository url. And save and apply it. Here you have to write your dockerfile for your project. It will be based on your application properties.

 4. And for running jenkins pipeline here we will write a jenkinsfile

which defines the pipeline. Please note you need to modify the jenkinsfile according to your details.

Now we are ready to run the Pipeline and check the output if an error is present on any stage during the run.

Go to your Pipeline project on Jenkins and click on Build Now to run manually. You should get a sequential output of the different stages similar to this one:

Jenkins file

You can also see console output:

hello

If everything is fine, you can check your Docker Hub repository for a new image tagged with the Jenkins build version matching with your Docker Hub registry:

Jenkins file

If it successfully pushes its well and good. Otherwise you might face an error. When I was applying for it I faced a problem. That was the Docker Permission Problem. I got the below error message while running the Jenkins job:

Jenkins file

To address this problem execute the below command on your Jenkins server terminal.

Jenkins file

Conclusion:

Here we have senn how to build your own Docker Image and upload to Docker Hub and this whole process we have done it by the help of Jenkins pipeline.As you can see, a lot of options are easily achievable by jenkins. Just keep playing and getting deeper on Jenkins Pipelines Orchestration. Thanks for being with me till the end. If you find this blog helpful do share it with your friends. Thanks!

HAPPY LEARNING!

Written by 

Naincy is currently a Software Intern at Knoldus. She is always ready to learn new technologies and tools. She loves painting and dancing.