In this project, I have achieved containerization-within-container. Yes, you read it right, Containerization-within-container. Now you are wondering that isn't it possible to do so ? So, Yes dear it is possible to do and here I will teach you the step-by-step procedure of how I build this interesting project.
- First, we will create a Jenkins image on our own using Dockerfile.
- When we launch this image, it should automatically starts Jenkins service in the container.
- Create a job chain of job1, job2 and job3 using a build pipeline plugin in jenkins.
- Job1 : Pull the Github repo automatically when some developers push repo to Github.
- 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 HTML, then Jenkins should start the container that has HTML already installed ).
- Job3 : Test your app if it is working or not.
- If not working, then send email to developer with error messages.
-
Create a folder/dir with any name you want say '/ws' on your system.
-
Create a file must be named as 'Dockerfile' in that folder/dir.
-
Open the Dockerfile file and write the following commands in it:-
After writing the command, for building the image using dockerfile you need to write the following command-
docker build -t jenkins-docker-os:v1.0 /ws (dir path)
This will create your own customize jenkins image having docker configured in it or you can pull my jenkins image from My Docker Hub Account.
After pulling or building the image, for using it we need to launch the image. For launching the os, write the following command-
docker run -dit -p 1234:8080 -v /:/host -v /var/run/docker.sock:/var/run/docker.sock --name c-in-c deepika1999/jenkins-docker-os:v1.0
This will launch your os and you can check it by typing docker ps
Go to your browser and type-
YourIP:1234
This will take you to the page of jenkins unlock, unlock your jenkins and then login to the Jenkins.
After login, create 3 jobs using build pipeline plugin in Jenkins.
-
JOB1- GITHUB PULL:- When some Dev commits the code, through triggers it will automatically push to Github and trigger the Job1 in Jenkins which will go to Github repo and downloads all the code from there and copy the data to some folder. For copying, we are making some folder so write the below cmd in the execute shell of your jenkins Job 1.
sudo mkdir /c-in-c
sudo cp -rvf * /c-in-c
-
JOB2- LAUNCH CONTAINER:- In the execute shell of Job 2, Write the following script-
if sudo cat /c-in-c/Website/index.html | grep html
then
if sudo docker ps | grep html-server
then
sudo docker stop html-server
sudo docker rm -f html-server
sudo docker run --privileged -d -it -p 4321:80 -v /c-in-c:/var/www/html --name html-server vimal13/apache-webserver-php:v1
else
sudo docker run --privileged -d -it -p 4321:80 -v /c-in-c:/var/www/html --name html-server vimal13/apache-webserver-php:v1
fi
else
echo "Code out of the context"
fi
-
JOB3- TESTING:- In the execute shell of Job 3, write the following script-
export status=$(curl -o /dev/null -s -w "%{http_code}" YOUR_IP:4321/Website/)
if [ status==200 ]
then
exit 0
else
exit 1
fi
After running all the jobs with success, type in your browser as- YOUR_IP:4321/Website/index.html
and you will see the output as below picture.
I hope you understood this project and like the little effort of mine in explaining you about this concept of Containerization-within-Container. If you still left with any queries or facing any issues while building the same then feel free to ping me on my LinkedIn Profile.
Also if you want that within one click your all of the work to be done or something like pre-created version of this project so here I have something for you. Go to My DockerHub Account and pull this image Docker-in-Docker. Here everything is pre-created, you just need to follow the README file of this image to access it.