简体   繁体   中英

How to run a privileged Docker container with the Jenkins declarative pipeline

I've been trying for a while to run a privileged container in a Jenkins Declarative Pipeline, but I can't find out the syntax or any examples in the docs. What I have so far, with a un-privileged container:

pipeline {
    agent none
    stages {
        stage("Build") {
            agent {
                docker { image "hello-world" }
            }
        }
    }
}

It seems like a common use-case, I can't believe nobody ran into it already :(

Please refer the example below:

 stage('Update dependencies version') {
        agent {
            docker {
                image 'maven'
                args '--privileged -v $HOME/.m2:/home/jenkins/.m2 -ti -u 496 -e MAVEN_CONFIG=/home/jenkins/.m2 -e MAVEN_OPTS=-Xmx2048m'
            }
        }

        when {
            not {
                branch 'master'
            }
        }

        steps {
            script {
                ....
            }
        }
    }

Or at the top level:

 pipeline {
        agent {
            docker {
                image 'maven'
                args '--privileged -v $HOME/.m2:/home/jenkins/.m2 -ti -u 496 -e MAVEN_CONFIG=/home/jenkins/.m2 -e MAVEN_OPTS=-Xmx2048m'
            }
        }
        stages {
            stage('Build') {
                steps {
                    script {
                        maven.cleanPackage()
                    }
                }
            }
        }
    }

Or inside script under stage:

        stage('Build') {
        agent {
            label 'jenkins-slave-swat-prod-01'
        }

        steps {
            script {
                docker.image('mysql:latest').withRun('-e "MYSQL_ROOT_PASSWORD=password" -e "MYSQL_DATABASE=scheduler" -p 3306:3306') { c ->
                    docker.image('maven').inside("--privileged -v $HOME/.m2:/home/jenkins/.m2 -ti -u 496 -e MAVEN_CONFIG=/home/jenkins/.m2 -e MAVEN_OPTS=-Xmx2048m --link ${c.id}:localhost") {
                        maven.cleanPackage()
                    }
                }
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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