I'm encountering an issue in my Jenkins CI/CD pipeline where Jenkins running in a Docker container is unable to find the docker command during the build process. Below is the setup:
Note : As I work with Windows , I change /var/run/docker.sock:/var/run/docker.sock
to /var/run/docker.sock.raw:/var/run/docker.sock
to detect testcontainer in the pipeline. Otherwise I cannot run integration.
Here is the groovy file shown below
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("https://github.com/Rapter1990/flightsearchapi.git", null, null, null)
],
[new BranchSpec("*/development/issue-2/implement-jenkins-for-ci-cd")],
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
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 {
docker {
image 'maven:3.9.9-amazoncorretto-21-alpine'
args '-v /root/.m2:/root/.m2'
}
}
environment {
GIT_REPO_URL = 'https://github.com/Rapter1990/flightsearchapi.git'
BRANCH_NAME = 'development/issue-2/implement-jenkins-for-ci-cd'
DOCKERHUB_USERNAME = 'noyandocker'
DOCKER_IMAGE_NAME = 'flightsearchapi-jenkins'
}
stages {
stage('Checkout') {
steps {
script {
checkout([
$class: 'GitSCM',
branches: [[name: "*/${env.BRANCH_NAME}"]],
userRemoteConfigs: [[url: "${env.GIT_REPO_URL}"]]
])
}
}
}
stage('Build') {
steps {
sh 'mvn clean install'
}
}
stage('Build Docker Image') {
steps {
sh "docker build -t ${env.DOCKERHUB_USERNAME}/${env.DOCKER_IMAGE_NAME}:latest ."
}
}
stage('Push Docker Image') {
steps {
withDockerRegistry([credentialsId: 'docker-hub-credentials', url: '']) {
sh "docker push ${env.DOCKERHUB_USERNAME}/${env.DOCKER_IMAGE_NAME}:latest"
}
}
}
}
post {
always {
cleanWs(cleanWhenNotBuilt: false,
deleteDirs: true,
disableDeferredWipeout: true,
notFailBuild: true,
patterns: [[pattern: '.gitignore', type: 'INCLUDE'],
[pattern: '.propsfile', type: 'EXCLUDE']])
}
}
}
In the pipeline, I am attempting to build and push a Docker image as part of the CI/CD process, but the error message shows
+ docker build -t noyandocker/flightsearchapi-jenkins:latest .
/var/jenkins_home/workspace/flightsearchapi@tmp/durable-7294ca4e/script.sh.copy: line 1: docker: not found
Can you fix the issue ?
The issue disappeared after I followed these steps shown below
1 ) Change agent as any from docker image
2 ) Define mvn for build step and docker for its relevant step
Here is the code shown below.
pipeline {
agent any
...
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"
}
}
}
...
}