Answers: <p>Your pipeline is executing a <code>shell</code> stage, which exits with 127 -- this indicates that the command or program you tried to execute in the shell was not found.
  In your case, this was <code>docker</code> -- <em>i.e.</em> your Jenkins agent is not finding the <code>docker</code> command in its environment.</p>
  <p>It looks like you're using the Jenkins controller as the agent as well, so the agent itself is running inside the same container as the controller. In order to make this pipeline work, you would need to provide access to the Docker socket.</p>
  <p>In short, you need to mount the socket into the Jenkins container by binding it as a volume, as you did with Jenkins home.</p>
  <p>Furthermore, since the binary was not found, you will need to add it to the Jenkins container (<em>i.e.</em> you need to install docker inside the Jenkins container).</p>
  <p>See <a href="https://stackoverflow.com/a/39469893/2707870">this thread</a></p>
  <p>However, I should point out that if your objective is to learn how to use or deploy Jenkins, this is the completely wrong way to do it.</p>
  <p>First of all, running an agent on the controller means that jobs can break the controller.
  Secondly running Docker from inside Docker can lead to unexpected consequences, unless it's done right. See <a href="https://devops.stackexchange.com/a/681/354">this thread</a>
  Finally, it obviously won't scale, but I suspect this might not be an issue for you until later, if ever.</p>
  <p>So, long story short I would:</p>
  <ol>
  <li>Add a Docker &quot;cloud&quot; to your controller</li>
  <li>Create an agent compatible with your Jenkins and build environment</li>
  <li>Define your jobs to use that agent</li>
  </ol>
  <p>All the details are on the Jenkins agent setup page:
  <a href="https://www.jenkins.io/doc/book/using/using-agents/" rel="nofollow noreferrer">https://www.jenkins.io/doc/book/using/using-agents/</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2024-10-13T05:57:46.510Z
Answers: 2024-10-13T05:57:46.510Z
Answers: Bruce Becker
Body: <p>I am learning Jenkins. I have created Jenkins container with docker and trying to run jenkins from there.</p>
  <pre class="lang-yml prettyprint-override"><code>services:
    jenkins:
      image: jenkins/jenkins:lts
      container_name: jenkins
      restart: always
      ports:
        - &quot;8080:8080&quot;
        - &quot;50000:50000&quot;
      volumes:
        - jenkins_home:/var/jenkins_home
      networks:
        - jenkins
  
  networks:
    jenkins:
  
  volumes:
    jenkins_home:
  </code></pre>
  <p>I have also installed docker-pipline plugin to my Jenkins Container. I checked the restart api button after the plugin is installed.
  <a href="https://i.sstatic.net/E4GnjhgZ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/E4GnjhgZ.png" alt="enter image description here" /></a></p>
  <p>My script for pipeline job is:</p>
  <pre class="lang-bash prettyprint-override"><code>pipeline {
      agent any
  
      stages {
          stage('w/o docker') {
              steps {
                  sh 'echo &quot;Without docker&quot;'
              }
          }
          stage('w docker') {
              agent{
                  docker{
                      image 'node:18-alpine'
                  }
              }
              steps {
                  sh 'echo &quot;With docker&quot;'
              }
          }
      }
  }
  
  </code></pre>
  <p>But when I build it I am getting the following error:</p>
  <pre class="lang-sh prettyprint-override"><code>Started by user Fahad Md kamal
  [Pipeline] Start of Pipeline
  [Pipeline] node
  Running on Jenkins in /var/jenkins_home/workspace/Docker build Pipeline
  [Pipeline] {
  [Pipeline] stage
  [Pipeline] { (w/o docker)
  [Pipeline] sh
  + echo Without docker
  Without docker
  [Pipeline] }
  [Pipeline] // stage
  [Pipeline] stage
  [Pipeline] { (w docker)
  [Pipeline] node
  Running on Jenkins in /var/jenkins_home/workspace/Docker build Pipeline@2
  [Pipeline] {
  [Pipeline] isUnix
  [Pipeline] withEnv
  [Pipeline] {
  [Pipeline] sh
  + docker inspect -f . node:18-alpine
  /var/jenkins_home/workspace/Docker build Pipeline@2@tmp/durable-5dab51a9/script.sh.copy: 1: docker: not found
  [Pipeline] isUnix
  [Pipeline] withEnv
  [Pipeline] {
  [Pipeline] sh
  + docker pull node:18-alpine
  /var/jenkins_home/workspace/Docker build Pipeline@2@tmp/durable-74f03549/script.sh.copy: 1: docker: not found
  [Pipeline] }
  [Pipeline] // withEnv
  [Pipeline] }
  [Pipeline] // withEnv
  [Pipeline] }
  [Pipeline] // node
  [Pipeline] }
  [Pipeline] // stage
  [Pipeline] }
  [Pipeline] // node
  [Pipeline] End of Pipeline
  ERROR: script returned exit code 127
  Finished: FAILURE
  </code></pre>
  <p>I have no idea what I am doing wrong here, and also kind of getting frustrated with it since I am facing the problem for over weeks now. I would be glad if you could help me.</p>
  
Tags: <docker><jenkins><jenkins-pipeline><docker-compose><jenkins-plugins>
Title: Jenkins pipeline job cannot access Docker Engine
UserDisplayName: Fahad Md Kamal
ThreadText: # Jenkins pipeline job cannot access Docker Engine
  
  (Asked by: username_0 on 2024-10-07)
  
  I am learning Jenkins. I have created Jenkins container with docker and trying to run jenkins from there.
  
  ```yml
  services:
    jenkins:
      image: jenkins/jenkins:lts
      container_name: jenkins
      restart: always
      ports:
        - "8080:8080"
        - "50000:50000"
      volumes:
        - jenkins_home:/var/jenkins_home
      networks:
        - jenkins
  
  networks:
    jenkins:
  
  volumes:
    jenkins_home:
  
  ```
  
  I have also installed docker-pipline plugin to my Jenkins Container. I checked the restart api button after the plugin is installed.
  [![enter image description here](https://i.sstatic.net/E4GnjhgZ.png)](https://i.sstatic.net/E4GnjhgZ.png)
  
  My script for pipeline job is:
  
  ```bash
  pipeline {
      agent any
  
      stages {
          stage('w/o docker') {
              steps {
                  sh 'echo "Without docker"'
              }
          }
          stage('w docker') {
              agent{
                  docker{
                      image 'node:18-alpine'
                  }
              }
              steps {
                  sh 'echo "With docker"'
              }
          }
      }
  }
  
  ```
  
  But when I build it I am getting the following error:
  
  ```sh
  Started by user username_0 Md username_0
  [Pipeline] Start of Pipeline
  [Pipeline] node
  Running on Jenkins in /var/jenkins_home/workspace/Docker build Pipeline
  [Pipeline] {
  [Pipeline] stage
  [Pipeline] { (w/o docker)
  [Pipeline] sh
  + echo Without docker
  Without docker
  [Pipeline] }
  [Pipeline] // stage
  [Pipeline] stage
  [Pipeline] { (w docker)
  [Pipeline] node
  Running on Jenkins in /var/jenkins_home/workspace/Docker build Pipeline@2
  [Pipeline] {
  [Pipeline] isUnix
  [Pipeline] withEnv
  [Pipeline] {
  [Pipeline] sh
  + docker inspect -f . node:18-alpine
  /var/jenkins_home/workspace/Docker build Pipeline@2@tmp/durable-5dab51a9/script.sh.copy: 1: docker: not found
  [Pipeline] isUnix
  [Pipeline] withEnv
  [Pipeline] {
  [Pipeline] sh
  + docker pull node:18-alpine
  /var/jenkins_home/workspace/Docker build Pipeline@2@tmp/durable-74f03549/script.sh.copy: 1: docker: not found
  [Pipeline] }
  [Pipeline] // withEnv
  [Pipeline] }
  [Pipeline] // withEnv
  [Pipeline] }
  [Pipeline] // node
  [Pipeline] }
  [Pipeline] // stage
  [Pipeline] }
  [Pipeline] // node
  [Pipeline] End of Pipeline
  ERROR: script returned exit code 127
  Finished: FAILURE
  
  ```
  
  I have no idea what I am doing wrong here, and also kind of getting frustrated with it since I am facing the problem for over weeks now. I would be glad if you could help me.
  
  **Tags:** docker, jenkins, jenkins-pipeline, docker-compose, jenkins-plugins
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-10-13. Score: 1
  
  Your pipeline is executing a `shell` stage, which exits with 127 -- this indicates that the command or program you tried to execute in the shell was not found.
  In your case, this was `docker` -- *i.e.* your Jenkins agent is not finding the `docker` command in its environment.
  
  It looks like you're using the Jenkins controller as the agent as well, so the agent itself is running inside the same container as the controller. In order to make this pipeline work, you would need to provide access to the Docker socket.
  
  In short, you need to mount the socket into the Jenkins container by binding it as a volume, as you did with Jenkins home.
  
  Furthermore, since the binary was not found, you will need to add it to the Jenkins container (*i.e.* you need to install docker inside the Jenkins container).
  
  See [this thread](https://stackoverflow.com/a/39469893/2707870)
  
  However, I should point out that if your objective is to learn how to use or deploy Jenkins, this is the completely wrong way to do it.
  
  First of all, running an agent on the controller means that jobs can break the controller.
  Secondly running Docker from inside Docker can lead to unexpected consequences, unless it's done right. See [this thread](https://devops.stackexchange.com/a/681/354)
  Finally, it obviously won't scale, but I suspect this might not be an issue for you until later, if ever.
  
  So, long story short I would:
  
  1. Add a Docker "cloud" to your controller
  2. Create an agent compatible with your Jenkins and build environment
  3. Define your jobs to use that agent
  
  All the details are on the Jenkins agent setup page:
  <https://www.jenkins.io/doc/book/using/using-agents/>
  
  
  ---
  
  