Thursday, July 4, 2024
HomeOperating SystemsCentosRunning Jenkins Server in Docker Container with Systemd

Running Jenkins Server in Docker Container with Systemd

Jenkins is an open source automation server used to automate repetitive tasks that are often encountered in continuous integration and delivery of software. In this guide, we will see how you can run Jenkins server on a Docker container managed via Systemd init system.

If you would prefer running Jenkins on a CentOS 7 or Ubuntu 18.04 instance, use the following guides instead.

This guide is specific for Docker guys who like containerizing applications and any infrastructure management tools.

How To Run Jenkins Server in Docker Container

Running Jenkins Server on a Docker has few dependencies that you’ll need to satisfy.

  • Linux or macOS
  • Docker Engine installed and running
  • A user account with sudo privileges

Step 1: Install Docker Engine

Start by installing Docker engine on your base operating system. You can use our previous guide for docker installation.

After the installation, you can confirm the version installed by running:

$ docker version
Client: Docker Engine - Community
 Version:           20.10.21
 API version:       1.41
 Go version:        go1.18.7
 Git commit:        baeda1f
 Built:             Tue Oct 25 18:01:58 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.21
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.7
  Git commit:       3056208
  Built:            Tue Oct 25 17:59:49 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.9
  GitCommit:        1c90a442489720eec95342e1789ee8a5e1b9536f
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Step 2: Add jenkins user

Next is to add Jenkins system user to your server. This user will manage Jenkins service.

sudo groupadd --system jenkins
sudo useradd -s /sbin/nologin --system -g jenkins jenkins
sudo usermod -aG docker jenkins

Ensure this user is added to the docker group.

$ id jenkins
uid=999(jenkins) gid=998(jenkins) groups=998(jenkins),999(docker)

Pull the LTS docker image release of Jenkins

sudo docker pull jenkins/jenkins:lts

Confirm that the image download was successful.

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
jenkins/jenkins     lts                 9cff19ad8c8b        3 weeks ago         730MB

Step 3: Create a Jenkins Data directory & container

We need a persistent storage for Jenkins data to ensure that the data is made to remain intact and can, therefore, be re-used in the event that the container shuts down or crashes.

sudo mkdir /var/jenkins
sudo chown -R 1000:1000 /var/jenkins

Step 4: Create a Jenkins Container Systemd service

Create a new systemd service unit file for Jenkins.

sudo vim /etc/systemd/system/jenkins-docker.service

Add:

[Unit]
Description=Jenkins Server
Documentation=https://jenkins.io/doc/
After=docker.service
Requires=docker.service

[Service]
Type=simple
User=jenkins
Group=jenkins
TimeoutStartSec=0
Restart=on-failure
RestartSec=30s
ExecStartPre=-/usr/bin/docker kill jenkins-server
ExecStartPre=-/usr/bin/docker rm jenkins-server
ExecStartPre=/usr/bin/docker pull jenkins/jenkins:lts
ExecStart=/usr/bin/docker run \
          --name jenkins-server \
          --publish 8080:8080 \
          --publish 50000:50000 \
          --volume /var/jenkins:/var/jenkins_home \
          jenkins/jenkins:lts
SyslogIdentifier=jenkins
ExecStop=/usr/bin/docker stop jenkins-server

[Install]
WantedBy=multi-user.target

Reload systemd and start jenkins service

sudo systemctl daemon-reload
sudo systemctl start jenkins-docker

On checking the status, you should get a running message.

$ systemctl status jenkins-docker
jenkins-docker.service - Jenkins Server
     Loaded: loaded (/etc/systemd/system/jenkins-docker.service; disabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-10-30 00:56:54 EAT; 9s ago
       Docs: https://jenkins.io/doc/
    Process: 8962 ExecStartPre=/usr/bin/docker kill jenkins-server (code=exited, status=1/FAILURE)
    Process: 8967 ExecStartPre=/usr/bin/docker rm jenkins-server (code=exited, status=1/FAILURE)
    Process: 8972 ExecStartPre=/usr/bin/docker pull jenkins/jenkins:lts (code=exited, status=0/SUCCESS)
   Main PID: 8983 (docker)
      Tasks: 5 (limit: 9460)
     Memory: 9.7M
        CPU: 89ms
     CGroup: /system.slice/jenkins-docker.service
             └─8983 /usr/bin/docker run --name jenkins-server --publish 8080:8080 --publish 50000:50000 --volume /var/jenkins:/var/jenkins_home jenkins/jenkins:lts

Oct 30 00:57:02 jammy jenkins[8983]: *************************************************************
Oct 30 00:57:02 jammy jenkins[8983]: *************************************************************
Oct 30 00:57:02 jammy jenkins[8983]: *************************************************************
Oct 30 00:57:02 jammy jenkins[8983]: Jenkins initial setup is required. An admin user has been created and a password generated.
Oct 30 00:57:02 jammy jenkins[8983]: Please use the following password to proceed to installation:
Oct 30 00:57:02 jammy jenkins[8983]: 77917c89e52e4f6087c311d04023833d
Oct 30 00:57:02 jammy jenkins[8983]: This may also be found at: /var/jenkins_home/secrets/initialAdminPassword
Oct 30 00:57:02 jammy jenkins[8983]: *************************************************************
Oct 30 00:57:02 jammy jenkins[8983]: *************************************************************
Oct 30 00:57:02 jammy jenkins[8983]: *************************************************************

Step 5: Unlock Jenkins Server installation

Browse to the URL to access web installation wizard:

 http://[serverip|hostname]:8080

When you first access a new Jenkins instance, you are asked to unlock it using an automatically generated password.

unlock jenkins centos7 min

Get the password from

# cat /var/jenkins/secrets/initialAdminPassword 
eec851b6f1ff43ce9f374a5e50d82fad

Copy-paste the automatically-generated alphanumeric password into the Administrator password field and click Continue.

In the next page, install recommended plugins or plugins that suit your desired Jenkins usage. if not sure, select installation of recommended plugins.

jenkins install plugins min

Next is to create an admin user account used to manage the Jenkins server.

install jenkins centos7 set admin account min

After a successful installation, you’ll get to Jenkins Management console.

install jenkins arch linux 02 min

Enjoy automating your jobs with Jenkins. For more reading, check Official Documentation.

Also check:

Thapelo Manthata
I’m a desktop support specialist transitioning into a SharePoint developer role by day and Software Engineering student by night. My superpowers include customer service, coding, the Microsoft office 365 suite including SharePoint and power platform.
RELATED ARTICLES

Most Popular

Recent Comments