/ Tools

Jenkins Docker in Docker

Yesterday I was trying to deploy a Jenkins on a Linux machine. From my past experience, the official Jenkins Docker image works very well, I can build all of my projects without doing any messy configuration.

However, the latest Jenkins Docker image seems to be a simplified version, as in most of the plugins and pre-configured things are gone...

Problem

With pipeline script

stage('Build'){
            agent{
                docker {image 'node'}
            }
            steps{
                sh 'npm install'
                sh 'npm run build'
            }
        }

Jenkins complains that docker is not found. It turns out that jenkins:lts image does not support docker in docker by default.

Investigation

After some research, the old way of making docker in docker happen is to simply bind mount host's docker CLI binary and docker's communication socket in to Jenkins container, so that the container can use host's docker to build.

However one blog post pops up quite often during my research, saying that docker has changed in a way that binding only the CLI binary to the container is not enough(even dangerous). So as of Jun/2019 the recommended way is to install docker CLI(CLI only, no daemon) inside the container and bind mount the communication socket, so that the program(in this case, Jenkins) in container is able to use its own docker CLI binary to call the host's daemon.

Code/Config is shown in the next section.

Solution

I've posted my Dockerfile and docker run command on Github gist.

Link to the gist

For easy reading, I just copy it here.

Dockerfile

The formatting here looks messed up, refer the link above...

FROM jenkins/jenkins:lts
# if we want to install via apt
USER root
ENV DOCKERVERSION=18.03.1-ce
RUN curl -fsSLO https://download.docker.com/linux/static/stable/x86_64/docker-${DOCKERVERSION}.tgz \
  && tar xzvf docker-${DOCKERVERSION}.tgz --strip 1 \
                 -C /usr/local/bin docker/docker \
  && rm docker-${DOCKERVERSION}.tgz
RUN groupadd docker
RUN usermod -aG docker jenkins
RUN chmod -v a+s $(which docker)
# drop back to the regular jenkins user - good practice
USER jenkins

docker run command

docker run -d \
-v jenkins_home:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
-p 8089:8080 -p 50000:50000 \
-e JENKINS_OPTS="--prefix=/devops" \
--name my-jenkins \
my-jenkins
mimimi

mimimi

Currently a Computer Engineering student at the National University of Singapore, passionate about software engineering. Contact me: info at mimimi.fun

Read More
Jenkins Docker in Docker
Share this