<p>You have to extend the <code>jenkins/jenkins:lts</code> to install the Docker client:</p>

<pre><code>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 \
  &amp;&amp; groupadd -g ${DOCKER_GID} docker \
  &amp;&amp; usermod -aG docker jenkins

# Switch back to jenkins user
USER jenkins
</code></pre>

<p>You can now declare pipelines with docker agents.</p>

CC BY-SA 4.0
2019-12-17T13:40:27.293Z
2019-12-17T13:40:27.293Z
Hedi Nasr
<p>I used something like that in the simple flask app image:</p>

<pre><code>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'
}
</code></pre>

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

CC BY-SA 4.0
2019-12-17T13:34:30.880Z
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.
Simon Richter
CC BY-SA 4.0
2019-12-17T13:25:53.780Z
2019-12-17T13:25:53.780Z
kporwit
<p>What are the permissions set to Jenkins docker container you are using in the instance?</p>

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

<p>docker-compose.yml</p>

<pre><code>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
</code></pre>

<p>Dockerfile</p>

<pre><code>FROM jenkins/jenkins:lts

#Switching to root user
USER root
RUN apt update &amp;&amp; \
    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
</code></pre>

<p>Create a volume in the same directory named <code>volume</code></p>

<p>This will allow you to run the docker command without any issue.</p>

CC BY-SA 4.0
2019-12-18T08:36:13.380Z
2019-12-18T08:36:13.380Z
Manish_
<p>I'm using the official <code>jenkins:lts</code> 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</p>

<pre><code>pipeline {
    agent {
        docker {
            image 'toolchain:2011'
            args '-v ${WORKSPACE}:${WORKSPACE} -w ${WORKSPACE}'
…
</code></pre>

<p>Running this job tries to execute the <code>docker</code> 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.</p>

<p>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?</p>

<docker><jenkins><jenkins-plugins>
Jenkins Pipeline Docker Agents with official Jenkins Docker image
Simon Richter
# Jenkins Pipeline Docker Agents with official Jenkins Docker image

(Asked by: username_0 on 2019-12-17)

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?

**Tags:** docker, jenkins, jenkins-plugins


---

## 3 answers

### Answer by username_1 on 2019-12-17. Score: 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.


---

### Answer by username_2 on 2019-12-17. Score: 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.

> **Comments:**
> **username_0**: 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.  

---

### Answer by username_3 on 2019-12-18. Score: 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.


---

