Surya.dev
Published on

Running Jenkins in Docker and VM

Authors

Jenkins is a popular open-source tool for automating software development processes, and using Docker to install Jenkins has become a popular choice for many developers. Docker allows you to run Jenkins in a containerized environment, making it easier to manage and scale your Jenkins instance. — Wikipedia 1

In this article, we will walk you through the steps to install Jenkins using Docker.

Image jenkins with docker

Prerequisites

Before you can install Jenkins using Docker, you will need to have the following:

  • A system with Docker installed. You can download Docker from the official website.

Understanding Jenkins in a Docker compose.yaml File

Docker Compose is a tool for defining and running multi-container Docker applications. In Compose, you use a YAML file to configure your application's services. Then, you create and start all the services from your configuration by running a single command.

# docker-compose.yml
services:
  jenkins:
    image: jenkins/jenkins:lts # :latest (weekly), :lts
    ports:
      - "8080:8080"
    volumes:
      - jenkins_home:/var/jenkins_home
    restart: unless-stopped

volumes:
  jenkins_home:

  # FYI https://www.jenkins.io/doc/book/installing/docker/

Next step you can running in terminal command docker compose up

Access the Jenkins Dashboard

Once the Jenkins container is up and running, you can access the Jenkins dashboard by visiting http://localhost:8080 in your web browser. You will be prompted to enter a password, which you can find in the Jenkins logs.

docker logs [container_id]
#OR
docker logs logs

In the logs, you will see a line similar to the following:

Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:

[password]

Enter the password in the prompt and follow the instructions to complete the initial setup of Jenkins.

Setup Jenkins with terminal

docker pull jenkins/jenkins:lts

docker run -p 2119:8080 --name namecontainer jenkins/jenkins:lts
#OR
docker run -p 8080:8080 jenkins/jenkins:lts


Running Jenkins on Virtual Machine/local

Colleagues can use a VPS from a web hosting provider, or can use a Virtual Machine Manager such as VirtualBox on a laptop.

Software Requirements

Installation JAVA 11

$ sudo apt update
$ apt install openjdk-11-jre-headless -y

check version java: java --version

#OUTPUT
root@iZt4nir2b2o938u2m5xms1Z:~# java --version
openjdk 11.0.21 2023-10-17
OpenJDK Runtime Environment (build 11.0.21+9-post-Ubuntu-0ubuntu120.04)
OpenJDK 64-Bit Server VM (build 11.0.21+9-post-Ubuntu-0ubuntu120.04, mixed mode, sharing)

Installing Git

$ sudo apt-get install git

Installation of Jenkins Packages

  1. The install guide instructions to install the GPG public key on Debian and Ubuntu are:
$ curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null

$ echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null

# FYI https://www.jenkins.io/blog/2023/03/27/repository-signing-keys-changing/
  1. Update repo and install jenkins
$ sudo apt update
$ sudo apt install jenkins -y

Output:

root@iZt4nir2b2o938u2m5xms1Z:~# apt install jenkins -y

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  jenkins
0 upgraded, 1 newly installed, 0 to remove and 8 not upgraded.
Need to get 85.8 MB of archives.
After this operation, 86.6 MB of additional disk space will be used.
Get:1 https://pkg.jenkins.io/debian binary/ jenkins 2.440 [85.8 MB]
Fetched 85.8 MB in 8s (10.8 MB/s)                                                                                               
Selecting previously unselected package jenkins.
(Reading database ... 116171 files and directories currently installed.)
Preparing to unpack .../archives/jenkins_2.440_all.deb ...
Unpacking jenkins (2.440) ...
Setting up jenkins (2.440) ...
Created symlink /etc/systemd/system/multi-user.target.wants/jenkins.service → /lib/systemd/system/jenkins.service.
Processing triggers for systemd (245.4-4ubuntu3.22) ...
  1. Checking Jenkins Status: Use systemctl command to check Jenkins service status in terminal
$ sudo systemctl status jenkins
#Output
● jenkins.service - Jenkins Continuous Integration Server
     Loaded: loaded (/lib/systemd/system/jenkins.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2024-01-14 22:51:40 CST; 3min 13s ago
   Main PID: 17108 (java)
      Tasks: 44 (limit: 4631)
     Memory: 1.2G
     CGroup: /system.slice/jenkins.service
             └─17108 /usr/bin/java -Djava.awt.headless=true -jar /usr/share/java/jenkins.war --webroot=/var/cache/jenkins/war -->
  1. Starting Jenkins:
# start jenkins
$ sudo systemctl start jenkins

# stop jenkins
$ sudo systemctl stop jenkins

# restart service jenkins
$ systemctl restart jenkins
  1. Optional: Aktivasi log jenkins
  • Edit the Jenkins service configuration file:

    $ systemctl edit --full jenkins.service
    
    • Uncomment JENKINS_LOG

      Environment="JENKINS_LOG=%L/jenkins/jenkins.log"
      
    • Uncomment JENKINS_LOG and change true

      Environment="JENKINS_ENABLE_ACCESS_LOG=true"
      
    • Restart jenkins

      $ sudo systemctl restart jenkins
      
  • Check the contents of the /var/log/jenkins/jenkins.log log file to ensure that logging is active

🥳 Congratulation

Access the Jenkins webui via a web browser using port 8080 Image unlcok jenkins

Footnotes

  1. The above quote is excerpted from Wikipedia, Jan 12, 2024.