Jenkins pipeline alpine agent "apk update ERROR: Unable to lock database: Permission denied"

Jenkins pipeline alpine agent "apk update ERROR: Unable to lock database: Permission denied"

我正在使用 Alpine docker 图像作为 Jenkins 管道代理,但在 运行 apk updateapk add package 期间我一直收到权限被拒绝的错误。我在 Ubuntu 图片上也看到了类似的错误,而 运行 apt updateapt install

这是我的 Jenkinsfile:

pipeline {
    agent none
    stages {
        stage('Initialization') {
            agent any
            steps {
                checkout scm
            }
        }

        stage('Git Clone') {
            agent { docker { image 'alpine:3.12.0' } }
            steps {
                sh '''
                    apk update;
                    apk add --no-cache git;
                    apk add --no-cache openssh;
                    git -v;
                '''
            }
        }
    }
}

这是 Jenkins 的输出:

+ docker inspect -f . alpine:3.12.0
WARNING: Error loading config file: /root/.docker/config.json: stat /root/.docker/config.json: permission denied
.
[Pipeline] withDockerContainer
Jenkins does not seem to be running inside a container
$ docker run -t -d -u 1001:0 -w "/opt/bitnami/jenkins/jenkins_home/workspace/Deploy Glosfy Frontend" -v "/opt/bitnami/jenkins/jenkins_home/workspace/Deploy Glosfy Frontend:/opt/bitnami/jenkins/jenkins_home/workspace/Deploy Glosfy Frontend:rw,z" -v "/opt/bitnami/jenkins/jenkins_home/workspace/Deploy Glosfy Frontend@tmp:/opt/bitnami/jenkins/jenkins_home/workspace/Deploy Glosfy Frontend@tmp:rw,z" -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** alpine:3.12.0 cat
$ docker top 166c9ace17a4eb6aef0af0bbc04902ee4a358212be7f029550fb39a921e305aa -eo pid,comm
[Pipeline] {
[Pipeline] sh
+ apk update
ERROR: Unable to lock database: Permission denied
ERROR: Failed to open apk database: Permission denied
[Pipeline] }
$ docker stop --time=1 166c9ace17a4eb6aef0af0bbc04902ee4a358212be7f029550fb39a921e305aa
$ docker rm -f 166c9ace17a4eb6aef0af0bbc04902ee4a358212be7f029550fb39a921e305aa
[Pipeline] // withDockerContainer
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
ERROR: script returned exit code 99
Finished: FAILURE

谁能帮我解决这个问题?

请像这样修改 Jenkins 管道中的 docker 标记:

docker { 
image 'alpine:3.12.0' 
args '-u root:root'
 } 

我认为问题是 Jenkins 是 运行 非 root 用户的容器,因此 Permission denied 错误。

尝试像这样更改您的管道:

agent {
    docker {
            image 'alpine:3.12.0'
            args '-u root'
        }
    }

参见