11. Agent Docker

11.1. Set-up Environment

11.1.1. Install Docker on Jenkins container

$ docker exec -it -u root jenkins sh
$ curl https://get.docker.com |sh

11.1.2. Jenkins and Docker Daemon

  • Cannot run Container inside Container

  • Mount docker.sock from Host to Jenkins container

  • Will spawn sibling containers

$ chmod 777 /var/run/docker.sock
$ docker run -v /var/run/docker.sock:/var/run/docker.sock ...

11.2. Using docker

$ docker pull openjdk:8-jdk
$ docker pull maven:3-jdk-8
$ docker pull golang:1.7
$ docker pull python:3

11.3. Pipeline DSL

11.3.1. Example 1

Listing 50. Docker container declaration
pipeline {
    agent { docker 'python:3.7.5' }

    stages {
        stage('build') {
            steps {
                sh 'python --version'
            }
        }
    }
}

11.3.2. Example 2

Listing 51. Verbose declaration
pipeline {
    agent {
        docker {
            image 'maven:3-alpine'
            label 'my-defined-label'
            args  '-v /home/jenkins/.m2:/home/jenkins/.m2'
        }
    }

    stages {
        stage('Build') {
            steps {
                sh '/bin/echo "Building..."'
            }
        }
    }
}

11.3.3. Example 3

Listing 52. Declaring docker container per build
pipeline {
    agent none

    stages {
        stage('Build') {
            agent { docker 'maven:3-alpine' }
            steps {
                sh 'mvn --version'
            }
        }

        stage('Test') {
            agent { docker 'openjdk:8-jre' }
            steps {
                sh 'java -version'
            }
        }
    }
}

11.4. Assignments

11.4.1. Jenkins Docker Plugin

  1. Skonfiguruj zadanie aby uruchamiało kontener

  2. Zadanie ma provisionować konfigurację wewnątrz kontenera

  3. Zadanie ma uruchamiać build wewnątrz kontenera

  4. Zadanie ma niszczyć kontener po buildze