title: Best-practice of setting up Jenkins-in-Docker with Docker-in-Jenkins
body: <p>I am developing an application for which I would like continuous integration using Jenkins, furthermore I would like to be able to control the environment for CI. I am developing locally on OS X, and I am pushing to a remote Ubuntu 16.04 VM. <strong>All of this lead me to Docker.</strong> Basically what I want is Jenkins-in-Docker, but also Docker-in-Jenkins. My build slave(s) will initially be limited to just <em>master</em>.</p>
  
  <p>Now, I've been reading some posts about Docker for the past few days, but I am still not entirely sure I understand it in-depth, yet. Hence this question.</p>
  
  <p>Currently, this is my setup:</p>
  
  <p><strong>Jenkins:</strong></p>
  
  <p><code>/docker-compose.yml</code></p>
  
  <pre class="lang-yaml prettyprint-override"><code>version: '3'
  services:
    jenkins:
      build: ./jenkins
      volumes:
        - /var/run/docker.sock:/var/run/docker.sock # allowing docker-in-docker
        - ./jenkins_home:/var/jenkins_home
      ports:
        - 8080:8080
  </code></pre>
  
  <p><code>/jenkins/Dockerfile</code></p>
  
  <pre><code>FROM docker:17.12 AS docker
  FROM jenkins/jenkins:lts
  
  COPY --from=docker /usr/local/bin/docker /usr/local/bin/docker
  </code></pre>
  
  <p>The above setup works just fine - until I use Docker-in-Jenkins. Jenkins is installed and all, but when I'm building any jobs that would then <em>use</em> Docker, I get an error that </p>
  
  <blockquote>
    <p>Got permission denied while trying to connect to the Docker daemon
    socket at unix:///var/run/docker.sock: Post (...) dial unix
    /var/run/docker.sock: connect: permission denied</p>
  </blockquote>
  
  <p><strong>CI Source:</strong></p>
  
  <p><code>/Jenkinsfile</code></p>
  
  <pre class="lang-groovy prettyprint-override"><code>pipeline {
    agent { dockerfile true }
    stages {
      stage('Initial') {
        steps {
          println 'Hello world!'
        }
      }
    }
  }
  </code></pre>
  
  <p>/Dockerfile</p>
  
  <pre><code>FROM python:3.6-slim-jessie
  COPY requirements.txt ./
  RUN pip install -r requirements.txt --no-cache-dir
  CMD ["python"]
  </code></pre>
  
  <p><strong>Now to the questions!</strong></p>
  
  <p>First, obviously I am missing some permission for the container to use my host Docker daemon. I see <a href="https://stackoverflow.com/questions/45447434/how-to-use-docker-from-inside-jenkins-docker-container">this post</a> (among others) has some answers to that.</p>
  
  <p><strong>Secondly, is this even the intended way of setting things up? I would like to learn docker the right way, and I don't know if I have missed something regarding e.g. multi-stage builds?</strong></p>