Jenkins Docker Permission Denied for varrundocker.sock in Pipeline - Stack Overflow

admin2025-04-21  2

I'm trying to build and push a Docker image in a Jenkins pipeline, but I keep getting a permission denied error when running docker login. My Jenkins instance is running in Docker. Here’s the error I get:

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.29/info: dial unix /var/run/docker.sock: connect: permission denied

I installed Docker and Node.js in the Jenkins container, and everything else seems to work fine. My pipeline script is:

pipeline {
    agent any
    
    tools {
        maven 'maven3'
        jdk 'jdk17'
    }
    
    environment {
        SCANNER_HOME=tool 'sonar-scanner'
    }

    stages {
        stage('Test Docker Access') {
            steps {
                script {
                    sh "docker --version"
                }
            }
        }

        stage('Build Docker Image') {
            steps {
                script {
                    withDockerRegistry(credentialsId: 'docker-cred', toolName: 'docker') {
                        sh "docker build -t santa:latest ."
                    }
                }
            }
        }

        stage('Tag & Push Docker Image') {
            steps {
                script {
                    withDockerRegistry(credentialsId: 'docker-cred', toolName: 'docker') {
                        sh "docker tag santa:latest marius/santa:latest"
                        sh "docker push marius/santa:latest"
                    }
                }
            }
        }
    }
}

I tried verifying if docker and nodejs is installed and they are installed

转载请注明原文地址:http://anycun.com/QandA/1745228894a90504.html