<p>You need to run that docker in interactive mode.</p>
<pre><code>docker run --rm -it -e STR=foo tmp
</code></pre>
<ul>
<li><code>-i</code> - interactive mode</li>
<li><code>--rm</code> - to delete the container once finished the process (<code>tmp</code>)</li>
</ul>

CC BY-SA 4.0
2023-05-24T07:03:20.133Z
2023-05-24T07:03:20.133Z
Abid Khan
<p>You can get the Docker stdout into a variable as follows and then log it at will.</p>
<pre><code>script {
  myVar = sh(script: 'docker run -e STR=foo -t tmp', returnStdout: true).trim() 
  println &quot;got ${myVar}&quot;
}
</code></pre>

CC BY-SA 4.0
2023-06-27T05:11:55.803Z
2023-06-27T05:11:55.803Z
Noumenon
<p>(Moved this question from Stack Overflow after getting no traction there; perhaps I'll have more luck here by getting consideration by more DevOps-specific experts)</p>
<p>As part of my Jenkins pipeline, I run a Docker container with Jenkins' <code>docker.withRun()</code>.<br />
For the purpose of demonstration, the Docker container entrypoint is a trivial bash script that echoes an environment variable:</p>
<pre><code>#! /bin/bash
# runme

if [ -z &quot;$STR&quot; ]
then
  echo 'STR is undefined'
else
  echo &quot;STR is $STR&quot;
fi
</code></pre>
<pre><code># Dockerfile

FROM debian:10.3 as base

COPY ./runme /usr/local/bin

ENTRYPOINT runme
</code></pre>
<p>Building and running locally:</p>
<pre><code>$ docker build --no-cache -t tmp .
[+] Building 0.3s (7/7) FINISHED
 =&gt; [internal] load build definition from Dockerfile                                          0.0s
 =&gt; =&gt; transferring dockerfile: 109B                                                          0.0s
 =&gt; [internal] load .dockerignore                                                             0.0s
 =&gt; =&gt; transferring context: 2B                                                               0.0s
 =&gt; [internal] load metadata for docker.io/library/debian:10.3                                0.0s
 =&gt; CACHED [1/2] FROM docker.io/library/debian:10.3                                           0.0s
 =&gt; [internal] load build context                                                             0.0s
 =&gt; =&gt; transferring context: 26B                                                              0.0s
 =&gt; [2/2] COPY ./runme /usr/local/bin                                                         0.1s
 =&gt; exporting to image                                                                        0.0s
 =&gt; =&gt; exporting layers                                                                       0.0s
 =&gt; =&gt; writing image sha256:f5b6fdb3035c12a061c1221f570f126f5a7479e7e3ab07bba0a9694b5b04ab52  0.0s
 =&gt; =&gt; naming to docker.io/library/tmp                                                        0.0s
$ docker run -t tmp
STR is undefined
$ docker run -e STR=foo -t tmp
STR is foo
$
</code></pre>
<p>I try to run this same Docker container from a Jenkins pipeline:</p>
<pre><code>pipeline {
  agent any

  options { timestamps{} }

  stages {
    stage('docker run') {
      steps {
        script {
          def img = 'tmp'
          def docker_run_args = '-t -e STR=foo'
          def ms = docker.image(img)

          ms.withRun(docker_run_args) {}
        }
      }
    }
  }
}
</code></pre>
<p>...I can see from the Jenkins job's console logs that the container is running, but its stdout is not included in the console logs:</p>
<pre><code>[Pipeline] stage
[Pipeline] { (docker run)
[Pipeline] script
[Pipeline] {
[Pipeline] isUnix
[Pipeline] sh
09:49:40  + docker run -d -t -e STAGE=foo tmp
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
09:49:45  + docker stop e5020f0e5416f34221d3cbc275fa44ccc05f9af03b138e4ec67d47b1d90a2d3a
09:49:45  e5020f0e5416f34221d3cbc275fa44ccc05f9af03b138e4ec67d47b1d90a2d3a
09:49:45  + docker rm -f e5020f0e5416f34221d3cbc275fa44ccc05f9af03b138e4ec67d47b1d90a2d3a
09:49:45  e5020f0e5416f34221d3cbc275fa44ccc05f9af03b138e4ec67d47b1d90a2d3a
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
</code></pre>
<p>As you can see from the above, the string &quot;<code>STR is ...</code>&quot; is not included in the Jenkins job's console logs, but appears in my terminal's stdout just fine when I run the container myself.<br />
I tried using <code>docker.run()</code> instead of <code>docker.withRun()</code>, but it didn't change the behavior.</p>
<p><strong>Is there a way I can get the Docker container's stdout included in the Jenkins job's console logs?</strong></p>

<docker><jenkins><jenkins-plugins>
How to get Docker container stdout in Jenkins logs?
StoneThrow
# How to get Docker container stdout in Jenkins logs?

(Asked by: username_0 on 2023-05-18)

(Moved this question from Stack Overflow after getting no traction there; perhaps I'll have more luck here by getting consideration by more DevOps-specific experts)
As part of my Jenkins pipeline, I run a Docker container with Jenkins' docker.withRun().
For the purpose of demonstration, the Docker container entrypoint is a trivial bash script that echoes an environment variable:
#! /bin/bash
# runme

if [ -z "$STR" ]
then
  echo 'STR is undefined'
else
  echo "STR is $STR"
fi

# Dockerfile

FROM debian:10.3 as base

COPY ./runme /usr/local/bin

ENTRYPOINT runme

Building and running locally:
$ docker build --no-cache -t tmp .
[+] Building 0.3s (7/7) FINISHED
 => [internal] load build definition from Dockerfile                                          0.0s
 => => transferring dockerfile: 109B                                                          0.0s
 => [internal] load .dockerignore                                                             0.0s
 => => transferring context: 2B                                                               0.0s
 => [internal] load metadata for docker.io/library/debian:10.3                                0.0s
 => CACHED [1/2] FROM docker.io/library/debian:10.3                                           0.0s
 => [internal] load build context                                                             0.0s
 => => transferring context: 26B                                                              0.0s
 => [2/2] COPY ./runme /usr/local/bin                                                         0.1s
 => exporting to image                                                                        0.0s
 => => exporting layers                                                                       0.0s
 => => writing image sha256:f5b6fdb3035c12a061c1221f570f126f5a7479e7e3ab07bba0a9694b5b04ab52  0.0s
 => => naming to docker.io/library/tmp                                                        0.0s
$ docker run -t tmp
STR is undefined
$ docker run -e STR=foo -t tmp
STR is foo
$

I try to run this same Docker container from a Jenkins pipeline:
pipeline {
  agent any

  options { timestamps{} }

  stages {
    stage('docker run') {
      steps {
        script {
          def img = 'tmp'
          def docker_run_args = '-t -e STR=foo'
          def ms = docker.image(img)

          ms.withRun(docker_run_args) {}
        }
      }
    }
  }
}

...I can see from the Jenkins job's console logs that the container is running, but its stdout is not included in the console logs:
[Pipeline] stage
[Pipeline] { (docker run)
[Pipeline] script
[Pipeline] {
[Pipeline] isUnix
[Pipeline] sh
09:49:40  + docker run -d -t -e STAGE=foo tmp
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
09:49:45  + docker stop e5020f0e5416f34221d3cbc275fa44ccc05f9af03b138e4ec67d47b1d90a2d3a
09:49:45  e5020f0e5416f34221d3cbc275fa44ccc05f9af03b138e4ec67d47b1d90a2d3a
09:49:45  + docker rm -f e5020f0e5416f34221d3cbc275fa44ccc05f9af03b138e4ec67d47b1d90a2d3a
09:49:45  e5020f0e5416f34221d3cbc275fa44ccc05f9af03b138e4ec67d47b1d90a2d3a
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage

As you can see from the above, the string "STR is ..." is not included in the Jenkins job's console logs, but appears in my terminal's stdout just fine when I run the container myself.
I tried using docker.run() instead of docker.withRun(), but it didn't change the behavior.
Is there a way I can get the Docker container's stdout included in the Jenkins job's console logs?

**Tags:** docker, jenkins, jenkins-plugins


---

## 2 answers

### Answer by username_1 on 2023-05-24. Score: 1

You need to run that docker in interactive mode.
docker run --rm -it -e STR=foo tmp


-i - interactive mode
--rm - to delete the container once finished the process (tmp)


---

### Answer by username_2 on 2023-06-27. Score: 0

You can get the Docker stdout into a variable as follows and then log it at will.
script {
  myVar = sh(script: 'docker run -e STR=foo -t tmp', returnStdout: true).trim() 
  println "got ${myVar}"
}


---

