Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture the output of a program run in Docker

Tags:

docker

I use docker to run a bash script and I need to capture its output and just its output.

docker run --rm --volume=/tmp/test.sh:/tmp/test.sh:ro --entrypoint=/bin/bash node:4.6.0 -xe /tmp/test.sh

The problem is that I also get the output from docker downloading the image. Thinks like:

Unable to find image 'node:4.6.0' locally
4.6.0: Pulling from library/node
6a5a5368e0c2: Pulling fs layer
7b9457ec39de: Pulling fs layer
ff18e19c2db4: Pulling fs layer
6a3d69edbe90: Pulling fs layer
0ce4b037e17f: Pulling fs layer

Is there a way to only capture the output generated by the bash script run by docker without any additional messages/errors/warnings that docker may output?

like image 631
daniels Avatar asked Apr 04 '17 20:04

daniels


1 Answers

The docker output is sent to stderr so it can be differentiated from your normal script output.

If you need both stderr and stdout from the container use the output from docker logs, which is the safest option but a bit more complex to setup. Note logs only works locally with the default json-file and journald log drivers.

CID=$(docker run -d --volume=/tmp/test.sh:/tmp/test.sh:ro \
        --entrypoint=/bin/bash node:4.6.0 -xe /tmp/test.sh)
docker wait $CID
docker logs $CID
docker rm $CID

If you are only interested in the stdout of your process, redirect stdout to a file. Any error output, along with dockers, will go to screen. The file will only contain stdout data.

docker run --rm --volume=/tmp/test.sh:/tmp/test.sh:ro \
  --entrypoint=/bin/bash node:4.6.0 -xe /tmp/test.sh > output

You could also redirect stderr to stdout inside the container so you only lose the docker stderr output.

docker run -rm --volume=/tmp/test.sh:/tmp/test.sh:ro \
  --entrypoint=/bin/bash node:4.6.0 -xec '/tmp/test.sh 2>&1' > output
like image 64
Matt Avatar answered Oct 21 '22 15:10

Matt