简体   繁体   English

如何使用Android和Docker构建支持为Jenkins构建Docker映像

[英]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. 我目前正在尝试构建自己的Jenkins Docker映像,目的是拥有一个Jenkins服务器,该服务器可以构建基于Android gradle的项目和docker映像。

From my github repo ( https://github.com/mikedolx/docker-jenkins-android ) this is how my docker file looks like: 在我的github存储库( https://github.com/mikedolx/docker-jenkins-android )中,这是我的docker文件的样子:

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. 我已经添加了安装docker所需的步骤。 I took them from this blog: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-16-04 . 我从以下博客中获取了它们: 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 我可以成功构建映像,并使用以下docker-compose.yml运行jenkins服务器

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 ). 我有一个管道项目设置来构建此映像( 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. 当我ssh进入jenkins容器并尝试运行“ docker”时,出现相同的错误。

Questions: 问题:

  1. how do i build the jenkins docker image to contain the needed binaries to build a docker image? 如何构建jenkins docker映像以包含构建docker映像所需的二进制文件?
  2. Is this the correct approach to buid a docker image via jenkins? 这是通过jenkins构建docker映像的正确方法吗?

Thanks in advance, 提前致谢,

Regards, 问候,

Michael 迈克尔

You need to run docker in docker. 您需要在docker中运行docker。

So, in a nutshell, you mount the host docker socket as a volume into Jenkins and have compatible docker binaries in your container. 因此,简而言之,您可以将主机Docker套接字作为卷安装到Jenkins中,并且容器中具有兼容的Docker二进制文件。

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. 还有更多需要考虑的事情,例如安全性取决于主机上运行的其他容器,以及使用jenkins代理时如何运行它。

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. 在我更改了Dockerfile中的安装顺序(移动了“ FROM jenkins / jenkins”之后的所有内容)之后,我终于在控制台中有了一个Docker二进制文件。 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. 这样做的原因是,因为我已将主机的docker.sock作为卷映射到我的容器中。 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. 编辑:将主机/var/run/docker.sock的所有权更改为jenkins:jenkins之后,我能够在jenkins项目中执行所需的docker命令行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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