jenkins和docker 使用docker做爲slave

使用docker做爲jenkins slave。java

 

文章來自:http://www.ciandcd.com
文中的代碼來自能夠從github下載: https://github.com/ciandcdnode

參考:git

https://www.cloudbees.com/blog/templating-jenkins-build-environments-docker-containersgithub

須要安裝插件:docker

CloudBees Docker Custom Build Environment Plugin
https://wiki.jenkins-ci.org/display/JENKINS/CloudBees+Docker+Custom+Build+Environment+Pluginssh

 

1. docker 和插件CloudBees Docker Custom Build Environment Pluginjvm

docker是一種很是好的方法來快速生成可重現,隔離的環境。docker相對虛擬機更輕量級,須要更少的資源來運行。maven

docker的另外一個好處是docker image能夠是從hub.docker.com或私有的repo獲取的編譯好的docker image, 也能夠下載文本格式的Dockerfile,而後在生成docker image。 測試

Dockerfile能夠與項目的源代碼一個版本控制,從而實現源代碼和構建環境docker image同步且可重現。ui


插件CloudBees Docker Custom Build Environment Plugin,經過此插件能夠直接使用docker image或Dockerfile來生成job的運行環境。 

 

2. jenkins docker images

https://hub.docker.com/r/jenkinsci/jnlp-slave/ (docker pull jenkinsci/jnlp-slave)

https://hub.docker.com/r/jenkinsci/ssh-slave/ (docker pull jenkinsci/ssh-slave)

https://hub.docker.com/r/jenkinsci/slave/  (docker pull jenkinsci/slave)

全部jenkins相關的docker https://hub.docker.com/u/jenkinsci/。

 

本實例中使用jnlp-slave docker image,可是個人環境沒有jenkins slave,job運行在master上,docker也安裝在master上。

若是你job運行在node/slave上,須要node上安裝docker。

 

確保已經安裝docker,且當前用戶能夠不加sudo來使用docker。

sudo groupadd docker
sudo gpasswd -a ${USER} docker
newgrp docker
sudo service docker restart

而後運行如下確保docker安裝且設置正確:

osboxes@osboxes:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
osboxes@osboxes:~$ docker info
Containers: 1
Running: 0
Paused: 0
Stopped: 1
Images: 3
Server Version: 1.11.2
Storage Driver: aufs
Root Dir: /var/lib/docker/aufs

測試下載jenkins slave docker image:

osboxes@osboxes:~$ docker -H unix:///var/run/docker.sock pull jenkinsci/jnlp-slave
Using default tag: latest
latest: Pulling from jenkinsci/jnlp-slave
7ccc78f8af6d: Already exists
a3ed95caeb02: Already exists
29f19d8b362b: Already exists
a068cb6fd68b: Already exists
8f8532f99b46: Already exists
fc6378a07ddb: Already exists
debdb2a461e8: Already exists
4a4e85ec23cd: Already exists
af041ce270a0: Already exists
82928c989a2b: Already exists
a05116d5d5b4: Already exists
Digest: sha256:cfdb32e8fa5ebf965caf8244c337ca5451eb6c45c8f881f1823bd8eb3c61bb62
Status: Image is up to date for jenkinsci/jnlp-slave:latest

 

3. 建立freestyle job使用docker

1) 使用測試git repo:https://github.com/ciandcd/JavaHelloWorld.git

2)jenkins job 配置

使用jenkinsci/jnlp-slave docker image,注意docker host uri的配置,個人docker和jenkins都在同一測試機器上。

3)job的配置xml以下:https://github.com/ciandcd/jenkins-awesome/blob/master/jenkins_home/jobs/docker_hello/config.xml

4) 檢查job的運行log能夠看到jenkins本身下載啓動docker image,而後運行後中止docker。

$ docker -H unix:///var/run/docker.sock inspect jenkinsci/jnlp-slave
$ docker -H unix:///var/run/docker.sock run --rm --entrypoint /bin/true alpine:3.2
$ docker -H unix:///var/run/docker.sock run --tty --detach --workdir /home/osboxes/github/jenkins-example/jenkins_home/jobs/docker_hello/workspace --volume /home/osboxes/github/jenkins-example/jenkins_home:/home/osboxes/github/jenkins-example/jenkins_home:rw --volume /tmp:/tmp:rw --net bridge --add-host dockerhost:172.17.0.1 --env BUILD_DISPLAY_NAME=#12 --env BUILD_ID=12 --env BUILD_NUMBER=12 --env BUILD_TAG=jenkins-docker_hello-12 --env BUILD_URL=http://10.130.28.78:8008/job/docker_hello/12/ --env CLASSPATH= --env EXECUTOR_NUMBER=0 --env GIT_BRANCH=origin/master --env GIT_COMMIT=b462496ca38e2c5b17aa6f599322cebffe104c24 --env GIT_PREVIOUS_COMMIT=b462496ca38e2c5b17aa6f599322cebffe104c24 --env GIT_URL=https://github.com/ciandcd/JavaHelloWorld.git --env HUDSON_HOME=/home/osboxes/github/jenkins-example/./jenkins_home --env HUDSON_SERVER_COOKIE=f2bf6016b1befbaa --env HUDSON_URL=http://10.130.28.78:8008/ --env JENKINS_SERVER_COOKIE=f2bf6016b1befbaa --env JENKINS_URL=http://10.130.28.78:8008/ --env JOB_BASE_NAME=docker_hello --env JOB_NAME=docker_hello --env JOB_URL=http://10.130.28.78:8008/job/docker_hello/ --env NODE_LABELS=master --env NODE_NAME=master --env PATH+JDK=/usr/lib/jvm/java-8-openjdk-amd64/bin --env WORKSPACE=/home/osboxes/github/jenkins-example/jenkins_home/jobs/docker_hello/workspace jenkinsci/jnlp-slave /bin/cat
Docker container a44510f2cc21a473c26759cfa650dc26b4c0b7799d11eb37ab21d02cf5682276 started to host the build
$ docker -H unix:///var/run/docker.sock exec --tty a44510f2cc21a473c26759cfa650dc26b4c0b7799d11eb37ab21d02cf5682276 env
[workspace] $ docker -H unix:///var/run/docker.sock exec --tty --user 1000:1000 a44510f2cc21a473c26759cfa650dc26b4c0b7799d11eb37ab21d02cf5682276 env BUILD_DISPLAY_NAME=#12 BUILD_ID=12 BUILD_NUMBER=12 BUILD_TAG=jenkins-docker_hello-12 BUILD_URL=http://10.130.28.78:8008/job/docker_hello/12/ CA_CERTIFICATES_JAVA_VERSION=20140324 CLASSPATH= EXECUTOR_NUMBER=0 GIT_BRANCH=origin/master GIT_COMMIT=b462496ca38e2c5b17aa6f599322cebffe104c24 GIT_PREVIOUS_COMMIT=b462496ca38e2c5b17aa6f599322cebffe104c24 GIT_URL=https://github.com/ciandcd/JavaHelloWorld.git HOME=/home/jenkins HOSTNAME=a44510f2cc21 HUDSON_HOME=/home/osboxes/github/jenkins-example/./jenkins_home HUDSON_SERVER_COOKIE=f2bf6016b1befbaa HUDSON_URL=http://10.130.28.78:8008/ JAVA_DEBIAN_VERSION=8u66-b01-1~bpo8+1 JAVA_VERSION=8u66 JENKINS_SERVER_COOKIE=f2bf6016b1befbaa JENKINS_URL=http://10.130.28.78:8008/ JOB_BASE_NAME=docker_hello JOB_NAME=docker_hello JOB_URL=http://10.130.28.78:8008/job/docker_hello/ LANG=C.UTF-8 NODE_LABELS=master NODE_NAME=master PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin PATH+JDK=/usr/lib/jvm/java-8-openjdk-amd64/bin TERM=xterm WORKSPACE=/home/osboxes/github/jenkins-example/jenkins_home/jobs/docker_hello/workspace /usr/share/maven/bin/mvn clean verify
env: /usr/share/maven/bin/mvn: No such file or directory
Build step 'Invoke top-level Maven targets' marked build as failure
Stopping Docker container after build completion
$ docker -H unix:///var/run/docker.sock kill a44510f2cc21a473c26759cfa650dc26b4c0b7799d11eb37ab21d02cf5682276
a44510f2cc21a473c26759cfa650dc26b4c0b7799d11eb37ab21d02cf5682276
$ docker rm --force a44510f2cc21a473c26759cfa650dc26b4c0b7799d11eb37ab21d02cf5682276
a44510f2cc21a473c26759cfa650dc26b4c0b7799d11eb37ab21d02cf5682276

 

參考:

DOCKER_PORT 相關:http://stackoverflow.com/questions/26561963/how-to-detect-a-docker-daemon-port

相關文章
相關標籤/搜索