1

I'm using the official jenkins:lts image to host a Jenkins instance, with the Docker control socket forwarded inside the container to allow Jenkins to start new containers, and now I'm trying to run a pipeline that starts with

pipeline {
    agent {
        docker {
            image 'toolchain:2011'
            args '-v ${WORKSPACE}:${WORKSPACE} -w ${WORKSPACE}'
…

Running this job tries to execute the docker command inside the Jenkins container, which doesn't work because the package isn't installed. The regular Docker plugin works fine, as it uses the control socket directly instead of trying to run external programs.

I suspect I'm not the first person to do this -- how would I set up a Jenkins instance inside a container that can execute a pipeline with a "docker" agent?

3 Answers 3

3

You have to extend the jenkins/jenkins:lts to install the Docker client:

FROM jenkins/jenkins:lts

# Switch to root
USER root

ENV DOCKER_VERSION      docker-18.06.3-ce
ARG DOCKER_GID=993 # put the correct docker gid

# Download and install docker client
RUN wget --quiet -O- \
  https://download.docker.com/linux/static/stable/x86_64/${DOCKER_VERSION}.tgz | \
  tar zx --strip-components=1 -C /usr/local/bin docker/docker \
  && groupadd -g ${DOCKER_GID} docker \
  && usermod -aG docker jenkins

# Switch back to jenkins user
USER jenkins

You can now declare pipelines with docker agents.

0

I used something like that in the simple flask app image:

stage('Deploy') {
        steps {
            sh 'docker build -t flask-sample:latest .'
            sh 'docker run -d --rm --name hello_app_flask -p 4010:4000 flask-sample:latest'
}

But I don't know wether it is approppriate option. Maybe, someone knows the 'proper' way to do it.

1
  • The difficulty is that the jenkins/jenkins:lts and jenkins/jenkins:latest images don't have the docker command line app, so running it from sh steps would also fail. Dec 17, 2019 at 13:34
0

What are the permissions set to Jenkins docker container you are using in the instance?

This is one of the solution. You can use the following docker-compose along with the Dockerfile.

docker-compose.yml

version: "3.7"

services:
  jenkins:
    build:
      context: .
      dockerfile: Dockerfile
    image: jenkins
    container_name: jenkins
    restart: always
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
      - "/etc/sysconfig/docker:/etc/sysconfig/docker"
      - "/home/ubuntu/jenkins/volume:/var/jenkins_home"
    ports:
      - "8443:8080"
    privileged: true

Dockerfile

FROM jenkins/jenkins:lts

#Switching to root user
USER root
RUN apt update && \
    apt install sudo curl jq apt-utils -y

RUN curl -sSL https://get.docker.com/ | sh
RUN  sudo apt-get install docker-compose -y

#install aws
RUN sudo apt-get install -y python-pip python-dev build-essential
RUN pip install awscli --upgrade --user
RUN export PATH=~/.local/bin:$PATH
#updating path to support aws 
ENV PATH="~/.local/bin:${PATH}"

#php installation 
#----------------
RUN apt install ca-certificates apt-transport-https 
RUN wget -q https://packages.sury.org/php/apt.gpg -O- | sudo apt-key add -
RUN echo "deb https://packages.sury.org/php/ stretch main" | sudo tee /etc/apt/sources.list.d/php.list

RUN apt update
RUN apt install -y php7.2
RUN apt-get install -y php7.2-common php7.2-opcache php7.2-curl php7.2-mbstring php7.2-mysql php7.2-zip php7.2-xml

#install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer

#install phpunit
RUN curl -sSL https://phar.phpunit.de/phpunit.phar -o phpunit.phar
RUN chmod +x phpunit.phar
RUN mv phpunit.phar /usr/local/bin/phpunit

Create a volume in the same directory named volume

This will allow you to run the docker command without any issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.