Install and Run Jenkins with Systemd and Docker on Ubuntu 20.04 - Best Method ?

Repetitive tasks are usually tedious and end up taking up a lot of your time and energy. Over time, multiple automation tools have been developed to help alleviate the hassle of executing repetitive jobs. When you download and configure Jenkins using docker, you have to use manual command to manage the process. It becomes quite unsafe and complicated. So, we will manage it using systemd. Actually, it is the system and service manager for Linux operating systems. It features on-demand starting, enabling, restarting, and stopping of daemons, logging, etc.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Linux Systemd queries.

In this context, we shall look into how to download the Jenkins package from docker hub repository and manage it using systemd manager on any Linux distribution such as Ubuntu, centos, Debian, Redhat etc which support systemd.

How to Install Jenkins with Systemd ?

For the configuration, you need to run a process we need users. So, we create a system group, add a system user to the group. As you don't need to login we provide no login shell. We have here given the name 'devops', you can give of your choice:

$ sudo groupadd --system devops
$ sudo useradd -s /sbin/nologin --system -g devops devops

During docker installation, a user docker was added by default. So to run our process inside docker, let’s add 'devops' user and your current login user to the docker group:

$ sudo usermod -aG docker devops
$ sudo usermod -aG docker $USER

We need a directory to map container volume to the host. So, let’s create a directory and give current user ownership in the directory:

$ sudo mkdir -p /data/jenkins
$ chown -R $USER:$USER /data/jenkins

You can now verify user 'devops' using the following command:

$ id devops

Now it's time to create a service. The service files are stored inside /etc/systemd/system/ So, create a file ending with .service as shown below:

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

Add following content in the file. The following configuration will create a service name docker-jenkins. It will simply pull the latest version of jenkins image from docker hub and run the container. It will also map port 8080 and 50000 in the host server, which are needed to access jenkins service. As defined in the Unit section below, it requires docker.service to be executed successfully. If you have different users, mount points change accordingly:

Description=My Jenkins Server
ExecStartPre=-/usr/bin/docker kill jenkins-server
ExecStartPre=-/usr/bin/docker rm jenkins-server
ExecStartPre=/usr/bin/docker pull jenkins/jenkins
ExecStart=/usr/bin/docker run \
--name jenkins-server \
--publish 8080:8080 \
--publish 50000:50000 \
--volume /data/jenkins:/var/jenkins_home \
ExecStop=/usr/bin/docker stop jenkins-server

Reload the service file in the systemd daemon, using the following command:

$ sudo systemctl daemon-reload

Start the docker service using the command below:

$ sudo systemctl start docker-jenkins

Systemd also provides default logging service in syslog. You can see the log in the following location:

$ sudo tail -f /var/log/syslog

Now, you can start the service after the server reboots using the 'enabling' feature of systemd manager:

$ sudo systemctl enable docker-jenkins

Also, check by restarting the service:

$ sudo systemctl restart docker-jenkins

How to Set up Jenkins ?

To setup jenkins first visit http://yourserverip:8080. Then you need to enter the initial admin password, which is set in the following path. Copy the output and paste it in 'Administrator password'. Then click on continue:

$ cat /data/jenkins/secrets/initialAdminPassword

You can install the required plugin and continue.

[Need assistance in Installing Software Packages on Debian Linux System ? We can help you. ]


This article covers how to Run Jenkins Server in Docker Container with Systemd. Jenkins is an opensource automation server that is designed to help software developers build, test and deploy applications and thereby streamline the continuous integration and delivery process. 

To create a system group for Jenkins, run the command:

$ sudo groupadd --system jenkins

Then create Jenkins system user:

$ sudo useradd -s /sbin/nologin --system -g jenkins jenkins

And finally add Jenkins user to docker group as shown:

$ sudo usermod -aG docker jenkins

To confirm that Jenkins user is added to the docker group, run the id command as shown:

$ id jenkins