Reputation: 153
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:
Thanks in advance,
Regards,
Michael
Upvotes: 2
Views: 2075
Reputation: 153
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.
Upvotes: 1
Reputation: 9075
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.
Upvotes: 0