12

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?

1
  • You can direct the script output to a file (e.g. /tmp/test.log) and map the file/folder to one on the host using the -v option Commented Apr 5, 2017 at 0:10

1 Answer 1

10

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.