Motivation: To run a basic health-check on a docker container by counting that a certain number of messages flow across stdout over a certain time horizon
Immediate goal: From within a shell started by docker exec
, read data that is being piped to stdout from the main process (PID 1)
I am not even sure if what I want is possible. If that is the case, an explanation as to why not would be much appreciated -- and would help advance my knowledge.
Steps to reproduce:
Start the container -- container1
docker run -it --name container1 ubuntu bash -c -i "COUNT=0; while true; do echo Keep the dance floor beat going; ((COUNT++)); sleep 1; echo \"Count is: \${COUNT}\"; done;"
In another terminal window, docker exec
to start another process in the same container
docker exec -it container1 bash
Can I somehow tail
/print
/read
the messages being passed over stdout by PID 1?
I understand that there are work arounds -- for example piping through tee
or otherwise writing to disk -- but was hoping for a magic bullet.
STDOUT is usually a command's normal output, and STDERR is typically used to output error messages. By default, docker logs shows the command's STDOUT and STDERR .
First of all, to list all running containers, use the docker ps command. Then, with the docker logs command you can list the logs for a particular container. Most of the time you'll end up tailing these logs in real time, or checking the last few logs lines.
Detached mode, shown by the option --detach or -d , means that a Docker container runs in the background of your terminal. It does not receive input or display output. If you run containers in the background, you can find out their details using docker ps and then reattach your terminal to its input and output.
If you are OK with strace
then try this:
docker exec -it container1 bash -c -i "\
apt-get update && apt-get install strace && \
strace -ff -e trace=write -e write=1 -p 1"
-p 1
is the PID-e write=1
is there to narrow the output to STDOUT (-e write=1,2
would show both STDOUT and STDERR)Depending on Docker version you might need to loosen up Docker's syscall security policy, e.g. by disabling it completely by adding --security-opt seccomp:unconfined
to docker run
when starting the container:
docker run --security-opt seccomp:unconfined -it --name container1 ubuntu bash -c -i "COUNT=0; while true; do echo Keep the dance floor beat going; ((COUNT++)); sleep 5; ech o \"Count is: \${COUNT}\"; done;"
Read more about the Docker's seccomp profiles here (>1.10).
Tested with:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With