2

I am running jenkins master and slave as docker container. I have setup a slave node using jenkins/ssh-slave image with label 'worker'. I can successfully run my pipeline on the worker node. However, when I am trying to run docker build command using the Jenkinsfile, I am getting error docker: not found.

pipeline {
agent { label 'worker' }
tools {nodejs "node"}
stages {
    stage ('Build APP') {
        steps {
            echo 'BUILDING APPLICATION'
            sh 'npm install'
        }
    }

    stage ('Create Package') {
        steps {
            script{
                echo 'BUILDING DOCKER IMAGE'
                docker.build("package${env.BUILD_NUMBER}")
            }
        }
    }

    stage('Package Test') {
        agent { docker }
        steps {
            echo 'RUNNING IMAGE IN CONATAINER'
            sh "docker run -p 5050:4000 -d package${env.BUILD_NUMBER}"
            echo 'CHECKING HEALTH STATUS'
            script {
                try {
                    sh "curl -s --head --request GET http://127.0.0.1:5050/ | grep '200'"
                    echo 'Health Check Passed!'
                } catch(Exception e) {
                    echo "Health Check Failed!"
                }
            }
        }
    }

In the third step 'package test' I have placed agent docker in the file but it doesn't seem to work. How can I place agent docker in a declarative pipeline?

Rot-man
  • 18,045
  • 12
  • 118
  • 124
bot
  • 1,293
  • 3
  • 17
  • 34
  • 2
    You use a docker container as a Jenkins slave, and you want to execute docker command inside the slave, thus you should check the container also has docker installed inside and expand to $PATH environment. – yong Feb 14 '19 at 02:22
  • I installed docker inside my ssh-slave container but docker run command is giving me error `Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?` . When I tried to fix this with `docker run -v /var/run/docker.sock:/var/run/docker.sock ...` it is giving me the same error. If I try to add a user `usermod -aG docker $USER`, it gives me _invalid reference_ error. Do you know what is the right way to install docker inside ssh-slave containers? – bot Feb 14 '19 at 15:51
  • 3
    Did you run the ssh-slave container with bind mount `-v /var/run/docker.sock:/var/run/docker.sock`? A container, with Docker installed, does not run its own Docker daemon, but both connects to the Docker daemon of the host system – yong Feb 15 '19 at 00:24
  • It worked. Being new to docker, I was trying to map the socket from inside the container but your comment made me realize that it is to be done from the host machine while running the image in the first step. Thanks! – bot Feb 15 '19 at 16:01

0 Answers0