Docker with Jenkins, git, maven and OpenShift CLI (oc)

Introduction

Creation of a docker image intended to be used for deploying Java applications into OpenShift (Red Hat’s public cloud application development and hosting platform that automates the provisioning, management and scaling of applications).

Dockerfile creation

The official Dokerfile from Jenkins already has git but it lacks maven and oc tools. Therefore, let's extend it to include them.

Let's create a new Dockerfile file with content:

FROM jenkinsci/jenkins:lts
USER root
RUN apt-get update
RUN apt-get install -y maven
RUN apt-get install -y wget
RUN rm -rf /var/lib/apt/lists/*
RUN wget https://github.com/openshift/origin/releases/download/v1.5.0/openshift-origin-client-tools-v1.5.0-031cbe4-linux-64bit.tar.gz
RUN mv openshift-origin-client-tools-v1.5.0-031cbe4-linux-64bit.tar.gz /opt/
RUN tar -zxvf /opt/openshift-origin-client-tools-v1.5.0-031cbe4-linux-64bit.tar.gz -C /opt/
RUN rm /opt/openshift-origin-client-tools-v1.5.0-031cbe4-linux-64bit.tar.gz
RUN echo 'export OC_CLIENT=/opt/openshift-origin-client-tools-v1.5.0-031cbe4-linux-64bit\nexport PATH=$OC_CLIENT:$PATH\n' >> /etc/bash.bashrc
USER jenkins

Docker image build

docker build -t ib/jenkins_oc .

Once command completes successfully, let's check the new image 'ib/jenkins_oc':

$ docker images
REPOSITORY                              TAG                 IMAGE ID            CREATED             SIZE
ib/jenkins_oc                           latest              eb8487a11e20        6 minutes ago       1.03GB

Run the new docker container

docker run -p 8090:8080 -p 50000:50000 -v /srv/jenkins_home:/var/jenkins_home --name jojenkins1 ib/jenkins_oc

The docker image exposes jenkins http on port 8090 and it persist jenkins configuration on the host machine at the previously created directory '/srv/jenkins_home'.

Access Jenkins

From a browser in the host machine:

http://127.0.27.27:8090

and enjoy it creating your jobs that retrive Java source code from git, package it with maven and deploy it to OpenShift cloud.