Can I read messages sent via stdout from a Docker container? (without "docker logs")

Motivation: To run a basic health check in a docker container, considering that a certain number of messages go through stdout over a certain time horizon

Immediate Goal:. Inside the shell launched with docker exec , read the data that is passed to standard output (stdout) from the main process (PID 1)

I'm not even sure if what I want is possible. If so, the explanation of why not be highly appreciated would help to advance my knowledge.

Steps to play:

  • Run 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 a different terminal window, docker exec to start another process in the same container docker exec -it container1 bash

Can I somehow tail / print / read send messages via stdout using PID 1?

I understand that there is work around - for example, laying through tee or otherwise writing to disk - but I was hoping for a magic bullet.

+6
source share
1 answer

If you're fine with strace , try the following:

 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 can narrow the output to STDOUT ( -e write=1,2 will show both STDOUT and STDERR)

Depending on the version of Docker, you may need to cancel the Sockall security policy for dockers, for example. completely disabling it by adding --security-opt seccomp:unconfined to docker run when the container starts:

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

More about secockp Docker profiles here (> 1.10).

Tested with

  • Windows 8.1
  • Docker version 1.10.2, assembly c3959b1
  • Docker machine version 0.6.0, assembly e27fb87
+2
source

Source: https://habr.com/ru/post/1013488/


All Articles