Capturing the output of a program running in Docker
I am using docker to run a bash script and I need to log its output and only 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 I am also getting output from the docker loading 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 a bash script run by docker without any additional messages / errors / warnings that docker can output?
source to share
The docker output is sent to stderr, so it can be distinguished from your normal script output.
If you need both stderr and stdout from a container, use output docker logs
, which is the safest option, but slightly more complex to configure. Note journals only work locally with journal settings json-file
and journald
.
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're only interested in the standard process, redirect stdout to a file. Any error output as well as docker will show up on the 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 can also redirect stderr to stdout inside the container so that you only lose 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
source to share