Jenkins pipeline fails with docker not found message

Hello Team,

I have installed Jenkins in Docker locally on my Mac using instruction from official documentation:

All works well but pipeline fails during Build stage with following error message:

/var/jenkins_home/workspace/rst-security-jenkins-code_master@tmp/durable-8621d176/script.sh: line 1: docker: not found

script returned exit code 127

This is my Jenkinsfile:

pipeline {
  environment {
    registry = "mydockerhub/my-cicd-app"
    registryCredential = 'dockerhub'
    dockerImage = ''
  }
  agent { dockerfile true }
  // agent { 
  //   docker { 
  //     image 'python:alpine3.7'
  //     args '-p 5000:5000'
  //       } 
  // }
  stages {
    stage('Build') {
      steps {
        sh 'pip install -r requirements.txt'
        sh 'apk add libstdc++'
        sh 'python ./app.py &'
      }
    }
    stage('Test App') {
      steps {
        echo "${env.NODE_NAME}"
        sh 'pwd'
        sh 'uname -a'
        sh 'python test.py'
      }
      post {
        always {
          junit 'test-reports/*.xml'
        }
      } 
    }
    // Uncomment for SAST lab step 
    // Commented section starts
    /*
    stage('SAS Test') {
      steps {
        snykSecurity(
          snykInstallation: 'SnykV2Plugin',
          snykTokenId: 'snyktoken',
          severity: 'medium',
          failOnIssues: true)
      }
    }
    */
    // Commented section ends 
    stage('Build image') {
      steps{
        script {
          dockerImage = docker.build registry + ":$BUILD_NUMBER"
        }
      }
    }
    stage('Upload Image to Registry') {
      steps{
        script {
          docker.withRegistry( '', registryCredential ) {
            dockerImage.push()
          }
        }
      }
    }
    stage('Remove Unused docker image') {
      steps{
        sh "docker rmi $registry:$BUILD_NUMBER"
      }
    }
    // Uncomment for K8s app diployment step
    // Commented section starts
    /*
    stage('Deploy Application') {
      agent {
        kubernetes {
            cloud 'kubernetes'
          }
        }
        steps {
          container('kubectl') {
            sh """cat <<EOF | kubectl apply --validate=false -f -
apiVersion: v1
kind: Namespace
metadata:
  name: my-cicd-app
---
apiVersion: v1
kind: Service
metadata:
  name: my-service
  namespace: my-cicd-app
spec:
  selector:
    app: my-cicd-app
  ports:
    - protocol: TCP
      port: 5000
      targetPort: 5000
  type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-demo
  namespace: my-cicd-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-cicd-app
  template:
    metadata:
      labels:
        app: my-cicd-app
    spec:
      containers:
      - name: my-cicd-app
        image: $registry:$BUILD_NUMBER
        imagePullPolicy: Always
        ports:
        - containerPort: 5000
EOF"""
        }
      }
    }
   */ 
  // Commented section ends
  }
}

What could be the issue here? What’s more interesting that when I attach to jenkins (or dind) container and move into /var/jenkins_home/workspace/rst-security-jenkins-code_master directory, I am able to manually build image using docker build command…

Thanks for any comments!
Luke

[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/test
[Pipeline] {
[Pipeline] isUnix
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ docker inspect -f . node:7-alpine
/var/jenkins_home/workspace/test@tmp/durable-1e0eb9b6/script.sh: 1: docker: not found
[Pipeline] isUnix
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ docker pull node:7-alpine
/var/jenkins_home/workspace/test@tmp/durable-4b97b766/script.sh: 1: docker: not found
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
version: '3.7'

services:
  jenkins:
    image: jenkins/jenkins:jdk11
    container_name: jenkins
    restart: always
    privileged: true
    user: root
    #user: 999:999
    healthcheck:
      test: curl -s https://localhost:8080 >/dev/null; if [[ $$? == 52 ]]; then echo 0; else echo 1; fi
      interval: 1m
      timeout: 5s
      retries: 3
    ports:
      - '8080:8080'
    networks:
      - default
    volumes:
      - '/docker/jenkins/jenkins_home:/var/jenkins_home'
      - '/etc/localtime:/etc/localtime:ro'
      - '/var/run/docker.sock:/var/run/docker.sock'
      - '~/.m2:/root/.m2'

same error here

Jenkins docker image does not include docket cli. It’s recommend you connect other agents (ssh, cloud, jnlp, etc) rather than using the controller directly.

If you want to use the controller you’ll need to extend the image and add the tools you need. There are docs on how to do it but I’m on.my phone atm

thanks for reply
after did some research
i find the way make it worked !
https://stackoverflow.com/questions/72990497/getting-glibc-2-32-and-glibc-2-34-not-found-in-jenkins-docker-with-dind-on
i changed my docker-compose file to

version: '3.9'

services:
  jenkins:
    build:
      dockerfile: Dockerfile
      context: .
    container_name: jenkins
    restart: always
    privileged: true
    user: root

    healthcheck:
      test: curl -s https://localhost:8080 >/dev/null; if [[ $$? == 52 ]]; then echo 0; else echo 1; fi
      interval: 1m
      timeout: 5s
      retries: 3

    ports:
      - '8080:8080'
    networks:
      - default
    volumes:
      - '/app:/app'
      - '/usr/share/fonts:/usr/share/fonts'
      - '/docker/jenkins/jenkins_home:/var/jenkins_home'
      - '/etc/localtime:/etc/localtime:ro'
      - '/var/run/docker.sock:/var/run/docker.sock'
      - '~/.ssh:/var/jenkins_home/.ssh'
      - '~/.m2:/root/.m2'
networks:
  default:

and then Dockerfile

# https://github.com/jenkinsci/docker/blob/master/README.md
FROM jenkins/jenkins:lts
MAINTAINER blankhang@gmil.com

USER root

# install docker cli
RUN apt-get -y update; apt-get install -y sudo; apt-get install -y git wget
RUN echo "Jenkins ALL=NOPASSWD: ALL" >> /etc/sudoers
RUN wget http://get.docker.com/builds/Linux/x86_64/docker-latest.tgz
RUN tar -xvzf docker-latest.tgz
RUN mv docker/* /usr/bin/

# update system and install chinese language support and maven nodejs
RUN apt-get update && apt-get install -y locales locales-all maven nodejs \
    && sed -i '/^#.* zh_CN.UTF-8 /s/^#//' /etc/locale.gen \
    && locale-gen \
    && rm -rf /var/lib/apt/lists/* \

# Setting Default Chinese Language and UTC+8 timezone
#ENV LANG C.UTF-8
ENV LANG zh_CN.UTF-8
ENV LANGUAGE zh_CN.UTF-8
ENV LC_ALL zh_CN.UTF-8
ENV TZ Asia/Shanghai

USER Jenkins

after this

[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/test
[Pipeline] {
[Pipeline] isUnix
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ docker inspect -f . node:7-alpine

Error: No such object: node:7-alpine
[Pipeline] isUnix
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ docker pull node:7-alpine
7-alpine: Pulling from library/node
90f4dba627d6: Pulling fs layer
1e674d353187: Pulling fs layer
d3a64c0f885a: Pulling fs layer
d3a64c0f885a: Verifying Checksum
d3a64c0f885a: Download complete
90f4dba627d6: Verifying Checksum
90f4dba627d6: Download complete
90f4dba627d6: Pull complete
1e674d353187: Verifying Checksum
1e674d353187: Download complete
1e674d353187: Pull complete
d3a64c0f885a: Pull complete
Digest: sha256:4954ce53247180e207772f936223b11d52a7e4ee712dfe73fe2a75e39f785067
Status: Downloaded newer image for node:7-alpine
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] withDockerContainer
Jenkins seems to be running inside container 9d84b70f7ec3d76685c852352aa3b8d4314c97f191e5414186c26026e43b3407
$ docker run -t -d -u 0:0 -w /var/jenkins_home/workspace/test --volumes-from 9d84b70f7ec3d76685c852352aa3b8d4314c97f191e5414186c26026e43b3407 -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** node:7-alpine cat
$ docker top 86ff055eb44c7427563b6110f8f7414c6f40a951d937384b4edb9711bc453fa1 -eo pid,comm
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] sh
+ node --version
v7.10.1
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
$ docker stop --time=1 86ff055eb44c7427563b6110f8f7414c6f40a951d937384b4edb9711bc453fa1
$ docker rm -f --volumes 86ff055eb44c7427563b6110f8f7414c6f40a951d937384b4edb9711bc453fa1
[Pipeline] // withDockerContainer
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

What if I instlled docker locally and I used this docker -compose file

version: “3.8”
services:
jenkins:
container_name: spreezy_jenkins
image: jenkins/jenkins:lts-jdk21
privileged: true
user: root
ports:
- 6833:8080
- 50000:50000
volumes:
- $PWD/jenkins_home:/var/jenkins_home
- /var/run/docker.sock:/var/run/docker.sock

restart: unless-stopped