Integrate docker with Jenkins using AWS EC2 instance

Prerequisites

Jenkins Instance
AMI- RHEL-8
Instance type- T2 Medium
Security group- port 8080 (custom TCP)- anywhere

Docker Instance

AMI- bitnami-stacksmith-base-centos-7
Instance type- T2 Micro
Security group- Type:All TCP- Source:anywhere

Step1 Connect to the jenkins machine

Install the following utilities by running the following command

yum install wget zip unzip -y

Step2 Install java

wget --no-cookies --header "Cookie: gpw_e24=xxx; oraclelicense=accept-securebackup-cookie;" "http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.rpm"

rpm -ivh jdk-8u131-linux-x64.rpm

java -version

Step3 Install tomcat

wget http://apache.mirror.vexxhost.com/tomcat/tomcat-8/v8.5.57/bin/apache-tomcat-8.5.57.zip

unzip apache-tomcat-8.5.57.zip

mv apache-tomcat-8.5.57 /opt/tomcat

chmod -R 755 /opt/tomcat

start tomcat using the following command

/opt/tomcat/bin/startup.sh

Step4 Check the browser by providing the public ipaddress:portnumber

http://3.80.3.41:8080/

Image for post
Image for post

Step5 Deploy Jenkins war file inside the tomcat of the webapps

you can check the link here https://updates.jenkins-ci.org/download/war/ and use the latest one

wget https://updates.jenkins-ci.org/download/war/2.255/jenkins.war

cp jenkins.war /opt/tomcat/webapps/

Now update the tomcat wepage by adding jenkins at the end as shown below

http://3.80.3.41:8080/jenkins

Step6 Now install Git

yum install git -y

cat /root/.jenkins/secrets/initialAdminPassword

Paste the following highlighted password in the jenkins page

Image for post
Image for post
Image for post
Image for post

Fill the form and continue

Image for post
Image for post

Go to manage jenkins → and Manage plugins

Image for post
Image for post

Search for

1.Publish Over SSH,
2. Maven Invoker and
select install without restart option

Image for post
Image for post

Go to Manage jenkins → and select Global Tool Configuration

Image for post
Image for post

Select add maven option as shown below and enter the following

Image for post
Image for post

Open now Docker machine using new terminal

Image for post
Image for post

use the following command to Install docker in the terminal window

yum install docker

service docker start

systemctl status docker

docker images

Image for post
Image for post

useradd dockeradmin

passwd dockeradmin

give new password and remember new password

Image for post
Image for post

groupadd docker

usermod -aG docker dockeradmin

sshd_config — OpenSSH daemon configuration file

vi /etc/ssh/sshd_config

Image for post
Image for post

Remove the comment for line 1 by deleting # symbol and add comment the line 3 by adding # save and exit the file . The final output looks as shown below

Image for post
Image for post

Now restart the service

service sshd restart

Image for post
Image for post

cd /opt
mkdir docker
cd docker

vi dockerfile

Paste the following text in the windowm, save and exit

FROM nginx:alpine
COPY . /usr/share/nginx/html

Next we need to give permission for docker admin

chown -R dockeradmin:dockeradmin /opt/docker
ls -ltra

Go to Jenkins dashboard now, and click Configuration system

Image for post
Image for post

Go to Publish over SSH section and enter the following

Name- docker_host
Hostname: Docker Machine Private IP address
Username dockeradmin
password: Docker Admin password
and click test configuration

Image for post
Image for post

Once you click test configuration you should see status as success as shown below

Image for post
Image for post

Go to Manage jenkins → Manage Plugins

Image for post
Image for post

search Maven Integration and click install without restart

Image for post
Image for post

Go to Jenkins dashboard and click new item

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Give Any description and select Github Project .In the Project URL use the link shown below

In the source code management use the same url link as shown below and select Git in the right

Image for post
Image for post

Build Section

In the build section Goals and options: clean install package

Image for post
Image for post

Under the post-build Actions

Image for post
Image for post

Select → Send build artifacts over ssh as shown below

Image for post
Image for post

Enter the following values in the tables

source files : webapp/target/*.war
Remove Prefix : webapp/target
Remote directory : /opt/docker

Exec Command :
docker pull ubuntu
docker run -it -d ubuntu

Click apply and save

Image for post
Image for post

Now go to docker machine and use the following command.

Now there are no images

docker images

Image for post
Image for post

Go to Jenkins dashboard and click Build now

Image for post
Image for post
Image for post
Image for post
The image shows the job is getting build
Image for post
Image for post
The status shows the build is unstable (Orange)
Image for post
Image for post
Check the log file for error

If the job is stable and permission denied use the following command in docker machine and again run the build command

sudo chmod 666 /var/run/docker.sock

Image for post
Image for post
Now the build is successful (Blue)

Check the log file to see the status of the build by clicking the blue balloon

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Now got to Docker machine and type the following command . you will now see ubuntu image

docker images

Image for post
Image for post

We now completed continuous integration

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store