(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?