简体   繁体   中英

How to build a docker image for jenkins with android and docker build support

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.

From my github repo ( https://github.com/mikedolx/docker-jenkins-android ) this is how my docker file looks like:

FROM xmartlabs/android AS android

USER root
RUN apt-get update && \
        apt-get install -y apt-transport-https curl software-properties-common && \
        curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - && \
        add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" && \
        apt-get update && \
        apt-cache policy docker-ce && \
        apt-get clean && \
        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

I have added the needed steps to install docker. I took them from this blog: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-16-04 .

I can successfully build the image, and run jenkins server with the following docker-compose.yml

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:

I have a pipeline project setup to build this image ( https://github.com/mikedolx/docker-nextcloud ). When i start the build it stops on the second stage, with the following log:

[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

When i ssh into the jenkins container and try to run "docker", i get the same error.

Questions:

  1. how do i build the jenkins docker image to contain the needed binaries to build a docker image?
  2. Is this the correct approach to buid a docker image via jenkins?

Thanks in advance,

Regards,

Michael

You need to run docker in docker.

So, in a nutshell, you mount the host docker socket as a volume into Jenkins and have compatible docker binaries in your container.

This is a good description

There is much more to it to consider, such as security depending on other containers running on your host and also how to run it when using jenkins agents.

After i changed the order of installation in my Dockerfile (moved everything after "FROM jenkins/jenkins") i finally had a docker binary in the console. Now when i run my build in jenkins, i get the following error

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock

the reason for this is, because i had mapped my host's docker.sock as a volume into my container. But it seems that somehow the permissions are wrong. Need to check this.

EDIT: After i changed the ownership of the hosts /var/run/docker.sock to jenkins:jenkins i was able to perform the required docker command line actions in my jenkins project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM