Integrate docker with Jenkins using AWS EC2 instance

Srini
6 min readSep 8, 2020

--

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/

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

Fill the form and continue

Go to manage jenkins → and Manage plugins

Search for

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

Go to Manage jenkins → and select Global Tool Configuration

Select add maven option as shown below and enter the following

Open now Docker machine using new terminal

use the following command to Install docker in the terminal window

yum install docker

service docker start

systemctl status docker

docker images

useradd dockeradmin

passwd dockeradmin

give new password and remember new password

groupadd docker

usermod -aG docker dockeradmin

sshd_config — OpenSSH daemon configuration file

vi /etc/ssh/sshd_config

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

Now restart the service

service sshd restart

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

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

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

Go to Manage jenkins → Manage Plugins

search Maven Integration and click install without restart

Go to Jenkins dashboard and click new item

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

Build Section

In the build section Goals and options: clean install package

Under the post-build Actions

Select → Send build artifacts over ssh as shown below

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

Now go to docker machine and use the following command.

Now there are no images

docker images

Go to Jenkins dashboard and click Build now

The image shows the job is getting build
The status shows the build is unstable (Orange)
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

Now the build is successful (Blue)

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

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

docker images

We now completed continuous integration

--

--

No responses yet