Docker in DockerのJenkins環境をdockerで構築

dockerで構築したjnekins内でdockerイメージを作成する

これができるとjenkinsでDockerイメージまで作成し、デプロイが簡単になります。

Dindイメージを使って作成してみます

Dockerfile

FROM docker:dind

############### add tools(jdk etc...) ####################
RUN apk update
RUN apk add wget git curl zip unzip	make bzip2 zlib openssl g++ libstdc++

# jdk
RUN apk add	openjdk7

# ant
RUN apk add apache-ant --update-cache --repository http://dl-4.alpinelinux.org/alpine/edge/testing/ --allow-untrusted
RUN echo "export PATH=$PATH:/usr/share/java/apache-ant/bin" >> /root/.bashrc

# node
RUN apk add	nodejs

# grunt
RUN npm install -g grunt-cli
# gulp
RUN npm install -g gulp

# Ruby
RUN apk add libffi libffi-dev gcc
RUN apk add ruby ruby-dev

# add
RUN apk add	expect

RUN apk add freetype freetype-dev fontconfig
RUN apk add xorg-server
RUN apk add bash
RUN apk add rsync --update-cache --repository http://dl-4.alpinelinux.org/alpine/edge/testing/ --allow-untrusted
RUN apk add openssh --update-cache --repository http://dl-4.alpinelinux.org/alpine/edge/testing/ --allow-untrusted

################## set up Jenkins ##########################

RUN apk --update add openjdk7 ttf-dejavu && rm -rf /var/cache/apk/*
ENV JENKINS_HOME /var/lib/jenkins

RUN mkdir -p /usr/local/jenkins
RUN adduser -D -H -s /bin/sh jenkins
RUN chown -R jenkins:jenkins /usr/local/jenkins/
ADD jenkins.war /usr/local/jenkins/jenkins.war
RUN chmod 644 /usr/local/jenkins/jenkins.war

# jst
RUN apk add tzdata --update-cache --repository http://dl-4.alpinelinux.org/alpine/edge/testing/ --allow-untrusted
RUN ln -sf	/usr/share/zoneinfo/Asia/Tokyo /etc/localtime

################### set process ##################
COPY jenkins.sh /usr/local/bin/jenkins.sh
ENTRYPOINT /usr/local/bin/start.sh && /bin/bash

start.sh

#!/bin/sh
set -eo pipefail

# If there are any arguments then we want to run those instead
java -jar /usr/local/jenkins/jenkins.war --httpPort=8080	> /tmp/jenkins.log 2>&1 &	

set -e
docker daemon \
								--host=unix:///var/run/docker.sock \
								--host=tcp://0.0.0.0:2375 \
								--storage-driver=vfs \
--bip=172.17.101.1/24		# docker0のアドレス域を変更

docker-compose.xml

ついでにdocker-composeで起動するように設定します

myjenkins:
	restart: always
	image: myjenkins:1.0
	ports: 
		- "8080:8080"
	volumes:
		- /data/jenkins:/var/lib/jenkins		# host volumeをマップング
		- /data/git:/opt/git:ro
	env_file:
		- ./.docker-compose.env
	environment:
		- JAVA_OPTS=-XX:MaxPermSize=256m
	privileged: true

.docker-compose.env

NO_PROXY=localhost,127.0.0.1
no_proxy=localhost,127.0.0.1
HTTP_PROXY=
HTTPS_PROXY=
http_proxy=
https_proxy=

起動

# docker build -t myjenkins:1.0 .
# docker-compose -f docker-compose.yml up -d myjenkins