简体   繁体   English

Jenkins 管道:在 docker 容器内构建 docker

[英]Jenkins Pipeline: Build docker within docker container

I'm trying to do the following我正在尝试执行以下操作

  1. Checkout the code签出代码
  2. Do some prechecks using some other docker images (don't want to install these on Jenkins node)使用其他一些 docker 映像进行一些预检查(不想将这些安装在 Jenkins 节点上)
  3. Build jar using docker image maven:3.6-jdk-8使用 docker 映像maven:3.6-jdk-8构建 jar
  4. Then run Dockerfile to build app image然后运行Dockerfile构建应用镜像
  5. Push the image to repository将图像推送到存储库

Now, I don't want to install anything apart from Docker on Jenkins node.现在,我不想在 Jenkins 节点上安装除 Docker 之外的任何东西。 I want to run the full pipeline in Docker container to achieve this.我想在 Docker 容器中运行完整的管道来实现这一点。 What I'm struggling is how to build the 4th step from within the container.我正在努力的是如何从容器内构建第四步。

I wrote the Jenkinsfile as below我写的Jenkinsfile如下

pipeline {

    agent none
    
    stages {
        stage('Maven build') {
            agent {
                docker {
                    image 'maven:3.6-jdk-8'
                    args '-u root:root'
                }
            }
            steps {
                checkout(
                    [
                        $class: 'GitSCM',
                        branches: [
                            [name: '*/master']
                        ],
                        doGenerateSubmoduleConfigurations: false, 
                        extensions: [], 
                        submoduleCfg: [], 
                        userRemoteConfigs: [
                            [
                                credentialsId: '<cred-id>',
                                url: '<github-url>']
                            ]
                        ])
                        
                sh '''
                    set -eux pipefail

                    mvn -e clean install
                '''
            }
        }
        stage('Build docker image') {
             // Which docker image to use?
        }
    }
}

But I'm not sure how to build a docker image within container.但我不确定如何在容器中构建 docker 映像。 The search didn't help that much.搜索并没有太大帮助。 I tried using the Jenkins node for the docker image building but it seems I cannot mix and match.我尝试使用 Jenkins 节点来构建 docker 图像,但似乎我无法混合搭配。 I totally understand this is quite an open question but I think it would be helpful to know the straightforward answer(s).我完全理解这是一个非常开放的问题,但我认为知道直截了当的答案会很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM