Jenkins Building Docker Image and Sending to Registry

Hi everyone. In this tutorial we will create a docker image with jenkins and send then to dockerhub.

What’s docker?
Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications.

You can learn more about docker in docker website

Why pipeline?
You can reuse everything you did, put your jenkins code inside git project, the change in pipeline is showed in “changes” inside job history

What’s dockerhub?
Dockerhub is a public docker registry to store your docker images inside. If you want a private registry, you can pay for it. We will use it because it is the most easeful docker registry.

What’s docker registy?
Docker registry is a server to distribute versions of docker images.

Jenkins with docker installed

We will use some pipeline codes, the jenkins need have installed docker inside him to find this commands.

I created a docker image of jenkins with docker installed.

Let’s see the Dockerfile:

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

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 add a volume in docker run command.

It will share the docker socket (used in your machine) with the container.

The complete run command:

After inicialized the jenkins, complete the jenkins startup wizard.

Read this tutorial to complete wizard and install the locale and blueocean plugins: Quick start with jenkins in docker.

Creating a job to test docker command

In home of jenkins, click on “New Item”, select “Pipeline” and put the job name as “docker-test”.

Put this script inside of job:

The screen will be like this:

Save the job.

Pipeline explanation

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

The job will have one step. It will run the docker build and use the jenkins build number in docker tag. With build number turn easeful to deploy or rollback based in jenkins.

Testing the docker command in job

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

The job will failure. Don’t worry.

In job’s home, you can click in circle and see the console output.

Or click on build number (#1) and click on “Console Output”.

In Console Output you will see this:

The error happens because the Dockerfile is not finded. We will resolve it soon.

The docker command is executed and the jenkins found the command.

Creating a dockerhub repository

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

dockerhub

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

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

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

Creating dockerhub credential

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

Click on “Add Credentials” in left menu.

Put your credential and save it.

Remenber 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.

The code you need to change is:

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

Change the environment variable “registryCredential” if necessary.

My environment is:

Building the first docker image

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

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

Let’s create a new step in pipeline to clone a git repository that have a Dockerfile inside.

The pipeline must be (but using your environment):

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.

Change the build stage to save build information in environment.

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

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

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

Save and run it.

Awesome, the build is complete and the image will be send to docker registry.

Complete pipeline to a node.js application

This step is for who did this tutorial: Jenkins Starting with Pipeline doing a Node.js test.

To complete the node.js pipeline, let’s change this pipeline to merging with node.js build and test.

The complete pipeline will be:

Now you have a pipeline for test and create a docker image for your application.

Thanks.

— — — — — — — — — — — — — — —

A special thanks to Matthias Döring, who notify me about the lack of docker cleanup. It is necessary to your server don’t use all disc space. The docker images are usefull bexause they are cache to next build, but use a lot of disc space.

https://medium.com/@cryptolukas/you-should-add-this-as-last-stage-or-post-task-d69fb384a361

I’m a FullStack Developer, working now with DevOps automations. I previously worked as a front-end, PHP Developer, and DevOps.