Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to read messages passed over stdout from within a Docker container? (without `docker logs`)

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:

  1. 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;"

  2. 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.

like image 864
JacobWuzHere Avatar asked Dec 24 '16 02:12

JacobWuzHere


People also ask

What is stdout in docker?

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 .

How do you access container logs in container runtime?

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.

How can you see the output of a process running in a detached container?

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.


1 Answers

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:

  • Windows 8.1
  • Docker version 1.10.2, build c3959b1
  • Docker-machine version 0.6.0, build e27fb87
like image 172
jannis Avatar answered Oct 26 '22 05:10

jannis