{{announcement.body}}
{{announcement.title}}

Building Docker Images to Docker Hub Using Jenkins Pipelines

DZone 's Guide to

Building Docker Images to Docker Hub Using Jenkins Pipelines

· Cloud Zone ·
Free Resource

Jenkins Pipeline is a powerful tool when you are using Jenkins to automate your deployments. Flexible and customized actions split between stages are a good reason to try this feature.

Building your own Docker Image and uploading it to Docker Hub to keep your repository updated is a good example to understand how Jenkins Pipelines can improve your way of work.

Prerequisites

  • A server with Jenkins and Docker running on it (Jenkins user should be allowed to run Docker).
  • Github account.
  • Docker Hub account.

Setting Up Your Environment

Install the Docker Pipelines plugin on Jenkins:

Manage Jenkins → Manage Plugins.

Search Docker Pipelines, click on Install without restart and wait until is done.
Upload your Dockerfile definition to your Github repository. Click on the green button Clone or Download and copy the URL as you will need it later.

Example Github repository


On Jenkins, you need to create 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. Fill in ID and Descriptions. Note that if you set the ID, you will need this specific ID to refer this credential from your scripts. Here we are just using dockerhub_id.

Docker Hub Id

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.

Creating Jenkins pipeline


Following that, you can skip all General and Build Trigger options and go straight to the Pipeline section. Here, you can include a Pipeline definition (usually named Jenkinsfile), or you can refer to an external location like Git or Subversion.

The Pipeline we are defining have four stages:

  • The first one is to get the Dockerfile from our Github repository.

  • The second one will build the image using $BUILD_NUMBER to tag the version.
  • The third one is pushing the built image to your Docker Hub registry.
  • Finally, we will cleanup the previously built image on the local server.

Please note that you need to modify the code with your specific Docker Hub and Github details:

Plain Text
 




x
35


 
1
pipeline { 
2
    environment { 
3
        registry = "YourDockerhubAccount/YourRepository" 
4
        registryCredential = 'dockerhub_id' 
5
        dockerImage = '' 
6
    }
7
    agent any 
8
    stages { 
9
        stage('Cloning our Git') { 
10
            steps { 
11
                git 'https://github.com/YourGithubAccount/YourGithubRepository.git' 
12
            }
13
        } 
14
        stage('Building our image') { 
15
            steps { 
16
                script { 
17
                    dockerImage = docker.build registry + ":$BUILD_NUMBER" 
18
                }
19
            } 
20
        }
21
        stage('Deploy our image') { 
22
            steps { 
23
                script { 
24
                    docker.withRegistry( '', registryCredential ) { 
25
                        dockerImage.push() 
26
                    }
27
                } 
28
            }
29
        } 
30
        stage('Cleaning up') { 
31
            steps { 
32
                sh "docker rmi $registry:$BUILD_NUMBER" 
33
            }
34
        } 
35
    }
36
}



Run Your Pipeline and Build Images

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:

Docker pipeline



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:

Docker Hub repository



This was a basic example of how to work with Pipelines and integrate different components of your deployments. There are possibilities of creating numerous complex integrations through Pipelines as you go ahead.

Some ideas to go further with Jenkins:

  • Define a webhook to run Pipeline when a commit is submitted to your Github repository.
  • Include several containers in the same pipeline to keep different stages (like backend and frontend) or different environments (dev/prod)
  • Set a notification by Email/Telegram/Slack with the status and/or output of your Pipeline.

As you can see, a lot of options are easily achievable. Just keep playing and getting deeper on Jenkins Pipelines Orchestration.

Topics:
cloud (add topic), cloud native, jenkins, kubernetes, serverless

Published at DZone with permission of Sudip Sengupta . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}