├── .gitignore ├── README.md ├── docker-compose.yml ├── LICENSE ├── entrypoint.sh └── Dockerfile /.gitignore: -------------------------------------------------------------------------------- 1 | ssh-creds 2 | certs 3 | https-run.sh 4 | ssh-creds 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jenkins-docker 2 | 3 | Simple example of how to install Docker inside of Jenkins, mount the socket, 4 | and use the entrypoint to give jenkins access to that socket with the docker 5 | gid permissions. 6 | 7 | Source Repo: https://github.com/sudo-bmitch/jenkins-docker 8 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.2' 2 | 3 | volumes: 4 | jenkins-home: 5 | 6 | services: 7 | jenkins: 8 | image: localhost:5000/jenkins-docker 9 | build: . 10 | restart: unless-stopped 11 | ports: 12 | - target: 8080 13 | published: 8080 14 | protocol: tcp 15 | mode: host 16 | volumes: 17 | - jenkins-home:/var/jenkins_home 18 | - /var/run/docker.sock:/var/run/docker.sock 19 | container_name: jenkins-docker 20 | 21 | 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2018 Brandon Mitchell 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # By: Brandon Mitchell 4 | # License: MIT 5 | # Source Repo: https://github.com/sudo-bmitch/jenkins-docker 6 | 7 | set -x 8 | 9 | # configure script to call original entrypoint 10 | set -- tini -- /usr/local/bin/jenkins.sh "$@" 11 | 12 | # In Prod, this may be configured with a GID already matching the container 13 | # allowing the container to be run directly as Jenkins. In Dev, or on unknown 14 | # environments, run the container as root to automatically correct docker 15 | # group in container to match the docker.sock GID mounted from the host. 16 | if [ "$(id -u)" = "0" ]; then 17 | # get gid of docker socket file 18 | SOCK_DOCKER_GID=`ls -ng /var/run/docker.sock | cut -f3 -d' '` 19 | 20 | # get group of docker inside container 21 | CUR_DOCKER_GID=`getent group docker | cut -f3 -d: || true` 22 | 23 | # if they don't match, adjust 24 | if [ ! -z "$SOCK_DOCKER_GID" -a "$SOCK_DOCKER_GID" != "$CUR_DOCKER_GID" ]; then 25 | groupmod -g ${SOCK_DOCKER_GID} -o docker 26 | fi 27 | if ! groups jenkins | grep -q docker; then 28 | usermod -aG docker jenkins 29 | fi 30 | # Add call to gosu to drop from root user to jenkins user 31 | # when running original entrypoint 32 | set -- gosu jenkins "$@" 33 | fi 34 | 35 | # replace the current pid 1 with original entrypoint 36 | exec "$@" 37 | 38 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | ARG JENKINS_VER=lts 2 | ARG JENKINS_REGISTRY=jenkins/jenkins 3 | FROM ${JENKINS_REGISTRY}:${JENKINS_VER} 4 | 5 | # switch to root, let the entrypoint drop back to jenkins 6 | USER root 7 | 8 | # install prerequisite debian packages 9 | RUN apt-get update \ 10 | && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ 11 | apt-transport-https \ 12 | ca-certificates \ 13 | curl \ 14 | gnupg2 \ 15 | software-properties-common \ 16 | vim \ 17 | wget \ 18 | && apt-get clean \ 19 | && rm -rf /var/lib/apt/lists/* 20 | 21 | # install gosu for a better su+exec command 22 | ARG GOSU_VERSION=1.10 23 | RUN dpkgArch="$(dpkg --print-architecture | awk -F- '{ print $NF }')" \ 24 | && wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch" \ 25 | && chmod +x /usr/local/bin/gosu \ 26 | && gosu nobody true 27 | 28 | # install docker 29 | ARG DOCKER_CLI_VERSION==5:19.03.0~3-0~debian-stretch 30 | # ARG DOCKER_CLI_VERSION= 31 | RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add - \ 32 | && add-apt-repository \ 33 | "deb [arch=amd64] https://download.docker.com/linux/debian \ 34 | $(lsb_release -cs) \ 35 | stable" \ 36 | && apt-get update \ 37 | && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ 38 | docker-ce-cli${DOCKER_CLI_VERSION} \ 39 | && apt-get clean \ 40 | && rm -rf /var/lib/apt/lists/* \ 41 | && groupadd -r docker \ 42 | && usermod -aG docker jenkins 43 | 44 | # entrypoint is used to update docker gid and revert back to jenkins user 45 | COPY entrypoint.sh /entrypoint.sh 46 | RUN chmod +x /entrypoint.sh 47 | ENTRYPOINT ["/entrypoint.sh"] 48 | HEALTHCHECK CMD curl -sSLf http://localhost:8080/login >/dev/null || exit 1 49 | 50 | ARG BUILD_DATE 51 | ARG VCS_REF 52 | ARG IMAGE_PATCH_VER=0 53 | LABEL \ 54 | org.label-schema.build-date=$BUILD_DATE \ 55 | org.label-schema.docker.cmd="docker run -d -p 8080:8080 -v \"$$(pwd)/jenkins-home:/var/jenkins_home\" -v /var/run/docker.sock:/var/run/docker.sock sudobmitch/jenkins-docker" \ 56 | org.label-schema.description="Jenkins with docker support, Jenkins ${JENKINS_VER}, Docker ${DOCKER_VER}" \ 57 | org.label-schema.name="bmitch3020/jenkins-docker" \ 58 | org.label-schema.schema-version="1.0" \ 59 | org.label-schema.url="https://github.com/sudo-bmitch/jenkins-docker" \ 60 | org.label-schema.vcs-ref=$VCS_REF \ 61 | org.label-schema.vcs-url="https://github.com/sudo-bmitch/jenkins-docker" \ 62 | org.label-schema.vendor="Brandon Mitchell" \ 63 | org.label-schema.version="${JENKINS_VER}-${IMAGE_PATCH_VER}" 64 | 65 | --------------------------------------------------------------------------------