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.
How to Install Jenkins on Ubuntu
Install Jenkins Server Stable on CentOS 7
Install Jenkins Server on CentOS 8
This guide is specific for Docker guys who like containerizing applications and any infrastructure management tools.
Running Jenkins Server in Docker – Dependencies
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.
How to install Docker CE on Ubuntu / Debian / Fedora / Arch / CentOS
After the installation, you can confirm the version installed by running:
$ docker version
Client:
Version: 18.06.1-ce
API version: 1.38
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:24:51 2018
OS/Arch: linux/amd64
Experimental: false
Server:
Engine:
Version: 18.06.1-ce
API version: 1.38 (minimum version 1.12)
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:23:15 2018
OS/Arch: linux/amd64
Experimental: false
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 Wed 2018-10-31 15:32:59 PDT; 6min ago
Docs: https://jenkins.io/doc/
Main PID: 24964 (docker)
Tasks: 11 (limit: 1111)
CGroup: /system.slice/jenkins-docker.service
`-24964 /usr/bin/docker run --privileged --name jenkins-server --publish 80:8080 --publish 50000:50000 --volume /var/jenkins:/var/jenkins_h
Oct 31 15:33:23 ubuntu-01 jenkins[24964]: INFO: Obtained the updated data file for hudson.tasks.Maven.MavenInstaller
Oct 31 15:33:23 ubuntu-01 jenkins[24964]: Oct 31, 2018 10:33:23 PM hudson.model.AsyncPeriodicWork$1 run
Oct 31 15:33:23 ubuntu-01 jenkins[24964]: INFO: Finished Download metadata. 13,352 ms
Oct 31 15:33:25 ubuntu-01 jenkins[24964]: Oct 31, 2018 10:33:25 PM hudson.model.UpdateSite updateData
Oct 31 15:33:25 ubuntu-01 jenkins[24964]: INFO: Obtained the latest update center data file for UpdateSource default
Oct 31 15:33:25 ubuntu-01 jenkins[24964]: Oct 31, 2018 10:33:25 PM jenkins.InitReactorRunner$1 onAttained
Oct 31 15:33:25 ubuntu-01 jenkins[24964]: INFO: Completed initialization
Oct 31 15:33:25 ubuntu-01 jenkins[24964]: Oct 31, 2018 10:33:25 PM hudson.WebAppMain$3 run
Oct 31 15:33:25 ubuntu-01 jenkins[24964]: INFO: Jenkins is fully up and running
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.
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.
Next is to create an admin user account used to manage the Jenkins server.
After a successful installation, you’ll get to Jenkins Management console.
Enjoy automating your jobs with Jenkins. For more reading, check Official Documentation.