Run docker commands in pipeline build by docker agent with jenkins master running as container

202 views Asked by At

I have a jenkins master running as a container in my local Windows 11 machine with docker desktop installed. My goal is to run docker commands in pipelines that are executed in cloud agents. The error I am facing is:

Post "http://172.23.0.2:2375/v1.24/build?buildargs=%7B%7D&cachefrom=%5B%5D&cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&labels=%7B%7D&memory=0&memswap=0&networkmode=default&rm=1&shmsize=0&t=test-image&target=&ulimits=null&version=1": dial tcp 172.23.0.2:2375: i/o timeout

I setup my environment following these steps. Firstly I build my jenkins master image using the following dockerfile:

FROM jenkins/jenkins:2.414.3-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"

Then I created a docker network:

docker network create jenkins

After I run my jenkins master 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 `
  --volume jenkins-data:/var/jenkins_home `
  --volume jenkins-docker-certs:/certs/client:ro `
  --publish 8080:8080 --publish 50000:50000 myjenkins-blueocean

Then I logged in my jenkins installing the suggested plugins and the docker plugin. After I run an alpine socat container:

docker run -d --restart=always -p 127.0.0.1:2376:2375 --network jenkins `
-v /var/run/docker.sock:/var/run/docker.sock alpine/socat tcp-listen:2375,fork,reuseaddr unix-connect:/var/run/docker.sock

After I inspected this container to get the IP address to use as Docker Host URI in the 'Cloud docker Configuration' section of jenkins. In this section I checked the 'Enabled' and the 'Expose DOCKER_HOST' options. enter image description here

Then I added a new docker agent template using 'jenkins/jnlp-agent-docker' as docker image.

enter image description here

Then I configured my jenkins pipeline which connects to my github repository where I pushed the following Jenkinsfile.

pipeline {
  agent {
    node {
      label 'docker-agent'
    }
  }
  stages {
    stage('Build') {
      steps {
        script {
          echo "Bulding docker images"
          dockerImage = docker.build("test-image", "./WebApiTest")
        }
      }
    }    
  }
}

Now, if I try to build my pipeline, it is able to get the Jenkinsfile from my repo, but the building of the docker image fails with the error that I posted in the beginning of this question.

0

There are 0 answers