title: Build docker image in Jenkins (in docker container) pipeline
body: <p>I use Jenkins from docker container. And I want to build docker image in Jenkins pipeline but docker is not exist in this container (where Jenkins).</p>
  
  <p>Jenkins container deployed by Docker Compose, yml file:</p>
  
  <pre><code>version: "3.3"
  services:
    jenkins:
      image: jenkins:alpine
      ports:
        - 8085:8080
      volumes:
        - ./FOR_JENKINS:/var/jenkins_home
  </code></pre>
  
  <p>What we can do to build docker image in Jenkins pipeline?
  Can we deploy some docker container with docker and use once for build docker image? or something else? How do you doing with they?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Thanks @VonC, I checked your information, but... "permission denied"</p>
  
  <p>Docker Compose file:</p>
  
  <pre><code>version: "3.3"
  services:
    jenkins:
      image: jenkins:alpine
      ports:
        - 8085:8080
      volumes:
        - ./FOR_JENKINS:/var/jenkins_home
  #      - /var/run/docker.sock:/var/run/docker.sock:rw
        - /var/run:/var/run:rw
  </code></pre>
  
  <p>Jenkinsfile:</p>
  
  <pre><code>pipeline {
      agent any
  
      stages {
          stage('Build') {
              steps {
                  echo "Compiling..."
                  sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt compile"
              }
          }
          /*stage('Unit Test') {
              steps {
                  echo "Testing..."
                  sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt coverage 'test-only * -- -F 4'"
                  sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt coverageReport"
                  sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt scalastyle || true"
              }
          }*/
          stage('DockerPublish') {
              steps {
                  echo "Docker Stage ..."
                  // Generate Jenkinsfile and prepare the artifact files.
                  sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt docker:stage"
  
                  echo "Docker Build-2 ..."
                  // Run the Docker tool to build the image
                  script {
                      docker.withTool('docker') {
  
                          echo "D1- ..."
                          //withDockerServer([credentialsId: "AWS-Jenkins-Build-Slave", uri: "tcp://192.168.0.29:2376"]) { 
                              echo "D2- ..."
                              sh "printenv" 
                              echo "D3- ..."
                              //sh "docker images" 
                              echo "D4- ..."
                              docker.build('my-app:latest', 'target/docker/stage').inside("--volume=/var/run/docker.sock:/var/run/docker.sock")
                              echo "D5- ..."
                              //base.push("tmp-fromjenkins") 
                          //}
  
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>[job1] Running shell script
  
  + docker build -t my-app:latest target/docker/stage
  
  Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.29/build?buildargs=%7B%7D&amp;cachefrom=%5B%5D&amp;cgroupparent=&amp;cpuperiod=0&amp;cpuquota=0&amp;cpusetcpus=&amp;cpusetmems=&amp;cpushares=0&amp;dockerfile=Dockerfile&amp;labels=%7B%7D&amp;memory=0&amp;memswap=0&amp;networkmode=default&amp;rm=1&amp;shmsize=0&amp;t=my-app%3Alatest&amp;target=&amp;ulimits=null: dial unix /var/run/docker.sock: connect: permission denied
  
  script returned exit code 1
  </code></pre>
  
  <p><strong>Edit:</strong>
  Last problem with "permission denied" fixed with:</p>
  
  <pre><code>&gt;&gt;sudo chmod 0777 /var/run/docker.sock
  </code></pre>
  
  <p><strong>Worked State:</strong></p>
  
  <p>Call in host:</p>
  
  <pre><code>&gt;&gt;sudo chmod 0777 /var/run/docker.sock
  </code></pre>
  
  <p>Docker Compose file:</p>
  
  <pre><code>version: "3.3"
  services:
    jenkins:
      image: jenkins:alpine
      ports:
        - 8085:8080
      volumes:
        - ./FOR_JENKINS:/var/jenkins_home
  #      - /var/run/docker.sock:/var/run/docker.sock:rw
        - /var/run:/var/run:rw
  </code></pre>
  
  <p>Jenkinsfile:</p>
  
  <pre><code>pipeline {
      agent any
  
      stages {
          stage('Build') {
              steps {
                  echo "Compiling..."
                  sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt compile"
              }
          }
          /*stage('Unit Test') {
              steps {
                  echo "Testing..."
                  sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt coverage 'test-only * -- -F 4'"
                  sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt coverageReport"
                  sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt scalastyle || true"
              }
          }*/
          stage('DockerPublish') {
              steps {
                  echo "Docker Stage ..."
                  // Generate Jenkinsfile and prepare the artifact files.
                  sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt docker:stage"
  
                  echo "Docker Build-2 ..."
                  // Run the Docker tool to build the image
                  script {
                      docker.withTool('docker') {
  
                          echo "D1- ..."
                          //withDockerServer([credentialsId: "AWS-Jenkins-Build-Slave", uri: "tcp://192.168.0.29:2376"]) { 
                              echo "D2- ..."
                              sh "printenv" 
                              echo "D3- ..."
                              //sh "docker images" 
                              echo "D4- ..."
                              docker.build('my-app:latest', 'target/docker/stage')
                              echo "D5- ..."
                              //base.push("tmp-fromjenkins") 
                          //}
  
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>My resolve:</strong></p>
  
  <p>I add some step in Jenkinsfile and get:</p>
  
  <pre><code>pipeline {
      agent any
      //def app
  
      stages {
          stage('Build') {
              steps {
                  echo "Compiling..."
                  sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt compile"
              }
          }
  
          stage('DockerPublish') {
              steps {
                  echo "Docker Stage ..."
                  // Generate Jenkinsfile and prepare the artifact files.
                  sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt docker:stage"
  
                  echo "Docker Build ..."
                  // Run the Docker tool to build the image
                  script {
                      docker.withTool('docker') {
  
                              echo "Environment:"
                              sh "printenv" 
                              app = docker.build('ivanbuh/myservice:latest', 'target/docker/stage')
                              echo "Push to Docker repository ..."
                           docker.withRegistry('https://registry.hub.docker.com', 'docker-hub-credentials') {  
                                  app.push("${env.BUILD_NUMBER}")
                                  app.push("latest")
                              }
  
                              echo "Complated ..."
                      }
                  }
              }
          }
  //https://boxboat.com/2017/05/30/jenkins-blue-ocean-pipeline/
  //https://gist.github.com/bvis/68f3ab6946134f7379c80f1a9132057a
          stage ('Deploy') {
              steps {
                  sh "docker stack deploy myservice --compose-file docker-compose.yml"
              }
          }
      }
  }
  </code></pre>