run jenkins pipeline agent with sudo

前端 未结 7 1588
一生所求
一生所求 2020-12-09 11:19

I have an Jenkins Server running in an docker container and have access to docker an the host system, so far it is working well. Now I want to set up a pipeline testing an s

7条回答
  •  醉梦人生
    2020-12-09 11:35

    What worked for me was

    node() {
        String jenkinsUserId = sh(returnStdout: true, script: 'id -u jenkins').trim()
        String dockerGroupId = sh(returnStdout: true, script: 'getent group docker | cut -d: -f3').trim()
        String containerUserMapping = "-u $jenkinsUserId:$dockerGroupId "
        docker.image('image')
            .inside(containerUserMapping + ' -v /var/run/docker.sock:/var/run/docker.sock:ro') {
                 sh "..."
             }
    }
    

    This way the user in the container still uses the jenkins user id + group id to avoid permissions conflicts with shared data but is also member of the docker group inside container which is required to access the docker socket (/var/run/docker.sock)

    I prefer this solution as it doesn't require any additional scripts or dockerfiles

提交回复
热议问题