Fumiya_Senoo’s diary

日々の気になったこと、役立ったことを記録しています

WSL2 で Jenkins 動かして、 Docker を操作する

ちょっと、 Jenkins で Docker を操作したかったので試してみた。

WSL2 は Stable Diffusion 触ってみました - Fumiya_Senoo’s diary のときにインストールした。

Jenkins のインストール

www.jenkins.io より

curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee \
  /usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins

インストールが終わったら↓で Jenkins サービスを起動する。

service jenkins start

Windows 上で localhost:8080 にアクセスすると Jenkins 管理画面へアクセスできる。 初期設定の流れはちゃんと読めばできるので割愛。

Docker のインストール

japan.zdnet.com より

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo aptinstall apt-transport-https ca-certificates curl gnupg lsb-release -y
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io -y
sudo usermod -aG docker $USER

Docker の起動確認

docker run hello-world

で、以下のように表示されれば、ちゃんと起動している(はず)。

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

以下のメッセージが表示される場合は、起動失敗している。

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

エラーの原因を探る

エラー発生直後に以下のコマンドを実行する。

dockerd

今回試した環境では以下のエラーが発生していた。

failed to start daemon: Error initializing network controller: error obtaining controller instance: unable to add return rule in DOCKER-ISOLATION-STAGE-1 chain:  (iptables failed: iptables --wait -A DOCKER-ISOLATION-STAGE-1 -j RETURN: iptables v1.8.7 (nf_tables):  RULE_APPEND failed (No such file or directory): rule in chain DOCKER-ISOLATION-STAGE-1
 (exit status 4))

iptables 周りで問題が起きている様子。

エラーを回避する

iptables を使用しないようにする。 当然ですが、公に公開する環境で iptables を使用しないようにするだけはまずいでしょう…

docs.docker.jp より

/etc/docker/daemon.json に以下を記載する。

{
    "iptables": false
}

エラーが解消したか確認する

以下コマンドで改めて Docker サービスを開始する。

service docker stop
service docker start

改めて docker run などで起動するか確認する。

(実際には dockerd プロセスが立ち上げっているか見たんだけれども)

# ps -ef | grep docker
root      5112    17  0 16:59 ?        00:00:00 /usr/bin/dockerd -p /var/run/docker.pid
root      5129  5112  0 16:59 ?        00:00:00 containerd --config /var/run/docker/containerd/containerd.toml --log-level info
root      5210    18  0 16:59 pts/0    00:00:00 grep --color=auto docker
#

Jenkins で Docker を操作する

Docker Pipeline | Jenkins plugin をインストールしたうえで、Jenkins のパイプラインを使用すると動きます。

Jenkins のパイプラインの書き方が正直よくわからなくて、以下を見ながら真似だけしました。

www.kimullaa.com mitomito.hatenablog.jp

もうちょっとわかったら Jenkins パイプラインについて書きたい。