How to install Jenkins using Docker on CentOS using Digital Ocean
This is a beginner tutorial on installing Jenkins using Docker. In this tutorial, we will be doing the installation on a Linux distribution known as CentOS. The process will be similar in other Linux distributions like Ubuntu and Fedora. It will be cost-free if we do this walkthrough on our local machines using tools like VirtualBox or VMWare but in real-life scenarios, we would more like to use virtual machines on platforms like AWS, Google Cloud or Digital Ocean. That is what we will be doing here: using Digital Ocean as our PAAS.
If you still need to get a DO account, please go here to create an account. It's free for a new user for the first 3 months. For those who already have an account, let's jump in.
Let us set up our server
After setting up our account and project, we can create a Droplet. A virtual machine is referred to as a droplet on Digital Ocean. You can create a droplet by clicking the green Create button at the top of the page.
On clicking the Create button, you see other resources Digital Ocean offers. Click on droplets from the list.
Selecting Droplets takes you to the detail page where you can configure your virtual machine. In the region section, select the closest region to you and skip to the "Choose an image" section where you select CentOS. This is an important section because we are determining the configuration of our virtual machine. When we choose the size, we select Basic under the droplet type.
We can now select the CPU options that suit us. If you are following this tutorial for practice, then the $12 option is ideal to set up Docker and Jenkins.
After selecting our ideal droplet, we have to choose an authentication method. This is how we want to access the virtual machine from our local computer. We can use SSH or password. Using SSH is the preferred method because it is more secure.
When you select SSH Key, you are prompted to create a new SSH Key. Follow the modal instructions to create and apply your SSH Key. Name your new SSH Key and click Add SSH Key. Click on the Create droplet button. Once you click on the Create Droplet button, DO begins to set up your server and you can see the progress:
Once the process is completed, you can see the IP address to access the server from your local machine.
You are now done setting up your virtual machine on Digital Ocean.
Now let's access our server from our local machine and install all the dependencies required to run Jenkins with Docker.
Open up your terminal on a Linux OS or Mac and run the following command:
ssh root@ip_address
root will be the root user and the username you use to access the server. If it is the first time running the command for your ip_address, then you get a prompt to allow the host to be added to your allowed hosts for future connections:
The authenticity of host 'ip_address' can't be established.
ED25519 key fingerprint is SHA_Fingerprint.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'ip_address' (ED25519) to the list of known hosts.
Enter passphrase for key '/Users/username/.ssh/id_ed25519':
Activate the web console with: systemctl enable --now cockpit.socket
[root@ip_address ~]#
We now have access to our server and can start installing dependencies.
Run the following command to install basic dependencies:
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
Then run this command to add the Docker repository:
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
It is advisable to create a new user on your server and not use the root user. So we will create a new user, you can name this user any meaningful name you like. I'll be naming my user Jenkins.
useradd -m jenkins
The above command creates a new user and its subsequent directory. Now that we have a new user, let us create a password for the user.
sudo passwd jenkins
The above command will generate a prompt to enter your password. Your password will not be displayed while typing it so don't be alarmed, your keyboard is fine. LOL. Remember to change jenkins
to your selected username.
New password:
BAD PASSWORD: The password fails the dictionary check — it is based on a dictionary word
Retype new password:
passwd: all authentication tokens updated successfully.
Note the BAD PASSWORD prompt I get after entering my password. If you are comfortable with your password, continue to retype your password to complete the process.
To make our new user have superuser privileges, we make it a sudo user. We do this by running the following command:
sudo visudo
This opens up the vi text editor. Go to the bottom of the file and type i on your keyboard to insert text into the file. Type the following command from the text editor:
jenkins ALL=(ALL) NOPASSWD: ALL
Remember to change jenkins
to your selected username. Type the ESC key on your keyboard to go out of edit mode. Type :wq
and then press Enter to save and quit the file.
Now that we have our new user as a sudo user, let us switch to the new user. We do that with the following command:
su — {username}
To verify that your new user is now a root user, type the following command:
sudo whoami
root
You should get a response root
when you type in the first command from above.
Now we have our new user set up, let's install docker.
sudo yum install docker-ce
Type y (yes) for all prompts that come up during installation.
Start the docker service with the following command:
sudo systemctl start docker
To ensure your docker service always runs on boot up, run the following command:
sudo systemctl enable docker
Now, update user permissions to allow our new user to have access to run docker commands:
sudo usermod -aG docker jenkins
Remember to update jenkins
to your username.
Log out of your server and log back in to apply the permissions to the new user.
Now let us install docker compose to help us run multiple containers.
DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
mkdir -p $DOCKER_CONFIG/cli-plugins
curl -SL https://github.com/docker/compose/releases/download/v2.27.0/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose
The above command downloads the latest release of Docker Compose (from the Compose releases repository) and installs Compose for the new user (jenkins)
under the $HOME
directory.
Let's give execute permissions to the binary
chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose
Now let's verify that docker compose has been installed successfully.
docker compose version
Docker Compose version v2.27.0
We have our Docker and Docker Compose set up. It's time to download the Jenkins docker image.
sudo docker pull jenkins/jenkins
Add a folder called jenkins_home in the home folder so it can be used as the docker volume to store data.
mkdir jenkins_home
Create a file in the home directory called docker-compose.yml
touch docker-compose.yml
vi docker-compose.yml
Type i
to enter edit mode and add the following text in the newly created file. Remember to take note of the spaces and indentations. Yaml files are sensitive to indentations. When you are done, type :wq
to quit and save the file.
version: '3'
services:
jenkins:
container_name: jenkins
image: jenkins/jenkins
ports:
- "8080:8080"
volumes:
- $PWD/jenkins_home:/var/jenkins_home
networks:
- net
networks:
net:
In the same folder as your docker-compose file, run the following command to start your Jenkins server.
docker compose up
Please take note of the initial password from the logs after starting up Jenkins
jenkins |
jenkins | *************************************************************
jenkins | *************************************************************
jenkins | *************************************************************
jenkins |
jenkins | Jenkins initial setup is required. An admin user has been created and a password generated.
jenkins | Please use the following password to proceed to installation:
jenkins |
jenkins | fe314819873a4c0ba8867585a2a9842b
jenkins |
jenkins | This may also be found at: /var/jenkins_home/secrets/initialAdminPassword
jenkins |
jenkins | *************************************************************
jenkins | *************************************************************
jenkins | *************************************************************
You can now retrieve your server IP address, either from your terminal or your Digital Ocean dashboard and put that in the address bar of your browser followed by :8080
which is the port you assigned to Jenkins in the docker-compose.yml file.
You can get the password from the terminal after running the command docker compose up
for the first time or retrieve the password from /var/jenkins_home/secrets/initialAdminPassword
.
Add the password and click on the Continue button.
Click on the Install suggested plugins button to install the most useful plugins.
Once the plugins are completely installed, the page switches to a new page where you create the admin user.
Fill the fields and click on the Save and Continue button.
The next page shows you your full Jenkins server URL. Click the Save and Finish button to complete your installation and set-up.
Click on the Start using Jenkins button to get access to creating amazing CI/CD pipelines.
Congratulations, you have completed your Jenkins installation. I’ll share how to create different types of pipelines in the upcoming articles. I hope this was fun. Please let me know in the comments if you have any challenges while following the walkthrough.