uni farm

docker上のjenkinsでdockerを実行

docker上のjenkinsでdockerを実行

docker内のjenkinsでdockerコマンドを実行するための設定方法を書いておく

環境

  • macOS: 10.13.4

構成ファイル

version: '3'
services:
  jenkins:
    build:
      context: ./
      dockerfile: Dockerfile-jenkins
    volumes:
      # using docker in jenkins
      - /var/run/docker.sock:/var/run/docker.sock 

    ports:
      - "8080:8080"
      - "50000:50000"
FROM jenkins/jenkins:lts-alpine

USER root

ENV JENKINS_REF /usr/share/jenkins/ref

# install jenkins plugins
COPY ./plugins.txt $JENKINS_REF/
RUN /usr/local/bin/install-plugins.sh < $JENKINS_REF/plugins.txt

RUN apk add --no-cache --virtual make && \
    apk add --no-cache ca-certificates wget && \
    update-ca-certificates && \
    apk add --no-cache openssl

# install docker, docker-compose
ENV DOCKER_VERSION 17.10.0-ce-rc2
RUN curl -fL -o docker.tgz "https://download.docker.com/linux/static/test/x86_64/docker-$DOCKER_VERSION.tgz" && \
    tar --strip-components=1 -xvzf docker.tgz -C /usr/bin

RUN curl -o /usr/local/bin/docker-compose -L https://github.com/docker/compose/releases/download/1.19.1/docker-compose-`uname -s`-`uname -m` && chmod +x /usr/local/bin/docker-compose

# add docker group
RUN addgroup -S docker && adduser jenkins docker

VOLUME /var/jenkins

ポイントは以下

  • volumesにてローカル(ホストマシン)のdockerソケットをマウントしている
    これによりdockerエンジンをjenkinsコンテナ内で使うことができる。ただしローカルのdockerを操作していることになるのを注意しておく
  • コンテナ内でコマンドを実行できるようにDockerfileでdocker, docker-composeのインストールをしている
  • jenkinsユーザがdockerコマンドを実行できるようにDockerfile内でグループに追加している

jenkinsのビルド

docker-compose up -d --build
...

docker-compose ps
    Name           Command       State       Ports     
-------------------------------------------------------
jenkins_1       /sbin/tini --    Up      0.0.0.0:50000-
                /usr/local/b             >50000/tcp, 0.
                ...                      0.0.0:8080->80
                                         80/tcp   

pipelineの作成と実行

実際にjenkinsのpipelineでdockerコマンドを実行してみる

http://localhost:8080よりNew Itemから、Enter an item nameフォームに名前を入力、Pipelineを選択、OKをクリック

create pipeline

PipelineScriptに実行スクリプトを入力、Saveをクリック

#!groovy
node {
    stage('step') {
        sh 'docker run hello-world'
        sh 'docker ps'
        sh 'pwd'
    }
}
create pipeline

作成したpipelineをクリックし、Build Nowで実行する 実行が終わったら、Console Outputをクリックしてログを見てみる

create pipeline

結果

docker rundocker psが実行される dockerエンジンはローカルのものを使用しているので、ローカルで実行中のコンテナが表示される

Started by user unknown or anonymous
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (step)
[Pipeline] sh
[test] Running shell script
+ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
9bb5a5d4561a: Already exists
Digest: sha256:3e1764d0f546ceac4565547df2ac4907fe46f007ea229fd7ef2718514bcec35d
Status: Downloaded newer image for hello-world:latest

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/engine/userguide/

[Pipeline] sh
[test] Running shell script
+ docker ps
CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS              PORTS                                              NAMES
19e965df6a1e        jenkins                      "/sbin/tini -- /us..."   26 minutes ago      Up 26 minutes       0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp   jenkins_1

[Pipeline] sh
[test] Running shell script
+ pwd
/var/jenkins_home/workspace/test
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

参考

2023, Built with Gatsby. This site uses Google Analytics.