End to End INTEGRATION Of Docker, Jenkins, Github, Git, Redhat
Ankit Kumar
DevOps| Terraform | Linux |Azure | Azure DevOps | AWS | RH294( Ansible) | Python | Docker | Kubernetes | Grafana | ELK | Prometheus
DevOps task2
Let us first understand what we are going to do in this task.
Task description
1. Create container image that’s has Jenkins installed using docker file
2. When we launch this image, it should automatically start the Jenkins service in the container.
3. Create a job chain of job1, job2, job3 and job4 using build pipeline plugin in Jenkins
4. Job1: Pull the Github repo automatically when some developers push the repo to Github.
5. Job2: By looking at the code or program file, Jenkins should automatically start the respective language interpreter install image container to deploy code ( eg. If code is of PHP, then Jenkins should start the container that has PHP already installed )
6. Job3: Test your app if it is working or not.
7. Job4: if the app is not working, then send an email to the developer with error messages.
8. Create One extra job job5 for monitor: If the container where the app is running. fails due to any reason then this job should automatically start the container again.
Let us begin our journey
Create a container image that’s has Jenkins installed using a Dockerfile.
Before we start our job we have to create a Jenkins image using Dockerfile. As we launch the image your Jenkins server start. This means as we launch the docker image, your Jenkins service starts.
All the codes related to the creation of the docker images are present in this screenshot.
Now once your docker image is ready we start to perform our task.
Set Jenkins after launch
Job1
Pull the Github repo automatically when some developers push the repo to Github. The job says that as you some developer share the code/push the code to the GitHub, then Jenkins will go and download the code. This is possible due to the remote trigger we use in the job1. Due to the remote Jenkins will know that some things change in your particular Github repository.
Once the Jenkins will pull the code, then we execute some code in the shell.
This code tells that, go and remove the folder(folder name = task2) if present, and create one folder and after the folder has been created, copy the code that we pull from the Github.
But we have to configure ssh because we want to create a copy of the folder that should present in the base operating system.
For ssh configuration, we have to install ssh plugins. After plugin install got to the manage credentials and set a credential for your base operating system, go to the manager Jenkins>set ssh.
Then configure job1
Job2
By looking at the code or program file, Jenkins should automatically start the respective language interpreter install image container to deploy code ( eg. If code is of PHP, then Jenkins should start the container that has PHP already installed)
The job tells that by looking to the code or program file, Jenkins will automatically launch the respective interpreter inside the docker.
Now the main, concepts start, our Jenkins is launched inside the docker and again we launch the docker where our interpreter is launch by looking to the code or program file.
Means docker inside docker. Now, this messy, because as we launch the docker inside docker, it could not able to launch and fail.
But before we understand why it fails to launch docker inside docker, let’s check some understand some basic concepts od docker.
Whenever we launch the docker, it shares the resources(Ram, CPU, Hardisk, etc) of the base operating system where it launches. This sharing is possible because the base operating system has a socket, which has capabilities to share its own resources to the docker.
But if we look into the docker this socket is not present. And this is the main cause of the failure of the docker inside docker.
When we try to launch docker inside docker, and due to the unavailability of the socket in docker, it is not able to launch docker inside docker.
But according to the requirement, we have to launch a docker inside the docker. So to solve this socket problem we mount the socket of the base operating system to the inside docker container at the time of launching the container.
So what we did basically is, we mount the socket of the base operating system to the docker and your problem of launching docker inside docker will shout out.
Now,
We can perform task2 quite easily with no error.
The required code for performing the job2 is given in the screenshot
the code is totally based on the conditional & loops.
JOB3
Test your app if it is working or not.
Once our code is deployed to the website, Jenkins will check either the deploy code is working properly or not.
Job4: if the app is not working, then send an email to the developer with error messages.
We enable the email notification from then Jenkins so that when either of the jobs fails it will send you an email and tell your job is failed.
But I would like to tell you one more thing before configuring email, the password column that is visible in the screenshot, you have to put the app password of the email, not your email password.
How to create an app password for the email?
Create & use App Passwords
Note: If you use 2-Step-Verification and are seeing a "password incorrect" error when trying to access your Google Account, an App Password may solve the problem.
Go to your Google Account.
On the left navigation panel, choose Security.
On the "Signing into Google" panel, choose App Passwords. If you don’t see this option:
2-Step Verification is not set up for your account.
2-Step Verification is set up for security keys only.
Your account is through work, school, or other organization.
You’ve turned on Advanced Protection for your account.
At the bottom, choose the Select app and choose the app you’re using.
Choose Select device and choose the device you’re using.
Choose Generate.
Follow the instructions to enter the App Password. The App Password is the 16-character code in the yellow bar on your device.
Choose Done.
Once the password is created put the password created in the password column.
Job 5: Create One extra job job5 for monitor: If the container where the app is running. fails due to any reason then this job should automatically start the container again.
The job will monitor our launched container and due to any reason, if the container will fail, it will launch the container.
Your task is completed.
Dashboard view
OUTPUT
All the plugins installed
- build pipeline
- email extension
- ssh
- Github
This task is the integration of the docker, Jenkins, Github, git, Redhat.
Github Link: https://github.com/ankiiitt/DEVOPS-TASK2
Thank You for reading & Viewing the article.
DevOps Engineer at SquareOps | AWS Certified Solution Architect
4 年Great work