While adding docker in path for jenkins pipeline

When I am trying to add Docker to the environment path in my jenkins pipeline, I am getting the following error:

  • docker version
    Client: Docker Engine - Community
    Version: 27.1.1
    API version: 1.46
    Go version: go1.21.12
    Git commit: 6312585
    Built: Tue Jul 23 19:57:19 2024
    OS/Arch: linux/amd64
    Context: default
    error during connect: Get “https://docker:2376/v1.46/version”: dial tcp: lookup docker on 127.0.0.11:53: no such host

Due to this, I am unable to move forward with my pipeline.

Below is the Jenkins pipeline:

// Scripted

// Declarative
pipeline {
	agent any
	//agent {docker {image 'maven:3.6.3'}}
	environment {
		dockerHome = tool 'MyDocker'
		mavenHome = tool 'myMaven'
		PATH = "$dockerHome/bin:$mavenHome/bin:$PATH"
	}
	stages {
		stage('Chekout') {
			steps {
				sh 'mvn --version'
				sh 'docker version'
				echo "Build"
				echo "PATH - $PATH"
				echo "BUILD NUMBER - $env.BUILD_NUMBER"
				echo "BUILD ID - $env.BUILD_ID"
				echo "JOB NAME - $env.JOB_NAME"
				echo "BUILD TAG - $env.BUILD_TAG"
				echo "BUILD URL - $env.BUILD_URL"
			}
		}

		stage('Compile') {
			steps {
				sh "mvn clean compile"
			}
		}

		stage('Dependency Check') {
			steps {
				sh "mvn dependency:tree"
			}
		}

		stage('Test') {
			steps {
				sh "mvn test"
			}
		}

		stage('Integeration Test') {
			steps {
				sh "mvn failsave:integeration-test failsafe:verify"
			}
		}
	}

	post {
		always {
			echo 'Im awesome. I run always'
		}
		success {
			echo 'I run when you are successful'
		}
		failure {
			echo 'I run when you fail'
		}
	}
}

It seems to be a Jenkins issue rather than Docker. You need a valid Docker context to connect to the docker daemon and the hostname “docker” is not it. It must be some default hostname which I saw only in Docker in Docker (Docker daemon in a Docker container).

The Jenkins documentation shows a way to use Docker in your pipeline

I see you tried something like that and commented out. Jenkins does not use the docker command in the documentation, but if you want to, you need a valid docker context.

Or at least a Docker host, which is also mentioned in the documentation.

At first, I added the maven and docker to my environment and then it ran successfully for two builds. Later, when I added the stages for maven compile and maven tests, there were errors with running maven tests. When I resolved that issue, this issue cropped up.