Jenkins can be used to automate anything. Jenkins is an automation platform that allows you to build, test and deploy software using pipelines. One can run bash and python scripts to automate anything they need. Jenkins provides a web GUI where you can create jobs and customize all the functionality that you want such as source control management, pre and post build actions as well as build triggers. In our scenario, A developer commits some code to git repository. Jenkins server becomes aware of this commit and triggers the appropriate pipeline.
You can run Jenkins in a docker container or you can use a dedicated server just for Jenkins. In my setup, I am going to use a dedicated server because running Jenkins in a container requires more detailed configuration. Of course, you can run Jenkins in a container and still do Docker builds by using Docker-in-Docker (DinD), Docker socket passing, or sidecar container patterns.
There is a good written article about this issue that you can read. That article also recommends 3 solutions you can use. As I said each solution has its own disadvantages and I will use a dedicated Jenkins server not a container.
https://www.tiuweehan.com/blog/2020-09-10-docker-in-jenkins-in-docker/
Jenkins Installation on a Dedicated Ubuntu Server:
Jenkins requires Java 11 or higher. First install Java RunTime Environment on your server
sudo apt update -y
sudo apt install default-jre -y
java -version
Run the following to install the new signing keys. Beginning April 5, 2023, the Jenkins will use new repository signing keys for the Linux installation packages.
sudo install -m 0755 -d /etc/apt/keyrings
sudo wget -O /etc/apt/keyrings/jenkins-keyring.asc https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
echo "deb [signed-by=/etc/apt/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/" | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
Then install Jenkins and cat command diplays the initial jenkins admin password, copyand paste it somewhere. We will use to start installation.
sudo apt-get update
sudo apt-get install -y jenkins
sudo systemctl enable --now jenkins
sudo systemctl status jenkins
sudo journalctl -u jenkins -f
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Enter your jenkins server ipaddress with port 8080. Now you can enter the intial admin password to continue to jenkins installation. If Jenkins fails to start, check /var/log/jenkins/jenkins.log

Select Install suggested plugins

This will install most commonly used jenkins plugins on our server. We can add additional plugins later on after the installation completes.
After this screen, you will be asked to create first admin user.

This server is in my home lab but it wll be accessible by using http://jenkins.configland.com:8080. So I entered its domain url.
After we enter this url, we can complete and finish the jenkins installation.

Jenkins Plugins:
When we use Jenkins, we will need some specific plugins depending on what we want from Jenkins to do. In our jenkinsfile we will want Jenkins to connect DockerHub so we need to install docker pipeline plugin. Navigate to:
Dashboard > Manage Jenkins > Manage Plugins > Available Plugins > search "docker pipeline" > Download and restart

We need the following plugins, make sure you install them.
Docker
Docker Pipeline
Docker Compose Build steps
Jenkins Credentials:
Now we can enter DockerHub and Git Credentials to allow Jenkins to be able to connect our accounts.
Manage Jenkins > Credentials > Global > Add Credentails

Use Git personal Access Token in Password field. Fill all the fields and click Create.

We will also need a DockerHub credentials. Logon to DockerHub first. Go to your Account Settings and Click on New Access Token to create a Token.
Go back to Jenkins Credentials section andcreate another global credential just like we did for Git.

Check if your host machine has git.
git version
#If it does not have git, run the command below
sudo apt install git
Install Docker on Jenkins Server:
We will use docker and docker compose commands in Jenkins Pipeline.Therefore our host machine has to have Docker and DockerCompose. Go ahead and install them by running the following commands on the Jenkins Server's Terminal.
sudo apt-get install -y ca-certificates curl gnupg lsb-release
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# add user to the docker group (both jenkins user and your user)
sudo usermod -aG docker $USER
sudo usermod -aG docker jenkins
sudo systemctl restart jenkins
If this is your Production Jenkins server, make sure you consider the following point as well
- setting up HTTPS/SSL
- limiting admin console exposure
- backing up the
/var/lib/jenkinsdirectory (It is Jenkins’ home directory and contains all the essential configuration and data Jenkins needs to function) - using proper firewall rules.