Jenkins and Docker Build a Docker image using an jenkins pipeline and push it into docker registry

0 votes
I had this requirement to build a docker image via a Jenkins pipeline (script basically) and then push it into the docker registry.

How do I go about doing this? Can somebody guide me through the process?
Aug 26, 2019 in Jenkins by Karan
• 19,610 points
146,062 views

2 answers to this question.

+2 votes
Best answer

In the illustration below, i have created a docker image of jenkins with docker installed.

Here is the Dockerfile:

FROM jenkins/jenkins:lts
USER root
RUN apt-get update && \
apt-get -y install apt-transport-https \
    ca-certificates \
    curl \
    gnupg2 \
    software-properties-common && \
curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; 
echo "$ID")/gpg > /tmp/dkey; apt-key add /tmp/dkey && \
add-apt-repository \
    "deb [arch=amd64] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") \
    $(lsb_release -cs) \
    stable" && \
apt-get update && \
apt-get -y install docker-ce
RUN apt-get install -y docker-ce
RUN usermod -a -G docker jenkins
USER jenkins

This Dockerfile is built from jenkins official image, install docker and give access to user jenkins build dockers.

Note: You can build this image, but is already in dockerhub gustavoapolinario/jenkins-docker.

Running jenkins with docker from host:

To run the container, you need to add a volume in docker run command.

… -v /var/run/docker.sock:/var/run/docker.sock …

This way the docker socket (used in your machine) wil be shared with the container.

The complete run command:

docker run --name jenkins-docker -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock gustavoapolinario/jenkins-docker

After initializing the jenkins, complete the jenkins startup wizard and install the locale and blueocean plugins: Quick start with jenkins in docker.

Test the docker command by creating a Job:

Go to Jenkins homepage, click on “New Item”, select “Pipeline” and enter the job name as “docker-test”.

New pipeline Job.

Add this script inside the job:

pipeline {  environment {
    registry = "docker_hub_account/repository_name"
    registryCredential = 'dockerhub'
  }  agent any  stages {
    stage('Building image') {
      steps{
        script {
          docker.build registry + ":$BUILD_NUMBER"
        }
      }
    }
  }
}

The screen will be like this:

Pipeline in job config

Save the job.

Pipeline explanation:

In this pipeline, We have 2 environment variables to change the registry and the credential easily.

environment {
    registry = "docker_hub_account/repository_name"
    registryCredential = 'dockerhub'
}

The job will have one step.Docker build command will be run by utilizing the jenkins build number in the docker tag. 

stages {
  stage('Building image') {
    steps{
      script {
        docker.build registry + ":$BUILD_NUMBER"
      }
    }
  }
}

Test the docker command in the job:

Click on “Build Now” in job’s menu.

    Job Menu

The job will fail. 

Now click on build number (#1) -> “Console Output”.

You would see something like this:

Started by user GUSTAVO WILLY APOLINARIO DOMINGUES
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/docker-test
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Building image)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
[teste234] Running shell script
+ docker build -t docker_hub_account/repository_name:1 .
unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /var/jenkins_home/workspace/docker-test/Dockerfile: no such file or directory
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE

The error happens because the Dockerfile is not found. This will be resolved soon.

The docker command is executed and the jenkins has been successful in finding the command.

Creating a dockerhub repository:

Create a dockerhub account, if you don’t have yet.

Logged in dockerhub, click on “Create” > “Create Repository”.

   Dockerhub menu to Create Repository

Put a name for your repository. For this example, use “docker-test”.

   Creating Dockerhub Repository

After docker repository created, get the name to use in our pipeline. In this case, the name is “gustavoapolinario/docker-test”.

Create the dockerhub credential:

From  jenkins home, click on “credentials” and “(global)”.

 Credentials

Click on “Add Credentials” in left menu.

Put your credential and save it.

Remember to change the credential environment (registryCredential) if you didn’t put “dockerhub” in Credential ID.

The credential is configured.

Configuring the environment of dockerhub:

Alter the job pipeline. Go to jenkins home, click on job name (docker-test), click on “Configure” in job menu.

You need to change the following code:

environment {
    registry = "docker_hub_account/repository_name"
    registryCredential = ‘dockerhub’
}

Change the environment variable “registry” to your repository name. In this case it is “gustavoapolinario/docker-test”.

Change the environment variable “registryCredential” if necessary.

My environment is:

environment {
    registry = "gustavoapolinario/docker-test"
    registryCredential = ‘dockerhub’
}

Building the first docker image:

With dockerhub credential and repository created, the jenkins can send the docker image build to dockerhub (our docker repository).

In this example, let’s build a node.js application. We need a Dockerfile to the build.

Create a new step in pipeline to clone a git repository that has a Dockerfile inside.

stage('Cloning Git') {
  steps {
    git 'https://github.com/gustavoapolinario/microservices-node-example-todo-frontend.git'
  }
}

The pipeline must be (but using your environment):

pipeline {
  environment {
    registry = "gustavoapolinario/docker-test"
    registryCredential = ‘dockerhub’
  }
  agent any
  stages {
    stage('Cloning Git') {
      steps {
        git 'https://github.com/gustavoapolinario/microservices-node-example-todo-frontend.git'
      }
    }
    stage('Building image') {
      steps{
        script {
          docker.build registry + ":$BUILD_NUMBER"
        }
      }
    }
  }
}

Save and run it clicking on “Build Now”. The Stage view in jenkins job will change to this:

Deploying the docker image to dockerhub:

At this moment, we clone a git and build a docker image.

We need put this image in docker registry to pull it in other machines.

First, create a environment to save docker image informations.

dockerImage = ''

Change the build stage to save build information in environment.

dockerImage = docker.build registry + ":$BUILD_NUMBER"

Ceate a new stage to push the docker image builded to dockerhub.

stage('Deploy Image') {
  steps{    script {
      docker.withRegistry( '', registryCredential ) {
        dockerImage.push()
      }
    }
  }
}

After build and deploy, delete the image to cleanup your server space.

stage('Remove Unused docker image') {
  steps{
    sh "docker rmi $registry:$BUILD_NUMBER"
  }
}

The final code will be (remember, using your environment):

pipeline {
  environment {
    registry = "gustavoapolinario/docker-test"
    registryCredential = 'dockerhub'
    dockerImage = ''
  }
  agent any
  stages {
    stage('Cloning Git') {
      steps {
        git 'https://github.com/gustavoapolinario/microservices-node-example-todo-frontend.git'
      }
    }
    stage('Building image') {
      steps{
        script {
          dockerImage = docker.build registry + ":$BUILD_NUMBER"
        }
      }
    }
    stage('Deploy Image') {
      steps{
        script {
          docker.withRegistry( '', registryCredential ) {
            dockerImage.push()
          }
        }
      }
    }
    stage('Remove Unused docker image') {
      steps{
        sh "docker rmi $registry:$BUILD_NUMBER"
      }
    }
  }
}

Save and run it.

The build is now complete and the image will be send to docker registry.

Ready to level up your Docker skills? Enroll now in our comprehensive Docker Course!

answered Aug 26, 2019 by Sirajul
• 59,230 points

selected Sep 3, 2019 by Kalgi
What is Blue Ocean in jenkins?
Sorry if this sounds stupid but where do I start here, do I run the Dockerfile at the top of the post to install docker or so I have to have it installed already.
You need to run the dockerfile as explained in the beginning of the answer. That would install docker I suppose.
What is "build_number" in dockerImage = docker.build registry + ":$BUILD_NUMBER".

Where do i find it
It is just a version no. Say you have one docker image like centos:7. Here 7 is the build number.
if we have deployment file to run the container then how could we  replace the image name into deployment.yaml file?

Thanks

Prabha
Hi@Prabha,

If you have a deployment file, then you have already written the image name inside your file. Otherwise, your deployment will not work.
+1 vote

You try something like this:

pipeline {  environment {
    registry = "docker_hub_account/repository_name"
    registryCredential = 'dockerhub'
  }  agent any  stages {
    stage('Building image') {
      steps{
        script {
          docker.build registry + ":$BUILD_NUMBER"
        }
      }
    }
  }
}

The above pipeline script, builds a docker image and pushes every single build onto the registry.

Level up your skills with our Microservices Training.

answered Sep 3, 2019 by Hari
I’m building a docker image using jenkins pipeline and push it into docker registry(docker hub), now i want to pull and run this image in other remote server. I am not sure this step (pull&run) must be in jenkins pipline and how.
Use Jenkins plugin in your remote system. You will find one option named cloud node. There you can set your configuration.

Related Questions In Jenkins

0 votes
1 answer

Use a docker image with jenkins declarative pipeline

Hey @Jithin, you could use something like ...READ MORE

answered Apr 12, 2019 in Jenkins by Bishu
4,927 views
0 votes
5 answers
0 votes
1 answer

Use Custom Docker registry with jenkins declarative pipeline

Yes, you can! try something like this: node ...READ MORE

answered Apr 12, 2019 in Jenkins by Bishu
2,374 views
+2 votes
1 answer
+2 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 58,020 points
2,713 views
+1 vote
1 answer
0 votes
2 answers

Jenkins Pipeline: Build using parameters

First, as @sophie may mentioned it you should name ...READ MORE

answered Aug 8, 2019 in Jenkins by Sirajul
• 59,230 points
17,910 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP