1 year ago

#312529

test-img

user18001074

How to catch stderr inside a docker container?

I've built a docker image named conda with several dependencies. And I've written a node.js script to launch a container of this image. However, I cannot catch exceptions that I raise inside the container. Here's my node.js demo test.js:

const child_process = require('child_process');

child_process.exec(`docker run --rm -t -v "$PWD:/env" --entrypoint python conda test.py`, (error, stdout, stderr) => {
    if (stdout) console.log('stdout: ', stdout);
    if (stderr) console.log('stderr: ', stderr);
})

where the test.py is simply:

import sys

raise ValueError('this is a value error')

When I run node test.js from terminal, the output is:

stdout:  Traceback (most recent call last):
  File "/env/test.py", line 3, in <module>
    raise ValueError('this is a value error')
ValueError: this is a value error

Which shows that a raised exception is caught as stdout inside the docker container. However if I change my js file to:

const child_process = require('child_process');

child_process.exec(`python test.py`, (error, stdout, stderr) => {
    if (stdout) console.log('stdout: ', stdout);
    if (stderr) console.log('stderr: ', stderr);
})

Then the output is logged as expected:

stderr:  Traceback (most recent call last):
  File "/home/vector/playgournd/test.py", line 3, in <module>
    raise ValueError('this is a value error')
ValueError: this is a value error

From my point of view there must be some tricks with stream piping inside docker. Anyone can help?

docker

stderr

0 Answers

Your Answer

Accepted video resources