1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

DockerのCentOSにJenkinsをインストール

Last updated at Posted at 2020-08-27

1. DockerでJenkinsを使う方法

以下の2つの方法が考えられます。

1.1. 公式イメージを使う

単にJenkinsを使うことが目的の場合、以下のJenkins公式イメージを使用するのが良いです。

Docker Hubの公式イメージ jenkins のバージョンは古いようです。

jenkins - Docker hub
[イメージID] jenkins:latest
[バージョン] v2.60.3 (2020/8/28 時点)

Githubにサイトがある jenkins/jenkins が最新のようです。

jenkinsci/docker: Docker official jenkins repo
[イメージID] jenkins/jenkins:lts
[バージョン] v2.235.5 (2020/8/28 時点)

1.2. OSイメージにインストール

CentOSなどのOSイメージに必要なパッケージと合わせてインストールすることもできます。

centos - Docker Hub

2. CentOSにJenkinsをインストール

今回、CentOSにJenkinsをインストールする方法を試しました。

2.1. Dockerfile

通常、Jenkins等のサービスをCentOSにインストールする場合、サービスの起動終了を__systemd__で管理します。
しかし、Dockerはプロセスごとにコンテナを作成するのが基本思想であるため、CentOSの公式イメージではsystemdが有効化されていません。

そのため、公式サイトに記載されている__systemdを有効にする方法__を考慮してDockerfileを作成しました。

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

以下Dockerfileの__2つ目のRUN以外__が参考にした部分です。
1つ目のRUNとVOLUMEが主要な内容と思われるのですが、__これが無くてもJenkinsは起動__しました。
ただ、以下のようにデフォルトのままだと「might cause issues(問題を引き起こす)」と記載されていたので、あった方が良い内容だとは思われます。

This Dockerfile deletes a number of unit files which might cause issues.
(Google翻訳:このDockerfileは、問題を引き起こす可能性があるいくつかのユニットファイルを削除します。)

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"]

操作コマンドについても、参考にしています。

操作コマンド
docker build --rm -t <イメージ名:タグ> .
docker run -it -d --privileged --name <コンテナ名> -h <ホスト名> -p 8080:8080 <イメージ名:タグ>

2.2. 補足

2.2.1. initscriptsパッケージ

CentOSイメージには、__initscriptsパッケージ__が含まれていません。
yumコマンドで追加していない場合は起動処理自体がエラーとなり実行されないため、あらかじめ追加が必要です。

以下は、まず、__initscriptsパッケージ__を含めずにビルドしたイメージでコンテナを起動。
その後、コンテナ内に入り、改めて「systemctl start jenkins」及び「systemctl status jenkins」コマンドを実行した際のエラーです。

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

/etc/init.d/functions が存在しないため、エラーになっています。

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. /etc/init.d/functions が含まれるパッケージの確認

どのyumパッケージに含まれるか__について「yum provides」で調べると、__initscripts__パッケージと分かります。
ただ、「/etc/init.d/functions」ではマッチせず、「*/init.d/functions」でマッチしました。

Linuxではシンボリックリンクが多用されています。
__表示されているパスを含めたファイル名が本来の名称とは限らない__ため、注意が必要です。
functionsだけで検索すると大量にマッチするので、少しずつディレクトリを削りました。

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バージョン

このDockerfileではCentOS:7を使用していますが、CentOS:8で試したところエラーになったためです。
CentOS 8のデフォルトのコンテナエンジンがDockerでなくPodmanになったからかもしれません(妄想です)。
詳細は確認していません。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?