Wednesday, August 31, 2016

JENKINS inside DOCKER container - Building continous Integration

 Docker is an open-source project that automates the deployment of applications inside software containers, by providing an additional layer of abstraction and automation of operating-system-level virtualization on Linux

Jenkins: The leading open source automation server, Jenkins provides hundreds of plugins to support building, deploying and automating any project.

Docker is the open platform to build, ship and run distributed applications, anywhere. At the core of the Docker solution is a registry service to manage images and the Docker Engine to build, ship and and run application containers. Continuous Integration (CI) and Continuous Delivery (CD) practices have emerged as the standard for modern software testing and delivery. The Docker solution accelerates and optimizes CI/CD pipelines, while maintaining a clear separation of concerns between the development and operations teams.

 NOTE: Get 2.7.2 LTS (Long-Term Support) .war or the latest 2.20 weekly release .
---------------------------------------------------------------------

Step 1 : Search for a docker-io   after "yum update"

                      yum search docker

 NOTE:      docker.io is maintained by Ubuntu
                   docker-engine is maintained by Docker
 -----------------------------------------------------------------------------
cat /etc/yum.repos.d/docker.repo
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/6
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg

-----------------------------------------------------------------------
Step2 :  Get the information on docke-io

                   yum info docker-io
-----------------------------------------------------------------------
Step 3 :  Install docker

                yum install docker-io
-----------------------------------------------------------------------
Step 4:  Check  the rpm package installed

              rpm -qa | grep docker

or
              yum list installed | grep docker
             docker-io.x86_64                       1.7.1-2.el6                @epel
             #

---------------------------------------------------------------------
Step 5:  Start docker

              service docker start
----------------------------------------------------------------------
Step 6:  Check docker process

               ps -ef | grep docker
--------------------------------------------------------------------
Step 7:  Check docker version

           docker version
------------------------------------------------------------------------
Step 8:  Verify docker is installed correctly by running a test image in a container.

[root@xyz sachin]# docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker Hub account:
 https://hub.docker.com

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

[root@xyz sachin]#
-----------------------------------------------------

Step 9:   Get Jenkins Image

                docker pull jenkins

-------------------------------------------------------
Step 10:  Check the list of images

[root@xyz ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
jenkins             latest              6785d71c011c        2 weeks ago         715.3 MB
hello-world         latest              f0cb9bdcaa69        8 weeks ago         1.848 kB
[root@xyz ~]#
-------------------------------------------------------

Step 11 : Clean up all previous  containers :
[root@xyz sachin]# docker stop master1
master1
[root@xyz sachin]# docker rm master1
master1
[root@xyz sachin]# docker rm b5d566eb56a8
b5d566eb56a8
[root@xyz sachin]# docker rm 2aed2624515d
2aed2624515d
[root@xyz sachin]#

[root@xyz sachin]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@xyz sachin]#
----------------------------------------------------------------------
Step 12 : Remove the previously created Image

[root@xyz sachin]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
newtest             latest              6f0bffe3609e        22 hours ago        715.3 MB
jenkins             latest              6785d71c011c        2 weeks ago         715.3 MB
jenkins             2.7.2               6785d71c011c        2 weeks ago         715.3 MB
hello-world         latest              f0cb9bdcaa69        8 weeks ago         1.848 kB
[root@xyz sachin]# docker rmi newtest
Untagged: newtest:latest
Deleted: 6f0bffe3609e15741c0a8a5635ff2ca530a8af0e573a5690b3291ffd76f6a6f9
Deleted: f955dee8ef822202fb0f34b9479a5406b157fe7a41f54f4b3ca6cb5ecc82ff1e
[root@xyz sachin]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
jenkins             2.7.2               6785d71c011c        2 weeks ago         715.3 MB
jenkins             latest              6785d71c011c        2 weeks ago         715.3 MB
hello-world         latest              f0cb9bdcaa69        8 weeks ago         1.848 kB
[root@xyz sachin]#

---------------------------------------------------------------
Step 13:   Run the docker container


[root@xyz sachin]# docker run -p 8080:8080 --name=My_jenkins-master -d jenkins
961b31686b2026e67f94f910007c5ad590250aa0ba587a388bce4a784978dcae
[root@xyz sachin]#

[root@xyz sachin]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                               NAMES
961b31686b20        jenkins             "/bin/tini -- /usr/l   27 seconds ago      Up 25 seconds       0.0.0.0:8080->8080/tcp, 50000/tcp   My_jenkins-master 
[root@xyz sachin]#


[root@xyz sachin]# docker stop My_jenkins-master
My_jenkins-master
[root@xyz sachinpb]# docker rm My_jenkins-master
My_jenkins-master
[root@xyz sachinpb]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@xyz sachinpb]#

-----------------------------------------------------------------------
Step 14:  Create a dockerfile

[root@xyz sachin]# cat Dockerfile
FROM jenkins:2.7.2
MAINTAINER spb
ENV JAVA_OPTS="-Xmx8192m"
[root@xyz sachin]#

-------------------------------------
step 15: Build docker Image
[root@xyz sachin]#     docker build -t myjenkins .
Sending build context to Docker daemon 2.048 kB
Sending build context to Docker daemon
Step 0 : FROM jenkins:2.7.2
 ---> 6785d71c011c
Step 1 : MAINTAINER spb
 ---> Running in 2e9649b9d84c
 ---> 1b5e85ca936e
Removing intermediate container 2e9649b9d84c
Step 2 : ENV JAVA_OPTS "-Xmx8192m"
 ---> Running in 96ef7fc01890
 ---> 7225f6c3c55b
Removing intermediate container 96ef7fc01890
Successfully built 7225f6c3c55b
[root@xyz sachin]#

Check  your new build Image  at repo :

[root@xyz sachin]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
myjenkins           latest              7225f6c3c55b        12 seconds ago      715.3 MB
jenkins             2.7.2               6785d71c011c        2 weeks ago         715.3 MB
jenkins             latest              6785d71c011c        2 weeks ago         715.3 MB
hello-world         latest              f0cb9bdcaa69        8 weeks ago         1.848 kB
[root@xyz sachin]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@xyz sachin]#
------------------------------------------------

Step 16:  Run docker container  created from new image (myjenkins) :


[root@xyz sachinpb]# docker run -p 8080:8080 --name=spb_master -d myjenkins
ceafb4599eba6f09a56d211aef21622608a9f9e62c49ce6c85db5bbbfdcca257
[root@xyz sachinpb]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                               NAMES
ceafb4599eba        myjenkins           "/bin/tini -- /usr/l   10 seconds ago      Up 8 seconds        50000/tcp, 0.0.0.0:8080->8080/tcp   spb_master        
[root@xyz sachin]#


----------------------------------------------------

Step 17:  Get more information on newly built image  about  environment variables and other configurations  details.

[root@xyz sachin]# docker inspect ceafb4599eba
[
{
    "Id": "ceafb4599eba6f09a56d211aef21622608a9f9e62c49ce6c85db5bbbfdcca257",
    "Created": "2016-08-31T06:32:35.058410573Z",
    "Path": "/bin/tini",
    "Args": [
        "--",
        "/usr/local/bin/jenkins.sh"
    ],
    "State": {
        "Running": true,
        "Paused": false,
        "Restarting": false,
        "OOMKilled": false,
        "Dead": false,
        "Pid": 11921,
        "ExitCode": 0,
        "Error": "",
        "StartedAt": "2016-08-31T06:32:36.935174498Z",
        "FinishedAt": "0001-01-01T00:00:00Z"
    },
.
.
.
.
.

        "Env": [
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
            "LANG=C.UTF-8",
            "JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64",
            "JAVA_VERSION=8u102",
            "JAVA_DEBIAN_VERSION=8u102-b14.1-1~bpo8+1",
            "CA_CERTIFICATES_JAVA_VERSION=20140324",
            "JENKINS_HOME=/var/jenkins_home",
            "JENKINS_SLAVE_AGENT_PORT=50000",
            "TINI_VERSION=0.9.0",
            "TINI_SHA=fa23d1e20732501c3bb8eeeca423c89ac80ed452",
            "JENKINS_VERSION=2.7.2",
            "JENKINS_UC=https://updates.jenkins.io",
            "COPY_REFERENCE_FILE_LOG=/var/jenkins_home/copy_reference_file.log",
            "JAVA_OPTS=-Xmx8192m"

[root@xyz sachin]#
-----------------------------------------------------------------------------------------------------

Step 18 : To access the jenkin server , you need to unlock the Jenkins as shown below:

http://machinename.com:8080/
 
[root@xyz sachin]# docker exec ceafb4599eba cat /var/jenkins_home/secrets/initialAdminPassword
04c2ee3b737b4f9793ce24cc94034b9e
[root@xyz sachin]#

Organizations who leverage Docker as part of their continuous integration pipeline can expect to increase stability, reduce complexity, and increase the agility of their software development processes. Docker allows users to ensure consistent repeatable processes, while simultaneously
allowing for a reduction in the number of test images that need to be tracked and maintained. The lightweight nature of containers means they can be spun up faster, allowing for more rapid test cycles.
--------------------------------------------------------------------------------------------------------- 
References: 

1) https://engineering.riotgames.com/news/putting-jenkins-docker-container
2) https://github.com/jenkinsci/docker/blob/master/Dockerfile
3) https://docs.docker.com/engine/installation/linux/rhel/
4) https://github.com/maxfields2000/dockerjenkins_tutorial