title: How to build a docker image for jenkins with android and docker build support
body: <p>i'm currently trying to build my own Jenkins docker image with the purpose to have a Jenkins server, that can build Android gradle based projects and docker images.</p>
  
  <p>From my github repo (<a href="https://github.com/mikedolx/docker-jenkins-android" rel="nofollow noreferrer">https://github.com/mikedolx/docker-jenkins-android</a>) this is how my docker file looks like:</p>
  
  <pre><code>FROM xmartlabs/android AS android
  
  USER root
  RUN apt-get update &amp;&amp; \
          apt-get install -y apt-transport-https curl software-properties-common &amp;&amp; \
          curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - &amp;&amp; \
          add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" &amp;&amp; \
          apt-get update &amp;&amp; \
          apt-cache policy docker-ce &amp;&amp; \
          apt-get clean &amp;&amp; \
          apt-get install -y docker-ce
  
  FROM jenkins/jenkins
  
  ENV ANDROID_HOME /opt/android-sdk-linux
  COPY --from=android ${ANDROID_HOME} ${ANDROID_HOME}
  COPY --from=android /usr/lib/jvm/java-8-oracle /usr/lib/jvm/java-8-oracle
  COPY --from=android /usr/bin/gradle /usr/bin/gradle
  ENV JAVA_HOME /usr/lib/jvm/java-8-oracle
  ENV PATH ${PATH}:${ANDROID_HOME}/tools:${ANDROID_HOME}/platform-tools
  
  # Unfortunately, "chown" flag seems not to be available for COPY in DockerHub.
  USER root
  RUN chown -R jenkins:jenkins ${ANDROID_HOME}
  USER jenkins
  
  ENV ANDROID_EMULATOR_FORCE_32BIT true
  </code></pre>
  
  <p>I have added the needed steps to install docker. I took them from this blog: <a href="https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-16-04" rel="nofollow noreferrer">https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-16-04</a>. </p>
  
  <p>I can successfully build the image, and run jenkins server with the following docker-compose.yml</p>
  
  <pre><code>version: '2.2'
  
  services:
  
    jenkins:
      image: mikedolx/jenkins-android:latest
      container_name: jenkins
      user: jenkins
      volumes:
        - jenkins-data:/var/jenkins_home
      ports:
        - 8080:8080
        - 50000:50000
  volumes:
    jenkins-data:
  </code></pre>
  
  <p>I have a pipeline project setup to build this image (<a href="https://github.com/mikedolx/docker-nextcloud" rel="nofollow noreferrer">https://github.com/mikedolx/docker-nextcloud</a>). When i start the build it stops on the second stage, with the following log:</p>
  
  <pre><code>[Nextcloud-Github] Running shell script
  + docker build -t mikedolx/nextcloud:14.0.1 --file Dockerfile.14.0 .
  /var/jenkins_home/workspace/Nextcloud-Github@tmp/durable-f5e443ce/script.sh: 2: /var/jenkins_home/workspace/Nextcloud-Github@tmp/durable-f5e443ce/script.sh: docker: not found
  </code></pre>
  
  <p>When i ssh into the jenkins container and try to run "docker", i get the same error.</p>
  
  <p>Questions:</p>
  
  <ol>
  <li>how do i build the jenkins docker image to contain the needed binaries to build a docker image?</li>
  <li>Is this the correct approach to buid a docker image via jenkins?</li>
  </ol>
  
  <p>Thanks in advance,</p>
  
  <p>Regards,</p>
  
  <p>Michael </p>