1

I have a Jenkins pipeline which is running fine but it depends upon JDK and maven installed tools. There were few instances in the past that these JDK and maven tool's name was changed(e.g. Maven 3.6.2 -> Maven 3.6.3 and it results in my pipeline failure.

stage ("build") {
        withMaven(jdk: 'Java SE 8u221', maven: 'Maven 3.6.3', tempBinDir: '') {
            sh 'mvn clean package jib:dockerBuild verify'
        }
    }

I want my pipeline to be independent of what tools are installed. So I rewrite my Jenkins pipeline like below to provide docker image of maven(since JDK is bundled with it)

pipeline {
   agent {
        docker {
            image 'maven:3-alpine' 
            args '-v /root/.m2:/root/.m2' 
        }
    }

    stages {
        stage('Checkout') {

            steps {
                git branch: "master", url: "repo url", credentialsId: 'id'
            }
         }

          stage ("build") {
              steps {
                sh 'mvn clean package jib:dockerBuild verify'
              }
        }
    }
}

But now I am getting an error Failed to execute goal com.google.cloud.tools:jib-maven-plugin:2.3.0:dockerBuild (default-cli) : Build to Docker daemon failed, perhaps you should make sure Docker is installed and you have correct privileges to run it

It seems that docker daemon is not visible after I provided a maven docker image.

Andrew Nepogoda
  • 1,825
  • 17
  • 24
Tarun Bharti
  • 185
  • 3
  • 17
  • Hi Tarun.., you're trying to build a docker image using the maven tools. And for this you need to have a docker-client installed in your docker image `maven:3-alpine` and should pass a env pointing to the `REMOTE_DOCKER_HOST` so that it can talk to the daemon and build the image as needed. – vijay v May 25 '20 at 15:20

1 Answers1

2

I did solve this by adding docker agent inside of my maven docker image

pipeline {
     agent any

    stages {

         stage('build Dockerfile') {

            steps {
                sh '''echo "FROM maven:3-alpine
                          RUN apk add --update docker openrc
                          RUN rc-update add docker boot" >/var/lib/jenkins/workspace/Dockerfile'''

            }
         }

         stage('run Dockerfile') {
             agent{
                 dockerfile {
                            filename '/var/lib/jenkins/workspace/Dockerfile'
                            args '--user root -v $HOME/.m2:/root/.m2  -v /var/run/docker.sock:/var/run/docker.sock'
                        }
             }

             steps {
                 sh 'docker version'
                 sh 'mvn -version'
                 sh 'java -version'
             }

         }

    }
}
Tarun Bharti
  • 185
  • 3
  • 17