CI is the process of automatically the build and testing the code when the code is committed into the version control system. CD is the extension of CI, the process to build, test and deploy into the production environment.
To start with CI/CD, the first step is to set up the required CI/CD tools and integrate it. So that, we can build and deploy the software seamlessly. In this post, we will see how to setup Jenkins in a containerized environment for creating the CI/CD pipeline. Jenkins is distributed as WAR files, native packages, installers and Docker images.
Steps to install Jenkins using Docker
1. Install docker
Docker installation is the pre-requisite for this Jenkins set up. Visit the official docker site and install the docker based on the platform you are using.
2. Create a Dockerfile to create a custom Jenkins docker image
First, we are referring the jenkins/jenkins:alpine image as the base image and using
an alpine version of the image, which is the slimmest version. And, switching to root
user to install the required packages.
FROM jenkins/jenkins:alpine
Next, we are installing docker inside the Jenkins image. So, we can run and
execute docker commands in your Jenkins job.
RUN \
echo "**** install runtime packages ****" \
&& apk add --no-cache \
docker
In order to run the docker commands inside Jenkins job, we have to assign the user
Jenkins to the group docker because all the jobs in Jenkins are executed as a Jenkins
user.
RUN groupmod -g 801 docker \
&& usermod -aG docker jenkins
If you are going to use ansible for application deployment, then ansible installation
is required.
RUN apk add --no-cache ansible python3
Expose the Jenkins home directory and set the default user for this image
VOLUME /var/lib/jenkins
USER jenkins
Finally, dockerfile will looks like below
Dockerfile
FROM jenkins/jenkins:alpineUSER root# set the version labelLABEL maintainer="Navin"# Update the Environment variablesENV JENKINS_HOME /var/lib/jenkinsENV COPY_REFERENCE_FILE_LOG $JENKINS_HOME/copy_reference_file.log# Install latest Docker CE binariesRUN \echo "**** install runtime packages ****" \&& apk add --no-cache \docker \&& echo "**** cleanup ****" \&& rm -rf \/tmp/*# Modify the UID/GID of user 'jenkins'# Modify the Jenkins Home directory, also modifying the ownership of itRUN \apk --no-cache add shadow \&& groupmod -g 800 jenkins \&& usermod -u 800 jenkins \&& mkdir "${JENKINS_HOME}" \&& usermod -d "${JENKINS_HOME}" jenkins \&& chown jenkins:jenkins "${JENKINS_HOME}"# Adding the user 'jenkins' to docker groupRUN groupmod -g 801 docker \&& usermod -aG docker jenkinsRUN \echo "**** install ansible and python ****" \&& apk add --no-cache ansible python3# Expose the files/folderVOLUME /var/lib/jenkins# set default userUSER jenkins
3. Create a docker-compose file
Once the dockerfile is ready, we can use docker command to build and run the image or
we can go with docker-compose. Here, we are using docker-compose to build and run the
image.
docker-compose.ymlversion: '3'services:jenkins:build: .container_name: jenkinsports:- "8080:8080"- "50000:50000"restart: alwaysvolumes:- "jenkins:/var/lib/jenkins"- "/var/run/docker.sock:/var/run/docker.sock"volumes:jenkins: {}
You have configured the docker-compose file to run the Jenkins container in port 8080.
Port 5000 is mapped for Jenkins slave configuration. As of now, we will leave this
slave configuration part.
To execute the docker-compose commands, docker-compose needs to be installed in the
host machine because it's a separate project which is written in python. For
installation, check this link.
Once it is installed, execute the below command to build and run the container in
detached mode.
docker-compose up -d
4. Access the Jenkins
By visiting the Jenkins URL in the following format {IP or Domain}:8080.
For eg: www.example.com:8080. You will get Jenkins Unlock page where you have to enter
the admin password which is automatically generated by Jenkins and it is saved under
the /secrets directory of jenkins_home
Once you have entered the password, it will redirect to the plugins installation page.
You can proceed further by selecting the Install Suggested Plugins option. This option
will install around 15+ plugins.
Plugin installation will take 5 to 10 minutes, it depends on the host machine
specification and bandwidth. After plugin installation, you can access the Jenkins
home page and create a job in Jenkins
Comments
Post a Comment