title: Getting logs from a docker container inside jenkins
body: <p>I am trying to get docker to setup a complete test environment that I can run integration tests in, but so far it is not really working and I am even having issues with getting logs back from it.</p>
  
  <p>So I want to run it as a pipeline and I want to use jenkinsfile. This is what I got so far:</p>
  
  <pre><code>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 -&gt; 
                          docker.image('munhunger/highly-oven').withRun('-e "test=test"') { h -&gt; 
                              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'
                              }
                              docker.image('gradle:latest').inside("--link ${h.id}:backend -e 'OVEN_URL=http://backend:8080'") {
                                  sh 'gradle test -b oven/build.gradle'
                              }
                              sh "docker logs ${h.id}"
                          }
                      }
                  }
              }
          }
          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")
                          }
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>But there seems to be a problem with the connection between my backend and the database...</p>
  
  <p>All I get from the build logs is this:</p>
  
  <pre><code>se.munhunger.oven.rest.UserTest &gt; System is up and running, Creating a user, it returns 204 upon creation FAILED
      java.lang.AssertionError at UserTest.java:38
  </code></pre>
  
  <p>which points to:</p>
  
  <pre><code>                Assert.assertEquals("non 204 from backend", 204,
                                      client.target(baseURL + "/api/user")
                                          .request()
                                          .header("email", "mail@mail.mail")
                                          .post(Entity.json(null))
                                          .getStatus());
  </code></pre>
  
  <p>I believe that the connection between the tester and the backend is working because the following test succeeds</p>
  
  <pre><code>            Assert.assertEquals(200,
                                  client.target(baseURL + "/swagger")
                                      .request()
                                      .get()
                                      .getStatus());
  </code></pre>
  
  <p>Which leads me to I guess the main question of how do I get the logs from my backend docker image?
  I feel like without it, it is pretty much impossible to debug what is going wrong</p>
  
  <p><strong>Edit</strong> 
  I've gotten some logs out, but at the wrong time.
  If I change to the following:</p>
  
  <pre><code>                docker.image('mysql:latest').withRun('-e "MYSQL_ROOT_PASSWORD=password" -e "MYSQL_USER=root" -e "MYSQL_DATABASE=highlygroceries"') { c -&gt; 
                      docker.image('munhunger/highly-oven').withRun('-e "test=test"') { h -&gt; 
                          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'
                          }
                          sh "docker logs ${h.id}"
                          docker.image('gradle:latest').inside("--link ${h.id}:backend -e 'OVEN_URL=http://backend:8080'") {
                              sh 'gradle test -b oven/build.gradle'
                          }
                          sh "docker logs ${h.id}"
                      }
                  }
  </code></pre>
  
  <p>I get all the logs from startup. but it is not printing out the logs from after the test failure</p>