casparjespersen
casparjespersen

Reputation: 3860

Best-practice of setting up Jenkins-in-Docker with Docker-in-Jenkins

I am developing an application for which I would like continuous integration using Jenkins, furthermore I would like to be able to control the environment for CI. I am developing locally on OS X, and I am pushing to a remote Ubuntu 16.04 VM. All of this lead me to Docker. Basically what I want is Jenkins-in-Docker, but also Docker-in-Jenkins. My build slave(s) will initially be limited to just master.

Now, I've been reading some posts about Docker for the past few days, but I am still not entirely sure I understand it in-depth, yet. Hence this question.

Currently, this is my setup:

Jenkins:

/docker-compose.yml

version: '3'
services:
  jenkins:
    build: ./jenkins
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock # allowing docker-in-docker
      - ./jenkins_home:/var/jenkins_home
    ports:
      - 8080:8080

/jenkins/Dockerfile

FROM docker:17.12 AS docker
FROM jenkins/jenkins:lts

COPY --from=docker /usr/local/bin/docker /usr/local/bin/docker

The above setup works just fine - until I use Docker-in-Jenkins. Jenkins is installed and all, but when I'm building any jobs that would then use Docker, I get an error that

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

CI Source:

/Jenkinsfile

pipeline {
  agent { dockerfile true }
  stages {
    stage('Initial') {
      steps {
        println 'Hello world!'
      }
    }
  }
}

/Dockerfile

FROM python:3.6-slim-jessie
COPY requirements.txt ./
RUN pip install -r requirements.txt --no-cache-dir
CMD ["python"]

Now to the questions!

First, obviously I am missing some permission for the container to use my host Docker daemon. I see this post (among others) has some answers to that.

Secondly, is this even the intended way of setting things up? I would like to learn docker the right way, and I don't know if I have missed something regarding e.g. multi-stage builds?

Upvotes: 4

Views: 3698

Answers (2)

Torkashvand
Torkashvand

Reputation: 416

Use this Dockerfile:

FROM jenkins/jenkins

USER root

RUN apt-get update RUN apt-get -y install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg2 \
    software-properties-common RUN curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; echo "$ID")/gpg | apt-key add - RUN add-apt-repository \    "deb [arch=amd64] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") \   $(lsb_release -cs) \    stable" RUN apt-get update RUN apt-get -y install docker-ce

USER jenkins

Upvotes: 0

Artem Aleksashkin
Artem Aleksashkin

Reputation: 21

Please check this Dockerfile for jenkins with docker.

FROM jenkins/jenkins:lts
MAINTAINER "[email protected]" Artem Aleksashkin

USER root
RUN apt-get update && \
    apt-get -y install apt-transport-https \
         ca-certificates \
         curl \
         gnupg2 \
         software-properties-common && \
    curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; echo "$ID")/gpg > /tmp/dkey; apt-key add /tmp/dkey && \
    add-apt-repository \
       "deb [arch=amd64] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") \
       $(lsb_release -cs) \
       stable" && \
    apt-get update && \
    apt-get -y install docker-ce && \
    rm -rf /var/lib/apt/lists/* && \
    curl -L "https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && \
    chmod +x /usr/local/bin/docker-compose && \
    usermod -aG docker jenkins

USER jenkins

Upvotes: 2

Related Questions