Jenkins unable to use docker agent in pipeline

Hello !

I am running Jenkins in Docker container but getting the below error while running a pipeline job. The below pipeline snippet checks if maven is being used or not, it throws below error message.

Installed Plugins are,

  1. Docker plugin
  2. Docker pipeline

Additionally, I also configured docker on Jenkins UI, under tools section. Can someone suggest over this ?

Pipeline snippet:

pipeline {
agent {
docker {
image ‘maven:latest’
}
}
stages {
stage (‘Check’) {
steps {
sh ‘mvn -v’
}
}
}
}

Seems docker is not in the path of the user running jenkins. the user also needs to be in the docker group so it can successfully run docker commands

2 Likes

And when the user is in the Docker group and yet this error occurs?

I know I’m replying very late. But if it helps someone then make use of this solution.

As mentioned in the official documentation, if you want to use Docker inside Jenkins, where Jenkins itself is being run as Docker container, then download Docker:dind image and run both Jenkins container and Docker:dind container on the same network.

  1. Create network
    docker network create jenkins

  2. Run docker:dind image’s container on the same network.

docker run \
  --name jenkins-docker \
  --rm \
  --detach \
  --privileged \
  --network jenkins \
  --network-alias docker \
  --env DOCKER_TLS_CERTDIR=/certs \
  --volume jenkins-docker-certs:/certs/client \
  --volume jenkins-data:/var/jenkins_home \
  --publish 2376:2376 \
  docker:dind \
  --storage-driver overlay2
  1. Also run Jenkins container on the same network.
    Create Dockerfile.
FROM jenkins/jenkins:2.452.2-jdk17
USER root
RUN apt-get update && apt-get install -y lsb-release
RUN curl -fsSLo /usr/share/keyrings/docker-archive-keyring.asc \
  https://download.docker.com/linux/debian/gpg
RUN echo "deb [arch=$(dpkg --print-architecture) \
  signed-by=/usr/share/keyrings/docker-archive-keyring.asc] \
  https://download.docker.com/linux/debian \
  $(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list
RUN apt-get update && apt-get install -y docker-ce-cli
USER jenkins
RUN jenkins-plugin-cli --plugins "blueocean docker-workflow"
  1. Build customized Jenkins image.
docker build -t myjenkins-blueocean:2.452.2-1 .
  1. Run Jenkins container.
docker run \
  --name jenkins-blueocean \
  --restart=on-failure \
  --detach \
  --network jenkins \
  --env DOCKER_HOST=tcp://docker:2376 \
  --env DOCKER_CERT_PATH=/certs/client \
  --env DOCKER_TLS_VERIFY=1 \
  --publish 8080:8080 \
  --publish 50000:50000 \
  --volume jenkins-data:/var/jenkins_home \
  --volume jenkins-docker-certs:/certs/client:ro \
  myjenkins-blueocean:2.452.2-1

Thank you.

I know I’m replying very late. But if it helps someone then make use of this solution.

As mentioned in the official documentation, if you want to use Docker inside Jenkins, where Jenkins itself is being run as Docker container, then download Docker:dind image and run both Jenkins container and Docker:dind container on the same network.

For easing other users, instead of going to documentation and search for it, I’ve provided required commands here.

  1. Create network
    docker network create jenkins

  2. Run docker:dind image’s container on the same network.

docker run \
  --name jenkins-docker \
  --rm \
  --detach \
  --privileged \
  --network jenkins \
  --network-alias docker \
  --env DOCKER_TLS_CERTDIR=/certs \
  --volume jenkins-docker-certs:/certs/client \
  --volume jenkins-data:/var/jenkins_home \
  --publish 2376:2376 \
  docker:dind \
  --storage-driver overlay2
  1. Also run Jenkins container on the same network.
    Create Dockerfile.
FROM jenkins/jenkins:2.452.2-jdk17
USER root
RUN apt-get update && apt-get install -y lsb-release
RUN curl -fsSLo /usr/share/keyrings/docker-archive-keyring.asc \
  https://download.docker.com/linux/debian/gpg
RUN echo "deb [arch=$(dpkg --print-architecture) \
  signed-by=/usr/share/keyrings/docker-archive-keyring.asc] \
  https://download.docker.com/linux/debian \
  $(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list
RUN apt-get update && apt-get install -y docker-ce-cli
USER jenkins
RUN jenkins-plugin-cli --plugins "blueocean docker-workflow"
  1. Build customized Jenkins image.
docker build -t myjenkins-blueocean:2.452.2-1 .
  1. Run Jenkins container.
docker run \
  --name jenkins-blueocean \
  --restart=on-failure \
  --detach \
  --network jenkins \
  --env DOCKER_HOST=tcp://docker:2376 \
  --env DOCKER_CERT_PATH=/certs/client \
  --env DOCKER_TLS_VERIFY=1 \
  --publish 8080:8080 \
  --publish 50000:50000 \
  --volume jenkins-data:/var/jenkins_home \
  --volume jenkins-docker-certs:/certs/client:ro \
  myjenkins-blueocean:2.452.2-1

Thank you.