- Prerequisites
- Installing Docker on
EC2(Amazon Linux Server)
for community version of docker - Integrating Docker with Jenkins
- Jenkins Job to copy artifacts on to DockerHost
- Build Jenkins
- Create Dockerfile & Run Image
- Deploy a war file on Docker container using Jenkins
- Build Jenkins
Prerequisites
- EC2 Instance
- With Internet Access
- Security Group with Port 8080 open for internet
- Jenkins Server
- Java v1.8.x
Installing Docker on EC2(Amazon Linux Server)
for community version of docker
yum update -y
yum install docker
Check docker status
$ service docker status
Redirecting to /bin/systemctl status docker.service
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: <https://docs.docker.com>
Start docker
$ service docker status
Redirecting to /bin/systemctl status docker.service
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2020-09-17 06:49:47 UTC; 3min 2s ago
Docs: <https://docs.docker.com>
Process: 3804 ExecStartPre=/usr/libexec/docker/docker-setup-runtimes.sh (code=exited, status=0/SUCCESS)
Process: 3791 ExecStartPre=/bin/mkdir -p /run/docker (code=exited, status=0/SUCCESS)
Main PID: 3812 (dockerd)
Tasks: 8
Memory: 37.1M
CGroup: /system.slice/docker.service
└─3812 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --default-uli...
Sep 17 06:49:47 docker dockerd[3812]: time="2020-09-17T06:49:47.583359680Z" level=info msg="scheme...grpc
Sep 17 06:49:47 docker dockerd[3812]: time="2020-09-17T06:49:47.583375963Z" level=info msg="ccReso...grpc
Sep 17 06:49:47 docker dockerd[3812]: time="2020-09-17T06:49:47.583388775Z" level=info msg="Client...grpc
Sep 17 06:49:47 docker dockerd[3812]: time="2020-09-17T06:49:47.607211309Z" level=info msg="Loadin...rt."
Sep 17 06:49:47 docker dockerd[3812]: time="2020-09-17T06:49:47.762254453Z" level=info msg="Defaul...ess"
Sep 17 06:49:47 docker dockerd[3812]: time="2020-09-17T06:49:47.814285724Z" level=info msg="Loadin...ne."
Sep 17 06:49:47 docker dockerd[3812]: time="2020-09-17T06:49:47.836780564Z" level=info msg="Docker...6-ce
Sep 17 06:49:47 docker dockerd[3812]: time="2020-09-17T06:49:47.837283580Z" level=info msg="Daemon...ion"
Sep 17 06:49:47 docker systemd[1]: Started Docker Application Container Engine.
Sep 17 06:49:47 docker dockerd[3812]: time="2020-09-17T06:49:47.872415421Z" level=info msg="API li...ock"
Hint: Some lines were ellipsized, use -l to show in full.
Pull tomcat image from Docker Hub
$ docker pull tomcat:latest
$ docker run -d --name tomcat-conatiner -p 8080:8080 tomcat:latest
Copy files in in and Refresh page
Manage Jenkins → Manage Plugins → Available We need docker server Credential So, This user must be part of docker group Password authentication → EC2 instance Manage Jenkins → Configure System Publish over SSH Test in Jenkins New Item → Enter an item name( Enter an item name: Deploy on Docker Edit Description: Deploy on Docker Source Code Management: git Build Post-build Actions Apply & Save Check Docker Server in Test Build Run image Check http://<Public_IP>:8080/webapp New Item → Enter an item name( Enter an item name: Deploy_on_Container Edit Description: Deploy_on_Container Source Code Management: git Build Post-build Actions Remove image and container of the same name within the target system (Docker server) before building The problem with this development is that the same Container & image is created and the build fails at the next build. Jenkins can not manage to solve these problems. So there are commercial products in the market. (Ansible is one of the solutions.) Source: https://www.udemy.com/course/valaxy-devops/$ docker exec -it tomcat-conatiner /bin/bash
webapps.dist
to webapps
/usr/local/tomcat/webapps.dist
$ cp -R * ../webapps
Integrating Docker with Jenkins
adduser
for docker server$ useradd dockeradmin
$ passwd dockeradmin
passwd: all authentication tokens updated successfully.
$ cat /etc/group
$ usermod -aG docker dockeradmin
$ id dockeradmin
uid=1001(dockeradmin) gid=1001(dockeradmin) groups=1001(dockeradmin),993(docker)
$ vi /etc/ssh/sshd_config
# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication yes
no
→ yes
$ service sshd reload
Jenkins Job to copy artifacts on to DockerHost
Maven Project
) → Edit DescriptionBuild Jenkins
Waiting for Jenkins to finish collecting data
[JENKINS] Archiving /var/lib/jenkins/workspace/Deploy_on_Docker/webapp/pom.xml to com.example.maven-project/webapp/1.0-SNAPSHOT/webapp-1.0-SNAPSHOT.pom
[JENKINS] Archiving /var/lib/jenkins/workspace/Deploy_on_Docker/webapp/target/webapp.war to com.example.maven-project/webapp/1.0-SNAPSHOT/webapp-1.0-SNAPSHOT.war
[JENKINS] Archiving /var/lib/jenkins/workspace/Deploy_on_Docker/server/pom.xml to com.example.maven-project/server/1.0-SNAPSHOT/server-1.0-SNAPSHOT.pom
[JENKINS] Archiving /var/lib/jenkins/workspace/Deploy_on_Docker/server/target/server.jar to com.example.maven-project/server/1.0-SNAPSHOT/server-1.0-SNAPSHOT.jar
[JENKINS] Archiving /var/lib/jenkins/workspace/Deploy_on_Docker/pom.xml to com.example.maven-project/maven-project/1.0-SNAPSHOT/maven-project-1.0-SNAPSHOT.pom
channel stopped
SSH: Connecting from host [jenkins]
SSH: Connecting with configuration [dockerhost] ...
SSH: Disconnecting configuration [dockerhost] ...
SSH: Transferred 1 file(s)
Finished: SUCCESS
$ su - dockeradmin
$ pwd
/home/dockeradmin/webapp/target
$ ls
webapp.war
Create Dockerfile & Run Image
/home/dockeradmin
$ vi Dockerfile
FROM tomcat:latest
MAINTAINER AR Shankar
COPY ./webapp.war /usr/local/tomcat/webapps
$ docker build -t devops-project .
$ docker images
devops-project latest b440b0c049a9 36 seconds ago 647MB
tomcat latest f796d3d2c195 35 hours ago 647MB
devops-project
image is created$ docker run --name devops-container -p 8080:8080 devops-project
Deploy a war file on Docker container using Jenkins
Maven Project
) → Edit Descriptioncd /home/dockeradmin; docker build -t devops-image .; docker run -d --name devops-container -p 8080:8080 devops-image;
Build Jenkins
Successfully built 769db991e33d
Successfully tagged devops-image:latest
fd61c46b1bb4621283a9ad0e4939a15675e1631577a1468e53e4505fe956f045
SSH: EXEC: completed after 22,820 ms
SSH: Disconnecting configuration [dockerhost] ...
SSH: Transferred 1 file(s)
Finished: SUCCESS