如何使用Jenkins Docker插件在容器中构buildMaven项目

在使用maven作为构build工具的项目上工作。 现在,当使用Jenkins进行部署时,我们需要使用Docker插件在Docker容器中构build项目。 我的理解是,项目应该build立在容器内,一旦完成,应该删除。

我试图使用类似于:docker.image(“imageName”)。{}现在我们如何确保容器被删除并装入一个卷,以便作为构build的一部分创build的jar可以在docker之后被访问容器删除?

有些人可以提供上述理解和上述命令的例子或任何链接参考?

我想,如果你使用pipe道工作,这将是很好的。 在这里你可以检查我的例子与评论

pipeline { stages { stage('Build') { agent { //here we select only docker build agents docker { image 'maven:latest' //container will start from this image args '-v /root/.m2:/root/.m2' //here you can map local maven repo, this let you to reuse local artifacts } } steps { sh 'mvn -B -DskipTests clean package' //this command will be executed inside maven container } } stage('Test') { //on this stage New container will be created, but current pipeline workspace will be remounted to it automatically agent { docker { image 'maven:latest' args '-v /root/.m2:/root/.m2' } } steps { sh 'mvn test' } } stage ('Build docker image') { //here you can check how you can build even docker images inside container agent { docker { image 'maven:latest' args '-v /root/.m2:/root/.m2 -v /var/run/docker.sock:/var/run/docker.sock' //here we expose docker socket to container. Now we can build docker images in the same way as on host machine where docker daemon is installed } } steps { sh 'mvn -Ddocker.skip=false -Ddocker.host=unix:///var/run/docker.sock docker:build' //example of how to build docker image with pom.xml and fabric8 plugin } } } 

}

即使Jenkins本身在主机的jenkins_home贴片机的容器中运行,这也可以工作。

请让我知道,如果我能从我的经验给你提供更多有用的细节