Jenkins in Docker Fails to Run Minikube: 'The Docker Driver Should Not Be Used with Root Privileges - Stack Overflow

admin2025-04-17  2

I'm encountering an issue in my Jenkins CI/CD pipeline where Jenkins running in a Docker container is unable to execute the docker command during the build process.Specifically, when the pipeline attempts to start Minikube, I receive the following error

+ minikube start
* minikube v1.35.0 on Debian 12.8 (docker/amd64)
* Automatically selected the docker driver. Other choices: none, ssh
* The "docker" driver should not be used with root privileges. If you wish to continue as root, use --force.
* If you are running minikube within a VM, consider using --driver=none:
*   /

X Exiting due to DRV_AS_ROOT: The "docker" driver should not be used with root privileges.

This error occurs because Minikube detects that it is running as root inside the Jenkins container and refuses to use the Docker driver under these conditions.

How can I fix the issue?

Here are the codes shown below

Here is the groovy file to create pipeline in Jenkins

import hudson.plugins.git.UserRemoteConfig
import hudson.plugins.git.BranchSpec
import hudson.plugins.git.GitSCM
import jenkins.model.*
import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition

def instance = Jenkins.getInstance()

def jobName = "flightsearchapi"
def job = instance.getItem(jobName)

if (job != null) {
    job.delete()
}

def pipelineJob = instance.createProject(org.jenkinsci.plugins.workflow.job.WorkflowJob, jobName)
def definition = new CpsScmFlowDefinition(
        new GitSCM(
                [
                        new UserRemoteConfig("git-repo-url", null, null, null)
                ],
                [new BranchSpec("*/branch-name")],
                false, Collections.emptyList(),
                null, null, Collections.emptyList()
        ),
        "Jenkinsfile"
)
definition.setLightweight(true)
pipelineJob.setDefinition(definition)
pipelineJob.save()

println("Pipeline job '${jobName}' has been successfully created!")

Here is the plugin.txt

workflow-aggregator
git
job-dsl
ws-cleanup
docker-plugin
docker-workflow
docker-commons

Here is the Dockerfile shown below

FROM jenkins/jenkins:lts

# Plugin list
COPY plugins.txt /usr/share/jenkins/ref/plugins.txt
RUN jenkins-plugin-cli --plugin-file /usr/share/jenkins/ref/plugins.txt

# For Groovy scripts, init.d directory
COPY init.groovy.d/ /var/jenkins_home/init.groovy.d/

# Install Docker CLI
USER root
RUN apt-get update && apt-get install -y docker.io

# Install kubectl
RUN curl -LO "/$(curl -L -s .txt)/bin/linux/amd64/kubectl" \
    && chmod +x kubectl \
    && mv kubectl /usr/local/bin/

# Install Minikube
RUN curl -LO  \
    && chmod +x minikube-linux-amd64 \
    && mv minikube-linux-amd64 /usr/local/bin/minikube

Here is the docker-compose.yml

version: '3.9'

services:
  jenkins:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: jenkins-server
    ports:
      - "8080:8080"    # Expose Jenkins UI on port 8080
      - "50000:50000"  # Expose port for Jenkins agents
    volumes:
      - jenkins_home:/var/jenkins_home   # Persistent Jenkins data
      - /var/run/docker.sock.raw:/var/run/docker.sock # Mount Docker socket for Docker builds
      - ../k8s:/var/jenkins_home/k8s # Mount Kubernetes configuration files (optional)
      - ./init.groovy.d:/var/jenkins_home/init.groovy.d # Mount Jenkins init scripts (optional)
    environment:
      JAVA_OPTS: "-Djenkins.install.runSetupWizard=false" # Skip setup wizard (optional)
    user: root # Run as root to allow installing dependencies

volumes:
  jenkins_home:

Here is the Jenkinsfile shown below

pipeline {
    agent any

    environment {
        GIT_REPO_URL = 'git-repo-url'
        BRANCH_NAME = 'branch-name-url'
        DOCKERHUB_USERNAME = 'dockerhub-username'
        DOCKER_IMAGE_NAME = 'docker-image-name'
    }

    stages {
        stage('Checkout') {
            steps {
                script {
                    checkout([
                        $class: 'GitSCM',
                        branches: [[name: "*/${env.BRANCH_NAME}"]],
                        userRemoteConfigs: [[url: "${env.GIT_REPO_URL}"]]
                    ])
                }
            }
        }

        stage('Build') {
            agent {
                    docker {
                        image 'maven:3.9.9-amazoncorretto-21-alpine'
                    }
                }
            steps {
                sh 'mvn clean install'
            }
        }

        stage('Build Docker Image') {
            agent {
                docker {
                    image 'docker:27.5.1'
                }
            }
            steps {
                sh "docker build -t ${env.DOCKERHUB_USERNAME}/${env.DOCKER_IMAGE_NAME}:latest ."
            }
        }

        stage('Push Docker Image') {
            agent {
                docker {
                    image 'docker:27.5.1'
                }
            }
            steps {
                withDockerRegistry([credentialsId: 'docker-hub-credentials', url: '']) {
                    sh "docker push ${env.DOCKERHUB_USERNAME}/${env.DOCKER_IMAGE_NAME}:latest"
                }
            }
        }

        stage('Deploy to Minikube') {
            agent any
            steps {
                script {
                    // Start Minikube
                    sh "minikube start"

                    // Open Minikube dashboard (optional, runs in the background)
                    sh "minikube dashboard &"

                    // Apply Kubernetes configurations
                    sh "kubectl apply -f k8s"

                    // Optional: Verify deployment status
                    sh "kubectl get pods -A"
                }
            }
        }

    }

    post {
            always {
                cleanWs(cleanWhenNotBuilt: false,
                        deleteDirs: true,
                        disableDeferredWipeout: true,
                        notFailBuild: true,
                        patterns: [[pattern: '.gitignore', type: 'INCLUDE'],
                                   [pattern: '.propsfile', type: 'EXCLUDE']])
            }
    }
}

How can I fix the issue?

I'm encountering an issue in my Jenkins CI/CD pipeline where Jenkins running in a Docker container is unable to execute the docker command during the build process.Specifically, when the pipeline attempts to start Minikube, I receive the following error

+ minikube start
* minikube v1.35.0 on Debian 12.8 (docker/amd64)
* Automatically selected the docker driver. Other choices: none, ssh
* The "docker" driver should not be used with root privileges. If you wish to continue as root, use --force.
* If you are running minikube within a VM, consider using --driver=none:
*   https://minikube.sigs.k8s.io/docs/reference/drivers/none/

X Exiting due to DRV_AS_ROOT: The "docker" driver should not be used with root privileges.

This error occurs because Minikube detects that it is running as root inside the Jenkins container and refuses to use the Docker driver under these conditions.

How can I fix the issue?

Here are the codes shown below

Here is the groovy file to create pipeline in Jenkins

import hudson.plugins.git.UserRemoteConfig
import hudson.plugins.git.BranchSpec
import hudson.plugins.git.GitSCM
import jenkins.model.*
import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition

def instance = Jenkins.getInstance()

def jobName = "flightsearchapi"
def job = instance.getItem(jobName)

if (job != null) {
    job.delete()
}

def pipelineJob = instance.createProject(org.jenkinsci.plugins.workflow.job.WorkflowJob, jobName)
def definition = new CpsScmFlowDefinition(
        new GitSCM(
                [
                        new UserRemoteConfig("git-repo-url", null, null, null)
                ],
                [new BranchSpec("*/branch-name")],
                false, Collections.emptyList(),
                null, null, Collections.emptyList()
        ),
        "Jenkinsfile"
)
definition.setLightweight(true)
pipelineJob.setDefinition(definition)
pipelineJob.save()

println("Pipeline job '${jobName}' has been successfully created!")

Here is the plugin.txt

workflow-aggregator
git
job-dsl
ws-cleanup
docker-plugin
docker-workflow
docker-commons

Here is the Dockerfile shown below

FROM jenkins/jenkins:lts

# Plugin list
COPY plugins.txt /usr/share/jenkins/ref/plugins.txt
RUN jenkins-plugin-cli --plugin-file /usr/share/jenkins/ref/plugins.txt

# For Groovy scripts, init.d directory
COPY init.groovy.d/ /var/jenkins_home/init.groovy.d/

# Install Docker CLI
USER root
RUN apt-get update && apt-get install -y docker.io

# Install kubectl
RUN curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" \
    && chmod +x kubectl \
    && mv kubectl /usr/local/bin/

# Install Minikube
RUN curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 \
    && chmod +x minikube-linux-amd64 \
    && mv minikube-linux-amd64 /usr/local/bin/minikube

Here is the docker-compose.yml

version: '3.9'

services:
  jenkins:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: jenkins-server
    ports:
      - "8080:8080"    # Expose Jenkins UI on port 8080
      - "50000:50000"  # Expose port for Jenkins agents
    volumes:
      - jenkins_home:/var/jenkins_home   # Persistent Jenkins data
      - /var/run/docker.sock.raw:/var/run/docker.sock # Mount Docker socket for Docker builds
      - ../k8s:/var/jenkins_home/k8s # Mount Kubernetes configuration files (optional)
      - ./init.groovy.d:/var/jenkins_home/init.groovy.d # Mount Jenkins init scripts (optional)
    environment:
      JAVA_OPTS: "-Djenkins.install.runSetupWizard=false" # Skip setup wizard (optional)
    user: root # Run as root to allow installing dependencies

volumes:
  jenkins_home:

Here is the Jenkinsfile shown below

pipeline {
    agent any

    environment {
        GIT_REPO_URL = 'git-repo-url'
        BRANCH_NAME = 'branch-name-url'
        DOCKERHUB_USERNAME = 'dockerhub-username'
        DOCKER_IMAGE_NAME = 'docker-image-name'
    }

    stages {
        stage('Checkout') {
            steps {
                script {
                    checkout([
                        $class: 'GitSCM',
                        branches: [[name: "*/${env.BRANCH_NAME}"]],
                        userRemoteConfigs: [[url: "${env.GIT_REPO_URL}"]]
                    ])
                }
            }
        }

        stage('Build') {
            agent {
                    docker {
                        image 'maven:3.9.9-amazoncorretto-21-alpine'
                    }
                }
            steps {
                sh 'mvn clean install'
            }
        }

        stage('Build Docker Image') {
            agent {
                docker {
                    image 'docker:27.5.1'
                }
            }
            steps {
                sh "docker build -t ${env.DOCKERHUB_USERNAME}/${env.DOCKER_IMAGE_NAME}:latest ."
            }
        }

        stage('Push Docker Image') {
            agent {
                docker {
                    image 'docker:27.5.1'
                }
            }
            steps {
                withDockerRegistry([credentialsId: 'docker-hub-credentials', url: '']) {
                    sh "docker push ${env.DOCKERHUB_USERNAME}/${env.DOCKER_IMAGE_NAME}:latest"
                }
            }
        }

        stage('Deploy to Minikube') {
            agent any
            steps {
                script {
                    // Start Minikube
                    sh "minikube start"

                    // Open Minikube dashboard (optional, runs in the background)
                    sh "minikube dashboard &"

                    // Apply Kubernetes configurations
                    sh "kubectl apply -f k8s"

                    // Optional: Verify deployment status
                    sh "kubectl get pods -A"
                }
            }
        }

    }

    post {
            always {
                cleanWs(cleanWhenNotBuilt: false,
                        deleteDirs: true,
                        disableDeferredWipeout: true,
                        notFailBuild: true,
                        patterns: [[pattern: '.gitignore', type: 'INCLUDE'],
                                   [pattern: '.propsfile', type: 'EXCLUDE']])
            }
    }
}

How can I fix the issue?

Share Improve this question asked Jan 30 at 19:34 Sercan Noyan GermiyanoğluSercan Noyan Germiyanoğlu 2,7864 gold badges53 silver badges124 bronze badges 3
  • The error message says If you wish to continue as root, use --force. Does that help? Or, can you remove the user: root line in the Compose file (you shouldn't normally need to install software into a running container)? – David Maze Commented Jan 30 at 21:19
  • @DavidMaze I tried to remove user: root from docker-compose.yml file but nothing changed. I got the same error again. – Sercan Noyan Germiyanoğlu Commented Jan 31 at 6:00
  • @DavidMaze I still couldn't fix the issue. – Sercan Noyan Germiyanoğlu Commented Feb 2 at 12:30
Add a comment  | 

1 Answer 1

Reset to default 0

There are many problems with the described setup and a discussion on them would be out of scope of a StackOverflow answer. But the root cause of your problem seems to be that since you switched to USER root in your Dockerfile, you're running all the processes within the resulting image as root, including Jenkins controller, that is, in turn, running Minikube from the pipeline (unless agent any assigns the stage to another agent - but you didn't mention you have real agents and I don't see any signs of that based on the given code).

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