我有jenkins作为Docker容器运行,我试图在这里安装jenkins构建和发布插件并在jenkins工作区内复制Dockerfile,但每当我运行构建时,它都会给我:
Started by user Jenkins Admin Building in workspace /var/lib/jenkins/jobs/workspace [workspace] $ docker build -t index.docker.io/test/openshift:latest --pull=true /var/lib/jenkins/jobs/test/workspace ERROR: Cannot run program "docker" (in directory "/var/lib/jenkins/jobs/workspace"): error=2, No such file or directory java.io.IOException: Cannot run program "docker" (in directory "/var/lib/jenkins/jobs/workspace"): error=2, No such file or directory at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048) at hudson.Proc$LocalProc.(Proc.java:244) at hudson.Proc$LocalProc. (Proc.java:216) at hudson.Launcher$LocalLauncher.launch(Launcher.java:803) at hudson.Launcher$ProcStarter.start(Launcher.java:381) Build step 'Docker Build and Publish' marked build as failure Finished: FAILURE
你能告诉我为什么会这样吗?
在Docker容器内docker
,默认情况下您无法访问-binary(因此出现错误消息No such file or directory
).
如果你想在Docker容器中使用Docker,你需要使用DinD(Docker-in-Docker)或DooD(Docker-of-Docker).第一个是Jenkins容器中的单独Docker安装,第二个只通过卷安装主机Docker安装.
关于DinD以及Jenkins的进一步阅读:
https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/
https://github.com/killercentury/docker-jenkins-dind
https://github.com/tehranian/dind-jenkins-slave
关于DooD以及Jenkins的进一步阅读:
http://container-solutions.com/running-docker-in-jenkins-in-docker/
https://hub.docker.com/r/axltxl/jenkins-dood/
以下有关使用Workflow插件的信息不再正确.我已经编写了一个名为plugin的插件docker-swarm-slave
,它提供了一个build-wrapper,你可以为一个作业自动配置Docker-container,如果你使用我的jenkins-dood
-image或直接在裸机上运行.
不幸的是,文档很稀疏,但也许它对某些人有用.
我有一个类似的用例:我希望能够自动启动一个Docker容器,该容器具有运行Jenkins Swarm客户端的指定映像,该 客户端将接管构建.我的jenkins-dood
-image包含一个脚本docker-slave
,它允许我自动配置Docker-Swarm-slave并使用Workflow-plugin执行我需要的内容,脚本如下所示:
node('master') { stage 'Create docker-slave' withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'swarm-login', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) { sh 'docker-slave --job-name $JOB_NAME --build-number $BUILD_NUMBER -i pitkley/python-swarm:3.4 -u $USERNAME -p $PASSWORD -- -labels "${JOB_NAME}_${BUILD_NUMBER}"' } stage 'Execute on docker-slave' node("${env.JOB_NAME}_${env.BUILD_NUMBER}") { sh 'hostname' } stage 'Remove docker-slave' sh 'docker-slave --job-name $JOB_NAME --build-number $BUILD_NUMBER --rm' }
(这假设您需要凭据来验证使用短ID保存的凭据swarm-credentials
.)