Jenkins Workflow Push Docker images to DockerHub.

In this blog, I will share the simplest way of deploying Docker Images to Docker Hub via Jenkins. I will be using a sample web app for deployment that is written in Django and available in my GitHub repo. I assume that you have the Jenkins server and docker up and running im currently running on my local machine so it is accessible on localhost:8080.

I have my project hosted on GitHub https://github.com/musaib072/Stock I have cloned the repo on my local system and created the Dockerfile in the root of the Workspace Here's how my Docker file looks

Once your Dockerfile is ready you can test the working of the app locally by first building an image

docker build -t <imagetag> .

The dot at the ending specifies that the Docker file is in the root of the directory from where you are running the command if it is in the other folder then you have to specify the path. After building the image you can run the image locally by

docker run -p 8000:8000 <imagetag>

The app can be accessible on your localhost:8000.Now you can push the code containing Dockerfile to your GitHub repository now we are going to build the image using Jenkins and automate the flow of fetching the code whenever there is a push then building the image and deploying to docker hub.

Go to https://hub.docker.com/ Click on the Repository tab and create a repository for storing the image give it a name and select Public/Private according to your convenience. After that go to Jenkins go to manage Jenkins Click on manage plugins and install the Git, Github, CloudBees Docker Build and Push Plugin and click on install without restart. Now on the main Dashboard click on New Item select freestyle project give it a name when the configuration will open select the GitHub Project to give the URL of your project repo. In source code management select Git again and enter the URL of your repo. If your repo is private you will need to provide the credentials. In Build trigger select GitHub hook trigger for GITScm polling. In the Build steps select Docker Build and Publish Image option note this option will only be available when you will install CloudBees Docker Build and Push. Give the repository name that you created in the docker hub with your account name it will be like accountname/reponame in tag give ${BUILD_NUMBER} so the image will be given a tag according to the build number In the docker host URI enter the address of your docker daemon it runs on port 2375 without TLS check if it is enable by going to docker desktop and in setting under general tab there is an option for exposing docker daemon and make sure it is checked now in Jenkins Under docker host URI add tcp://localhost:2375 if you are running docker on server replace it with public IP of your server. Under the Registry credential add the credential of your docker hub account that is username and password click on Apply .

Now it's Time to configure the GitHub repo. Click on the project repo and under the setting tab you will see the WEBHOOK option click on that and in payload url give the URL of your Jenkins server Note it doesn't accept localhost if you are running Jenkins on localhost on your machine you need to expose the localhost on the internet you can do it by downloading and installing ngrok which will give you a URL at the end of URL add /github-webhook/as in my case im running Jenkins on the local machine i have configured ngrok . After you are done setting the URL add it to your webhook it will look like this

Now make a change in the readme file and push the code to the GitHub repo you will see that it automatically triggers the build and publish it to docker hub.

Did you find this article valuable?

Support MUSAIB SHAIKH'S BLOG by becoming a sponsor. Any amount is appreciated!