Getting logs from a docker container inside jenkins Getting logs from a docker container inside jenkins docker docker

Getting logs from a docker container inside jenkins


It helps when you are able to run the integration tests without using your Jenkins file, so instead of using these nested docker.image statements, you should use docker-compose.

I to integration testing like this:

stage('Run integration tests') {  steps {    script {      try {        timeout(30) {          // Tear up integration test environment          sh "docker-compose up -d"          // Wait until it is ready          waitUntil {            "healthy" == sh(returnStdout: true,              script: "docker inspect CONTAINER_NAME --format=\"{{ .State.Health.Status }}\"").trim()          }          docker.image('IMAGENAME').inside('--network projectname_default') {            sh "gradle integrationTest"          }        }      } finally {        try {          step([$class: 'JUnitResultArchiver', testResults: '**/build/integrationTest-results/TEST-*.xml'])        } catch (Exception e) {          // Ignore exception when there are no test results        }        sh "docker-compose logs >integration-test.log"        sh "docker-compose down --rmi local --volumes --remove-orphans"        archive 'integration-test.log'      }    }  }}

As you can see, I attach the Gradle testing container to the network that is set up by docker-compose. This allows you re-using your compose not only for testing.

Now you have to make sure that all containers that you use log to stdout. At the end, you get all your logs in archive integration-test.log. Of course, you can also extend this to get a separate log file for each container.


Turns out you can straight up catch that shit:

pipeline {    agent any    stages {        stage('build war') {            agent {                docker {                     image 'gradle:latest'                    reuseNode true                 }            }            steps {                sh 'gradle war -b oven/build.gradle'            }        }        stage('test') {            steps {                script {                    docker.image('mysql:latest').withRun('-e "MYSQL_ROOT_PASSWORD=password" -e "MYSQL_USER=root" -e "MYSQL_DATABASE=highlygroceries"') { c ->                         docker.image('munhunger/highly-oven').withRun('-e "test=test"') { h ->                             docker.image('mysql:latest').inside("--link ${c.id}:db") {                                sh 'while ! mysqladmin ping -hdb --silent; do sleep 1; done'                            }                            docker.image('munhunger/highly-oven').inside("--link ${c.id}:db -e 'DB_URL=db:3306' -e 'DB_PASS=password' -e 'DB_USER=root'") {                                sh 'sleep 5'                            }                            try {                                docker.image('gradle:latest').inside("--link ${h.id}:backend -e 'OVEN_URL=http://backend:8080'") {                                        sh 'gradle test -b oven/build.gradle'                                }                            }                            catch (exc) {                                sh "docker logs ${h.id}"                                throw exc                            }                        }                    }                }            }        }        stage('build dockerimage') {            steps {                script {                    dir('oven') {                        def image = docker.build("munhunger/highly-oven")                        docker.withRegistry('https://registry.hub.docker.com', 'docker-hub-credentials') {                            image.push("${env.BUILD_NUMBER}")                            image.push("latest")                        }                    }                }            }        }    }}