bookmark_borderDeploy on Docker using Jenkins

  1. Prerequisites
  2. Installing Docker on EC2(Amazon Linux Server)for community version of docker
  3. Integrating Docker with Jenkins
  4. Jenkins Job to copy artifacts on to DockerHost
  5. Build Jenkins
  6. Create Dockerfile & Run Image
  7. Deploy a war file on Docker container using Jenkins
  8. Build Jenkins

Prerequisites

  1. EC2 Instance
    • With Internet Access
    • Security Group with Port 8080 open for internet
    • Jenkins Server
  2. 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

$ docker exec -it tomcat-conatiner /bin/bash

Copy files in webapps.dist to webapps

in /usr/local/tomcat/webapps.dist

$ cp -R * ../webapps

and Refresh page

Integrating Docker with Jenkins

Manage Jenkins → Manage Plugins → Available

We need docker server Credential So, adduser for docker server

$ useradd dockeradmin
$ passwd dockeradmin
passwd: all authentication tokens updated successfully.
  • Password setting is needed.

This user must be part of docker group

$ cat /etc/group
  • Check group
$ usermod -aG docker dockeradmin
$ id dockeradmin
uid=1001(dockeradmin) gid=1001(dockeradmin) groups=1001(dockeradmin),993(docker)

Password authentication → EC2 instance

$ vi /etc/ssh/sshd_config
# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication yes
  • PasswordAuthentication noyes
$ service sshd reload
  • service sshd reload

Manage Jenkins → Configure System

Publish over SSH

  • SSH server Name: dockerhost Hostname: host IP Username: dockeradmin Password: –

Test in Jenkins

Jenkins Job to copy artifacts on to DockerHost

New Item → Enter an item name(Maven Project) → Edit Description

Enter an item name: Deploy on Docker

Edit Description: Deploy on Docker

Source Code Management: git

Build

  • Goals and options: clean install package

Post-build Actions

  • Send build artifacts over SSH example) Name: dockerhost Transfers Transfer Set
    • Source files: webapp/target/*.war
    • Remove prefix: webapp/target/
    • Remote directory : .

Apply & Save

Build 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

Check Docker Server

$ su - dockeradmin
$ pwd
/home/dockeradmin/webapp/target
$ ls
webapp.war

Create Dockerfile & Run Image

in /home/dockeradmin

$ vi Dockerfile
FROM tomcat:latest

MAINTAINER AR Shankar

COPY ./webapp.war /usr/local/tomcat/webapps

Test Build

$ 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

Run image

$ docker run --name devops-container -p 8080:8080 devops-project

Check http://<Public_IP>:8080/webapp

Deploy a war file on Docker container using Jenkins

New Item → Enter an item name(Maven Project) → Edit Description

Enter an item name: Deploy_on_Container

Edit Description: Deploy_on_Container

Source Code Management: git

Build

  • Goals and options: clean install package

Post-build Actions

  • Send build artifacts over SSH example) Name: dockerhost Transfers Transfer Set
    • Source files: webapp/target/*.war
    • Remove prefix: webapp/target/
    • Remote directory : .
    • Exec command:
    cd /home/dockeradmin; docker build -t devops-image .; docker run -d --name devops-container -p 8080:8080 devops-image;

Remove image and container of the same name within the target system (Docker server) before building

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

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/

ANOTE.DEV