Install Jenkins on Docker’s CentOS

4 minute read

1. How to use Jenkins with Docker

There are two possible methods:

1.1. Use official image

If your goal is simply to use Jenkins, it’s a good idea to use the official Jenkins image below.

Official image of Docker Hub The version of jenkins seems to be old.

jenkins - Docker hub
[Image ID] jenkins: latest
[Version] v2.60.3 (as of August 28, 2020)

Jenkins / jenkins, which has a site on Github, seems to be the latest.

jenkinsci/docker: Docker official jenkins repo
[Image ID] jenkins / jenkins: lts
[Version] v2.235.5 (as of August 28, 2020)

1.2. Install on OS image

It can also be installed with the packages required for an OS image such as CentOS.

centos - Docker Hub

2. Install Jenkins on CentOS

This time, I tried how to install Jenkins on CentOS.

2.1. Dockerfile
Normally, when installing a service such as Jenkins on CentOS, the start and end of the service is managed by systemd.
However, since Docker’s basic idea is to create a container for each process, systemd is not enabled in the official image of __CentOS. __

Therefore, I created a Dockerfile considering how to enable __systemd described on the official website.

Systemd integration: Dockerfile for systemd base image
https://hub.docker.com/_/centos?tab=description

Below is the part referenced by __ other than the second RUN of the Dockerfile.
The first RUN and VOLUME seem to be the main content, but Jenkins started __ without this __.
However, if you leave the default as shown below, it says “might cause issues”, so it seems better to have it.

This Dockerfile deletes a number of unit files which might cause issues.
(Google Translate: This Dockerfile removes some unit files that can cause problems.)

Dockerfile


FROM centos:7
ENV container docker
RUN (cd /lib/systemd/system/sysinit.target.wants/; \
     for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \
    rm -f /lib/systemd/system/multi-user.target.wants/*; \
    rm -f /etc/systemd/system/*.wants/*; \
    rm -f /lib/systemd/system/local-fs.target.wants/*; \
    rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
    rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
    rm -f /lib/systemd/system/basic.target.wants/*; \
    rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]
EXPOSE 8080
RUN yum install -y java wget initscripts && \
    cd /etc/yum.repos.d && \
    wget http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo && \
    rpm --import http://pkg.jenkins.io/redhat-stable/jenkins.io.key && \
    yum install -y jenkins && \
    systemctl enable jenkins
CMD ["/sbin/init"]

I also refer to the operation commands.

Operation command


docker build --rm -t <Image name:tag> .
docker run -it -d --privileged --name <Container name> -h <hostname> -p 8080:8080 <Image name:tag>

2.2. Supplement

2.2.1. initscripts package

The CentOS image does not include the __initscripts package __.
If you do not add it with the yum command, the startup process itself will result in an error and will not be executed, so you need to add it in advance.

The following first starts the container with the image built without including the initscripts package __.
After that, it is an error when entering the container and executing the “__systemctl start jenkins
” and “systemctl status jenkins” commands again.

bash


[root@localhost /]# systemctl start jenkins
Job for jenkins.service failed because the control process exited with error code. See "systemctl status jenkins.service" and "journalctl -xe" for details.
[root@localhost /]#
[root@localhost /]# systemctl status jenkins
● jenkins.service - LSB: Jenkins Automation Server
   Loaded: loaded (/etc/rc.d/init.d/jenkins; bad; vendor preset: disabled)
   Active: failed (Result: exit-code) since Thu 2020-08-27 01:31:06 UTC; 5s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 88 ExecStart=/etc/rc.d/init.d/jenkins start (code=exited, status=1/FAILURE)

Aug 27 01:31:06 localhost systemd[1]: Starting LSB: Jenkins Automation Server...
Aug 27 01:31:06 localhost jenkins[88]: /etc/rc.d/init.d/jenkins: line 59: /etc/init.d/functions: No such file or directory
Aug 27 01:31:06 localhost systemd[1]: jenkins.service: control process exited, code=exited status=1
Aug 27 01:31:06 localhost systemd[1]: Failed to start LSB: Jenkins Automation Server.
Aug 27 01:31:06 localhost systemd[1]: Unit jenkins.service entered failed state.
Aug 27 01:31:06 localhost systemd[1]: jenkins.service failed.
[root@localhost /]#

Aug 27 01:31:06 localhost jenkins[88]: /etc/rc.d/init.d/jenkins: line 59: /etc/init.d/functions: No such file or directory

It is an error because __ /etc/init.d/functions__ does not exist.

bash


[root@localhost /]# ls -l /etc/init.d/functions
ls: cannot access /etc/init.d/functions: No such file or directory
[root@localhost /]#

2.2.2. Checking the package that contains /etc/init.d/functions

If you look up “yum provides” to find out which yum package contains , you will find that it is a __initscripts package.
However, it did not match in “/etc/init.d/functions”, but it matched in “* /init.d/functions”.

Symbolic links are heavily used in Linux.
Please note that the file name including the displayed path is not always the original name __.
If you search only by functions, it will match a lot, so I deleted the directory little by little.

bash


[root@localhost /]# yum provides */init.d/functions*
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: ftp-srv2.kddilabs.jp
 * extras: ftp-srv2.kddilabs.jp
 * updates: ftp-srv2.kddilabs.jp
initscripts-9.49.49-1.el7.x86_64 : The inittab file and the /etc/init.d scripts
Repo        : base
Matched from:
Filename    : /etc/rc.d/init.d/functions

[root@localhost /]#

2.2.3. CentOS version

This Dockerfile uses CentOS: 7, but when I tried it with CentOS: 8, I got an error.
Maybe it’s because CentOS 8’s default container engine is now Podman instead of Docker (a delusion).
Details have not been confirmed.