Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker container - Send log from a process other than main (PID1)

I can see all STDOUTs and STDERRs came from my-container's main/initial process (PID1) are sent to docker logs my-container and the same is valid for any application that is run in this process.

For example, when I docker attach to my container, echo and also other commands' outputs are sent to logs, but when I'm in a process other than the main, e.g when I run docker exec, no any outputs are sent to logs. Any solutions?

like image 761
Reza Nooralizadeh Avatar asked Oct 30 '25 13:10

Reza Nooralizadeh


1 Answers

Redirect the output to /proc/1/fd/1 to get it into the logs.

As an example, start a container with

docker run -d --name mycontainer debian tail -f /dev/null

then put the contents of /etc/os-release into the logs with

docker exec mycontainer sh -c 'cat /etc/os-release > /proc/1/fd/1'

Note that you have to have the redirection in single quotes. If you don't, it'll be handled by the shell on the host. By using quotes, it's handled by the shell inside the container which is what you want.

docker logs mycontainer will now show the contents of the file.

So basically write whatever you want to /proc/1/fd/1 and it'll show up in the log.

like image 88
Hans Kilian Avatar answered Nov 02 '25 05:11

Hans Kilian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!