{"id":7359,"date":"2021-06-22T16:32:11","date_gmt":"2021-06-22T16:32:11","guid":{"rendered":"https:\/\/linuxways.net\/?p=7359"},"modified":"2021-06-22T16:32:11","modified_gmt":"2021-06-22T16:32:11","slug":"install-and-run-jenkins-with-systemd-and-docker","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/install-and-run-jenkins-with-systemd-and-docker\/","title":{"rendered":"Install and Run Jenkins with Systemd and Docker"},"content":{"rendered":"<p>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.<\/p>\n<p>In this article, I am going to download the Jenkins package from docker hub repository and manage it using systemd manager. I have used Ubuntu Linux, but you can do it in centos, Debian, Redhat etc which support systemd.<\/p>\n<h2>Prerequisites<\/h2>\n<ol>\n<li>Latest version of docker installed<\/li>\n<li>Internet connection for downloading Jenkins image<\/li>\n<\/ol>\n<h2>Configuration<\/h2>\n<p>To run a process we need users. So, we create a system group, add a system user to the group. As you don\u2019t need to login we provide no login shell. I have here given the name \u2018devops\u2019, you can give of your choice.<\/p>\n<pre>$ sudo groupadd --system devops<\/pre>\n<pre>$ sudo useradd -s \/sbin\/nologin --system -g devops devops<\/pre>\n<p>During docker installation, a user docker was added by default. So to run our process inside docker, let\u2019s add \u2018devops\u2019 user and your current login user to the docker group.<\/p>\n<pre>$ sudo usermod -aG docker devops<\/pre>\n<pre>$ sudo usermod -aG docker $USER<\/pre>\n<p>We need a directory to map container volume to the host. So, let\u2019s create a directory and give current user ownership in the directory.<\/p>\n<pre>$ sudo mkdir -p \/data\/jenkins<\/pre>\n<pre>$ chown -R $USER:$USER \/data\/jenkins<\/pre>\n<p>You can now verify user \u2018devops\u2019 using the following command. Your output must be similar to the following.<\/p>\n<pre>$ id devops<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"644\" height=\"81\" class=\"wp-image-7360\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-537.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-537.png 644w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-537-300x38.png 300w\" sizes=\"auto, (max-width: 644px) 100vw, 644px\" \/><\/p>\n<p>Now it\u2019s time to create a service. The service files are stored inside \/etc\/systemd\/system\/ So, create a file ending with .service as shown below.<\/p>\n<pre>$ vim \/etc\/systemd\/system\/docker-jenkins.service<\/pre>\n<p>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.<\/p>\n<pre>[Unit]\r\n\r\nDescription=My Jenkins Server\r\n\r\nDocumentation=https:\/\/jenkins.io\/doc\/\r\n\r\nAfter=docker.service\r\n\r\nRequires=docker.service\r\n\r\n[Service]\r\n\r\nType=simple\r\n\r\nUser=devops\r\n\r\nGroup=devops\r\n\r\nTimeoutStartSec=0\r\n\r\nRestart=on-failure\r\n\r\nRestartSec=30s\r\n\r\nExecStartPre=-\/usr\/bin\/docker kill jenkins-server\r\n\r\nExecStartPre=-\/usr\/bin\/docker rm jenkins-server\r\n\r\nExecStartPre=\/usr\/bin\/docker pull jenkins\/jenkins\r\n\r\nExecStart=\/usr\/bin\/docker run \\\r\n\r\n--name jenkins-server \\\r\n\r\n--publish 8080:8080 \\\r\n\r\n--publish 50000:50000 \\\r\n\r\n--volume \/data\/jenkins:\/var\/jenkins_home \\\r\n\r\njenkins\/jenkins\r\n\r\nSyslogIdentifier=jenkin\r\n\r\nExecStop=\/usr\/bin\/docker stop jenkins-server\r\n\r\n[Install]\r\n\r\nWantedBy=multi-user.target<\/pre>\n<p>Reload the service file in the systemd daemon, using the following command.<\/p>\n<pre>$ sudo systemctl daemon-reload<\/pre>\n<p>Start the docker service using the command below.<\/p>\n<pre>$ sudo systemctl start docker-jenkins<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"971\" height=\"280\" class=\"wp-image-7361\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-538.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-538.png 971w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-538-300x87.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-538-768x221.png 768w\" sizes=\"auto, (max-width: 971px) 100vw, 971px\" \/><\/p>\n<p>Systemd also provides default logging service in syslog. You can see the log in the following location.<\/p>\n<pre>$ sudo tail -f \/var\/log\/syslog<\/pre>\n<p>Now, you can start the service after the server reboots using the \u2018enabling\u2019 feature of systemd manager.<\/p>\n<pre>$ sudo systemctl enable docker-jenkins<\/pre>\n<p>Also, check by restarting the service.<\/p>\n<pre>$ sudo systemctl restart docker-jenkins<\/pre>\n<h2>Jenkins setup<\/h2>\n<p>To setup jenkins first visit <a href=\"http:\/\/yourserverip:8080\">http:\/\/yourserverip:8080<\/a> Then you need to enter the initial admin password, which is set in the following path. Copy the output and paste it in \u201cAdministrator password\u2019. Then click on continue.<\/p>\n<pre>$ cat \/data\/jenkins\/secrets\/initialAdminPassword<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1036\" height=\"658\" class=\"wp-image-7362\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-539.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-539.png 1036w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-539-300x191.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-539-1024x650.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-539-768x488.png 768w\" sizes=\"auto, (max-width: 1036px) 100vw, 1036px\" \/><\/p>\n<p>You can install the required plugin and continue.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1025\" height=\"582\" class=\"wp-image-7363\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-540.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-540.png 1025w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-540-300x170.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-540-768x436.png 768w\" sizes=\"auto, (max-width: 1025px) 100vw, 1025px\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>In this article, you learnt how to install the latest version of Jenkins using docker and configure the service to use it. Managing Jenkins with systemd makes the work simpler. Thank you for reading.<\/p>","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>","protected":false},"author":1,"featured_media":7556,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[160,248,100],"class_list":["post-7359","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu","tag-docker","tag-jenkins","tag-ubuntu-20-04"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/7359","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=7359"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/7359\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/7556"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=7359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=7359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=7359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}