Hey all, In this blog, we're going to see what is Jenkins why it is used in the IT world and how to set up Jenkins on a local machine using docker
Let's get started...
Introduction:
If you are a software developer or a DevOps engineer, you might have heard of Jenkins. But what is Jenkins exactly, and how does it work? In this blog post, we will see a brief introduction to Jenkins and its features.
What is Jenkins:
Jenkins is an open-source automation server that helps you build, test and deploy software. It is written in Java and runs on various platforms such as Windows, Linux and macOS. Jenkins supports various version control tools such as Git and can execute different types of scripts and commands. Jenkins also has a rich ecosystem of plugins that extend its functionality and integrate with other tools.
One of the main features of Jenkins is its ability to create and manage pipelines.
A pipeline is a sequence of steps that define the workflow of your software delivery process. For example, a pipeline can consist of building your code, running tests, performing code analysis, deploying to a staging environment and releasing to production.
You can create pipelines using the Jenkins user interface or by writing a Jenkins file, which is a text file that uses Groovy syntax to describe the pipeline.
Jenkins pipelines enable you to implement continuous integration and continuous delivery (CI/CD) practices in your software development lifecycle.
CI/CD is a methodology that aims to deliver software faster and more reliably by automating the integration, testing and deployment stages.
By using Jenkins pipelines, you can ensure that your code is always in a deployable state and that any errors or bugs are detected early.
Setting up Jenkins server on local machine using docker container:
So here, we are going to use a docker container to set up the Jenkins server on our local machine for that we need a docker file to create an image and to run a container
before setting up the Jenkins server on a local machine, we should need some prerequisites to be available handy
Pre-requisite:
Docker should be installed in the local machine prior ⇾ Link to refer
Docker compose file to spin up the Jenkins server in our local machine
Steps to use the docker file:
- clone the repository - Link
Docker file:
version: '3.7'
services:
jenkins:
image: jenkins/jenkins:lts
privileged: true
user: root
ports:
- 8083:8080
- 50003:50000
container_name: my-jenkins-3
volumes:
# Create a folder in your local machine in the name of jenkins_data(user defined name you can give whatever name you want)
# and copy the path of it and paste it like below
# Example: - [your jenkins_data path]:/var/jenkins_home
- D:\jenkins\gova\Session\jenkins_data:/var/jenkins_home
- /var/run/docker.sock:/var/run/docker.sock
Explanation:
version: '3.7'
: This specifies the version of the Docker Compose file format being used.
services
: This section defines the services to be created.
jenkins
: This is the name of the service being defined.
image
: jenkins/jenkins:lts: This specifies the Docker image to be used for this service. The jenkins/jenkins:lts image is the Long-Term Support (LTS) release of Jenkins.
privileged: true
: This flag gives the container full access to the host system.
user
: root: This sets the user of the container to root.
ports
: This section maps the ports of the container to the ports of the host machine.
8083:8080
: Maps the Jenkins web interface to port 8083 on the host machine.
50003:50000
: Maps the Jenkins agent communication port to port 50003 on the host machine.
container_name
: my-jenkins-3: This specifies the name of the container.
volumes
: This section defines the volumes to be mounted in the container.
D:\jenkins\gova\Session\jenkins_data:/var/jenkins_home
: Maps the jenkins_data directory on the host machine to the /var/jenkins_home directory in the container. This directory will store the Jenkins configuration and data./var/run/docker.sock:/var/run/docker.sock
: Mounts the Docker socket on the host machine to the Docker socket in the container. This allows Jenkins to interact with the Docker daemon running on the host machine.
When you run docker-compose up with the above Docker Compose file, it will create a Docker container based on the jenkins/jenkins:lts image
The container will run in the foreground and display Jenkins logs in the console. Once the container is up and running, you can access the Jenkins web interface at
http://localhost:8083
to start configuring your Jenkins instance.
Now open the repository in your local machine inside that repository open the terminal or command prompt, then
run the below command to start
docker-compose up
- it will pull the image from the public registry and create a container in our local machine in the name of
my-jenkins-3
at the end of the process we will get the initial admin password
once you received the password in terminal/cmd then hit localhost:8083
in your browser to access this Jenkins
Paste the password that you received in Terminal/cmd or else we can manually search it in our Jenkins data folder in this picture itself you see the path to find the initial password [like that you will get the path for your folder]
get that and paste it their click continue
Click Install Suggested plugins to install all the required plugins for Jenkins
It will ask you to create a new user create it then save and continue at the end you will get the Jenkins dashboard page
after restarting your machine sometimes the container will be stopped so run the below command to start the container whenever you are going to work on Jenkins it's a good practice to cross-check whether the container running in the background or not
to start the container, use the below command
docker start my-jenkins-3
to stop the container, use the below command
docker stop my-jenkins-3
To auto-start the Jenkins whenever you start or restart your machine, run the below-mentioned command
docker update --restart=always my-jenkins-3
docker update --restart unless-stopped my-jenkins-3
Both commands update a Docker container named "my-Jenkins-3" and set it to automatically restart in case of any failures.
The first command uses the "--restart=always" flag, which instructs Docker to always restart the container, even if it was stopped manually.
The second command uses the "unless-stopped" option, which tells Docker to restart the container unless it was explicitly stopped by the user.
I hope you like this blog post please follow me in all the platform
Here is my github repo where i stored all of my devops project do star and fork as well as follow me on github for more updates