Docker — DevOps — Jenkins Setup
An article to setup Jenkins via using Docker containers i.e. docker run command
Overview
Hey to start off let’s think of an old traditional setup, we use to have an old desktop or laptop with a limited set of resources and low on performance.
Next, I want you to also visualize a scenario where we need to install quite a bit of DevOps tools and packages like Jenkins, Docker, Maven, Mysql and many more onto a single machine and to run all of them at once… quite bizarre isn’t it?
Even today if we need to spin up multiple virtual machines onto our home machines, its performance starts degrading!
So, to help us with the above situation technologist came up with a super popular tool i.e. Docker, that provides the light weighted images for most of the popular software or packages.
A brief introduction for docker could be found on link Docker Overview.
Our Goal
So, what are we going to set up or learn over this blog?
In the next few steps, we will try to set up and create a modern-day DevOps use case i.e. we install and set up Jenkins and Tomcat and that is via using Docker images and will then create a Jenkins job to deploy a Java application over the tomcat.
Prerequisites
- A machine i.e. a laptop or home PC.
- Docker to be installed, up and running.
- A command line utility like Putty etc.
- Knowledge(bit) of Jenkins & tomcat.
Note: Please refer to following link Docker Installation for Docker installation steps.
Execution
Assuming that all prerequisites are closed and now docker is up & running.
First, we going to run 2 containers i.e. a Jenkins & a tomcat container. Post that we will try to access tomcat setup via Jenkins container and try to play around with the tomcat setup, using Jenkins jobs & plugins.
1. Jenkins (container)
Below is a complete docker run command to spin up a Docker container for Jenkins i.e. using an image jenkinsci/blueocean.
docker container run -u root --rm -d -p 8080:8080 -v jenkins-data:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock jenkinsci/blueocean
Docker will first look for an image on the local repository and in case of failure will go for a search on docker hub i.e. docker global repository.
Next, check for container logs and grab the Jenkins admin user password as the same is required for the first log in and initial setup.
If all goes well, Jenkins should be up and running as of now and the same can be verified via hitting up Url as follows <docker IP:8080>.
Enter the administrator password as we captured from Jenkins container logs to initialize the setup, next continue with Jenkins installation and setup with default plugins, users.
2. Tomcat (container)
Use the below command to spin up a container i.e. using a centos image so as to set up a customized tomcat.
docker run -d -t --name tomcat -h tomcat -p 8888:8080 centos
docker ps
Install java and tomcat
Jump onto the tomcat container and let’s quickly download and install the required packages i.e. java and the tomcat.
$teckdevOps$ docker exec -it tomcat /bin/bash
$[root@tomcat /]# yum install java
$[root@tomcat /]# mkdir teckdevOps
$[root@tomcat /]# cd teckdevOps/$[root@tomcat /]# wget http://mirrors.estointernet.in/apache/tomcat/tomcat-8/v8.5.41/bin/apache-tomcat-8.5.41.tar.gz$[root@tomcat /]# tar -xvzf apache-tomcat-8.5.41.tar.gz
$[root@tomcat /]# cd apache-tomcat-8.5.41/bin
$[root@tomcat /]# ./startup.sh
Once we finish with the above steps, tomcat should be accessible over port 8888.
3. Jenkins — Plugins
Before we proceed with our workflow we need to install a few of Jenkins plugins. Plugins basically empower Jenkins and come with additional/extensive features.
Plugins to Install
- Maven Integration
- Publish over SSH
Jenkins → Manage Jenkins → Manage Plugins → Maven Integration
Jenkins → Manage Jenkins → Manage Plugins → Publish over SSH
4. Jenkins — Pre Setup
Post installation of mandatory plugins, next we have to close some of the pre-requisite before we go, set up and run our Jenkins job.
Setup SSH server details i.e. follow the below path and setup tomcat server connection details(as shown in the following snapshot).
Jenkins → Manage Jenkins → Configure System → Publish over SSH.
Test connection, Save & proceed if success. 😎
5. Jenkins - Jobs
Finally, we going to create a Jenkins job to pick the code from the source code repository(git in our case) and deploy it over the tomcat server.
Jenkins → New item
i) Jenkins → Create a new job
Select a freestyle project, give a name to it and proceed to the next step.
ii) Job → SCM
Give a brief project description and move to the SCM(Source Code Management) section of the job that is to be used to configure the git project repository URL.
Just for the demo purpose and to simplify the process we going to use a sample tomcat application that is readily available over the internet.
Configure git repository from where our code to be picked and proceed to the next step.
iii) Job → Post Build Step
Lastly, jump on to ‘Post-build Actions’ and select ‘Send build artifacts over SSH’ and configure deployment steps i.e. server, remote directory and execution commands for deployment(refer to below snapshot). Save & Apply.
iv) Job → Build Now
On Jenkins project page initiate a manual Build to initiate the build process, monitor the console logs for job processing and check for any error/exception.
Console logs
Finally, we can check for deployment status on the tomcat server and if all looks ok our new tomcat application url should be ready, up and running.
Tomcat server - deployment status
Tomcat Url
Bravo!! 🎯
Our DevOps implementation cycle has been executed well and we have successfully deployed a sample application over the tomcat server i.e. via Jenkins job.
Great and well done for making it to the end, hope article will be useful and comprehensive for all reading junkies!
— A blog by teckdevOps