How to use docker agent in Jenkinsfile pipeline?

Viewed 5163

I have a Django project uploaded in GitHub and I need to link it with jenkins. I installed Jenkins and Docker services on Ubuntu 20.04 machine.

I configured the Jenkins server with my repo and I installed all the suggestd plggins + docker pipeline plugin.

after that, I created a Jenkinsfile that uses docker agent to run the stages inside a python docker container but I'm getting "‘Jenkins’ doesn’t have label ‘docker’" in the console output. I tried to add the label docker in the project settings but still the same error appears!

This is my Jenkinsfile:

pipeline {
   agent any
       stages {


       stage("install pip dependencies") {
      agent { 
        docker {
           label "docker" 
            image "python:3.7"
           }
           }
       steps {
          withEnv(["HOME=${env.WORKSPACE}"]) {
              sh "pip install virtualenv"
              sh "virtualenv venv"
              sh "pip install -r requirements.txt "

         }
       }

     }

}}

What am I missing?

Thank you!

2 Answers

That message means your only available node, which happens to be your Jenkins controller, does not have the label docker that you've required on your agent in this block:

agent {
    docker {
        label 'docker'
        image 'python:3.7'
    }
}

Adding the label docker to the controller, then restarting Jenkins (required for the label change to be recognized, though that surprised me. It might be a peculiarity of labeling the controller itself, since you should avoid scheduling jobs to run there if possible) resolves the issue.

Pre-label:

Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (install pip dependencies)
[Pipeline] node
Still waiting to schedule task
‘Jenkins’ doesn’t have label ‘docker’
Aborted by admin
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: ABORTED

Post-label, pre-restart:

Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (install pip dependencies)
[Pipeline] node
Still waiting to schedule task
‘Jenkins’ doesn’t have label ‘docker’
Aborted by admin
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: ABORTED

Post-restart, highlighting that my controller doesn't have docker installed

Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (install pip dependencies)
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/test@2
[Pipeline] {
[Pipeline] isUnix
[Pipeline] sh
+ docker inspect -f . python:3.7
/var/jenkins_home/workspace/test@2@tmp/durable-4a9f38a7/script.sh: 1: /var/jenkins_home/workspace/test@2@tmp/durable-4a9f38a7/script.sh: docker: not found
[Pipeline] isUnix
[Pipeline] sh
+ docker pull python:3.7
/var/jenkins_home/workspace/test@2@tmp/durable-58d19d02/script.sh: 1: /var/jenkins_home/workspace/test@2@tmp/durable-58d19d02/script.sh: docker: not found
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE

Your pipeline will looks like the following one:

pipeline {
  agent {
    label 'docker'
  }
  stages {
    stage('install pip dependencies') {
      steps {
        withEnv(["HOME=${env.WORKSPACE}"]) {
          sh'''
            pip install virtualenv
            virtualenv venv
            pip install -r requirements.txt
          '''
        }
      }
    }
  }
}

But before you need to follow these steps to make jenkins run docker containers as slaves:

  • install docker on your host;
  • add jenkins to docker group: sudo usermod -aG docker jenkins
  • Modify ExecStart=/usr/bin/dockerd line in the file /lib/systemd/system/docker.service to the following ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock -H fd:// -s overlay2 --containerd=/run/containerd/containerd.sock
  • run sudo systemctl daemon-reload and sudo systemctl restart docker
  • configure docker section in Jenkins: manage Jenkins -> manage nodes and clouds -> configure clouds -> add a new cloud -> docker type tcp://127.0.0.1:2375 (or 4243) or unix:///var/run/docker.sock in Docker URL field. Configure agent, set any label and use it in pipeline.

Probably you will need to turn off selinux.

Related