MLops for beginners
Day14 — Containerized Jenkins
How to containerize jenkins. How to do docker inside docker (dind).
In my last 2 articles, I gave a introduction to containers and jenkins. This article is a continuation for how to install jenkins inside a container to manager other containers.
I’ll break our complete task into several small nuggets to make things easier.
- start your docker service (if not already started)
- check the status
Now this status log gives something noteworthy, API listen on /var/run/docker.sock
. This message tells that our docker API is listening on this socket for docker commands. So if anybody gives commands on this socket then those will be listened by my RHEL8 docker engine API.
Now let’s get back to our job. Create a container. I’ll use Dockerfile here to create our jenkins image.
We already know that docker images are just a group of layers that stack together to make a complete image.
So my first step is simple.
We can see here that the image ID and size for both the images is same. That means myjenkins:v1 is using the same layer as centos:latest.
If you face such issue in building image then it is probably due to the firewall rules.
These commands will solve the errors. (check man pages for more info)
After this, you,ll get another error.
This error says that yum is not able to find jenkins in its pre configured repository. So I need to configure jenkins repository in order to make it work.
We have created a myjenkins:v1 image. Also we can notice that the image ID is changed and size is gradually increased.
Our goal for this article is “Docker inside Docker”. For that, I’ll install docker and required library in my image.
In last line of Dockerfile, I have added command to install docker-ce
, containerd.io
, and git
and python
(for future use).
I’ll tag my new image as myjenkins:v2
.
You can notice that new packages are being installed over the last layer present (from myjenkins:v1). This is one of the great features of docker called Layer Caching.
My step 6 has failed and the error is quite obvious that the packages were not found in the pre configured repo. So we’ll have to configure a repo for this too.
My plan now is to copy this docker.repo inside the docker using ADD
command and this will configure my repo for docker.
Now time to create a container and check Docker inside Docker.
Now we again see something about the socket. Now the hacking part comes in. I’ll mount base OS docker socket to my container. Then whatever commands I’ll give from my container they will be executed by my base OS docker engine.
Great!! We have done docker inside docker!
Let’s do this with jenkins now. For this we’ll need to add few layers in out Dockerfile.
- Change Dockerfile
- Build new image
- Create container
- open in browser
- check for logs
- Somewhere in the logs
- Select plugins ( I am selecting only git)
- Create job
- Execute shell command
- Job created
- Build Job and check console output
- After some time (Depending upon your internet speed and availability of httpd:latest image)
- Check on Redhat OS for new contianer.
We now successfully created a new container from our jenkins container.