Guide
March 26th

Docker Jenkins tutorial. Push docker images to Dockerize.io from your Jenkins jobs

In this brief docker jenkins tutorial you will learn how to configure Jenkins jobs to build Docker Images based on a Dockerfile and push it automatically to Dockerize.io.

Prerequisites

1. Docker installed

Before you start, you need to have docker installed and your docker daemon should be running. To check it, just run the following command:

$ docker --version

If you don't have Docker installed, select your OS from the list below and follow the setup instructions:

2. A dockerized application code in git

Let's create a simple Hello World application on Flask Python a push it on a github repository.

  • app.py:
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Flask Dockerized'

if __name__ == '__main__':
    app.run(debug=True,host='0.0.0.0')
  • requirements.txt:
Flask==0.10.1
  • Dockerfile:
FROM ubuntu:latest

RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential

COPY . /app

WORKDIR /app

RUN pip install -r requirements.txt

ENTRYPOINT ["python"]
CMD ["app.py"]

For testing purpose, you can build the image and run the container typing:

$ docker build -t flask-sample:latest .
$ docker run -p 5000:5000 flask-sample

Then, you will see the app running on http://localhost:5000.

Finally, just push you hello world application in a code repository such as Github, Gitlab or Bitbucket.

Step 1: Setting up Jenkins using Docker

There are at least two ways to have a Jenkins instance, you can install it manually from here or you can use the official jenkins docker image from DockerHub. It is recommended using the LTS version, what you can try by-self using the weekly release version applying the same instructions.

The official image doesn't contain docker installed, so let's create a docker image with Jenkins and Docker installed just making a new Dockerfile:

# 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. To get the jenkins docker build, let's run the following command:

$ docker build -t jenkins-with-docker .

To run the container, you need to share the docker socket (used in your machine) with the container, to do it you can use a docker volume (-v option).

$ docker run \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -p 8080:8080 \
    jenkins-with-docker

Then, your Jenkins docker server instance will be running on http://localhost:8080.

header image

Step 2: Complete the Setup Wizard

As you can see on the previous screen, run jenkins on docker welcomes us with the “Setup Wizard Screen” which requires us to enter the “InitialAdminPassword” located under the JenkinsHome_ directory defaulted to /var/jenkins_home/secrets/initialAdminPassword. The password is displayed in the start-up logs as well.

Easy way (without extra effort)

When you run the Jenkins container, you can find the admin password embedded into the logs:

...
*************************************************************
*************************************************************
*************************************************************

Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:

15ad1140f50849d3a50e599918e885a4

This may also be found at: /var/jenkins_home/secrets/initialAdminPassword

*************************************************************
*************************************************************
*************************************************************
...

header image

Funny way

Let's play with container a bit.

  • 1 - Obtain the container id to inspect it:
$ docker ps

As you can see on the following image the container id that we will use for this tutorial is ffa4cbbb4f50.

header image

  • 2 - Run a bash terminal inside the container

Just execute $ docker exec -ti <container_id> /bin/bash:

$ docker exec -ti ffa4cbbb4f50 /bin/bash

It would open a bash terminal inside out container, so you will have access to the Jenkins configuration files used on the executed container.

header image

  • 3 - Obtain the admin pass

There is a initialAdminPassword file inside var/jenkins_home/secrets that contain the admin pass.

$ cd var/jenkins_home/secrets/
$ cat initialAdminPassword

header image

For this tutorial the admin key is 15ad1140f50849d3a50e599918e885a4

Once you have set the admin pass and pressed on continue button, you will be able to customize your Jenkins. Plugins extend Jenkins with additional features to support many different needs. For this tutorial let's use the suggested plugins, son click on the first button.

header image

Then, you will be able to monitor the plugin's installation for a while. It could take some minutes.

header image

Then you can create a new admin user adding the required information:

header image

The last step is to set the Jenkins URL up and start to using!!!

header image

header image

Then you jenkins server instance is READY!!!

header image

Step 3 - Create a Job

You need to create and configure a job on Jenkins who will be responsible to build the application and push it on Dockerize. For this we will use a general purpose Job, but you also can generate a pipeline job.

1. Create your app on Dockerize

Just create a new app via cli or admin panel and set a port to 5000.

header image

Then keep in mind the deploy info, you will need it to configure the jenkins job.

header image

2. Create the application Job

  1. Click on New item, then enter an item name and choose the freestyle project option.
  2. Choose your source code management (git in this tutorial) and set the repository URL and credentials.
  3. On build step, choose execute shell and write the commands provided by Dockerize (Deploy screen), then just save the configuration:
$ docker login -u <USERNAME> -p <PASSWORD> reg.dockerize.io
$ docker build -t reg.dockerize.io/fgriberi-461be96/python-flask-jenkins .
$ docker push reg.dockerize.io/fgriberi-461be96/python-flask-jenkins

Let's go through all the steps together:

Then, you will be able to run the build from Jenkins. To do it just follow the next steps:

  1. Click on Build Now button
  2. On your Build History, click on the last build
  3. Click on Console Output to monitor the job execution
  4. At the end, you will see the following message Finished: SUCCESS
  5. Go to Dockerize.io app created befire and continue with the steps there until release button.

Congratulation!!! Your job has uploaded your docker project image on dockerize!

NOTE: You can arrive to the same result using Pipeline-As-A-Code Job in Jenkins, the cool part of using this plugin is that your entire Jenkins job configuration can be created, updated and version controlled along with the rest of our source code.

Bonus 1: SSL certificate for HTTPS

It's already done for you. If you need to connect your custom domain, SSL certificate will be provided for it.


Bonus 2: Autoscaling

With autoscaling the app will be scaled up when CPU and RAM load goes up and scaled down when it goes down.

Now you can deploy your Jenkins Job without a massive build time.

Dockerize your Job

Just for $2/month
Share this article:

Ask a question

Please type your name.
Please provide your email address.
Please tell us a bit more

More great guides

Docker nodejs express tutorial

Docker NodeJS Express guide

Simple Nodejs Socket guide for docker deploy. Use it for comfort start with us.

Read Guide
Docker python django tutorial

Docker Python Django tutorial

This is a brief tutorial that presents how you can run a simple 'Hello World'-style web application written in Django in docker.

Read Guide
Docker PHP laravel tutorial

Docker PHP laravel tutorial

This quickstart guide provides a basic introduction to the Laravel framework running on docker. This is a great starting point if you are brand new to the Laravel framework or if you want to have your docker PHP app.

Read Guide
7 times faster deployment
Dockerize automaticaly completes deployment after receiving your Docker image
50% of AWS price
We don’t have any hidden prices and will never ask you to pay extra fee for our services
Open Sidebar Close Sidebar