Integrate Docker in CI/CD pipeline using Jenkins on AWS EC2
Docker is a tool designed to make it easier to create, deploy, and run applications by using containers.
Install Docker
Pre-requisites
Amazon Linux EC2 Instance
Install docker and start docker services
yum install docker -y docker — version
# start docker services
service docker start
service docker status
docker pull tomcat:latest
Create a user called dockeradmin
useradd dockeradmin
passwd dockeradmin
Add a user to docker group to manage docker
usermod -aG docker dockeradmin
Validation test
Create a tomcat docker container by pulling a docker image from the public docker registry
docker run -d — name test-tomcat-server -p 8090:8080 tomcat:latest
Integration between Docker-host and Jenkins
Install “publish Over SSH”
Manage Jenkins
>Manage Plugins
>Available
>Publish over SSH
Enable connection between Docker-host and Jenkins
Manage Jenkins
>Configure System
>Publish Over SSH
>SSH Servers
SSH Servers: — Name:
docker-host
Hostname:
<ServerIP>
Advanced
> choseUse password authentication, or use a different key
password:
******
Create a Dockerfile in dockeradmin (/home/dockeradmin)
# Pull tomcat latest image from dockerhub
From tomcat:latest
# Maintainer
MAINTAINER “Ravi V”
# copy war file on to container
COPY ./webapp.war /usr/local/tomcat/webapps
Create a Jenkins Job
Enter an item name:
DeployUsingDocker
use Maven project
Source Code Management:
Repository:
<Git Repository>
Branches to build :
*/master
Poll SCM : —
* * * *
Build:
Root POM:
pom.xml
Goals and options:
clean install package
Post-build Actions
Send build artifacts over SSH
SSH Publishers
SSH Server Name:
docker-host
Transfers
>Transfer set
Source files:
webapp/target/*.war
Remove prefix:
webapp/target
Remote directory:
//home//ansadmin
or.
Exec command:
cd /home/dockeradmin;
docker build -t simple-devops-image .;
docker run -d — name simple-devops-container -p 8080:8080 simple-devops-image;
Save and run the job now.
Now integration with docker is complete. Access http://<public ip>:8080/<warfile>