Jenkins Declarative pipeline docker: command not f

2019-08-29 02:27发布

问题:

I have installed Jenkins locally on my machine. I pulling some source code from my github repository which has a Docker file in the root directory. I want to build and docker image and push it to docker hub but my build fails with the following message...

docker build -f myapp-web:latest . \n
/Users/Shared/Jenkins/Home/workspace/MyApp@tmp/durable-ee9851e9/script.sh: line 1: docker: command not found 
pipeline {

    agent any

    tools {
        maven 'maven_3.6.1'
        jdk 'jdk8'
    }

    stages {
        stage('Build') {
            steps {
                withMaven(maven: 'maven_3.6.1', mavenSettingsConfig: '5d7a8237-6d6a-4189-a907-518900dc7755') {
                    sh "mvn clean install "
                }
            }
        }
        stage('Build Image') {
            steps {
                script {
                    sh 'docker build -f myapp-web:latest .'
                }
            }
        }
        stage('Deploy Image') {
            steps {
                withDockerRegistry([credentialsId: "docker-hub", url: ""]) {
                    sh 'docker push myapp-web:latest'
                }
            }
        }
    }
}

Docker is install and running on my local machine and Jenkins is configured with the following plugin Docker pipeline Docker plugin

Any ideas greatly appreciated

回答1:

If you are getting docker: command not found through the pipeline although its already installed on the same node where the pipeline is running, you need to ensure that the pipeline is reading the correct $PATH environment where the docker binary should be exist.

For the second issue you have mentioned

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock

you need to add the user that is being used within the pipeline to the docker group so it can run docker commands using:

usermod -aG docker $USER