简体   繁体   中英

Python on docker to print out to stdout

I'm running a docker container that executes commands with server. It then logs the output to files. I have another docker that runs every few minutes and pick up everything from docker log {name} . I'm looking for a way to read the executions log files and print it to STDOUT for the logger service to pick the data to. I tried something like: subprocess.call("cat {latest_file}".format(latest_file=latest_file), shell=True) but it only print it to console whilst running.

my question is: can I add my own files/directories to the docker logger?

Assuming that you know the name of the log file beforehand, you can let the application continue to log as is (ie to a file) and symlink that file to /dev/stdout or /dev/stderr

This is a quite common solution, for example nginx does it

# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
    && ln -sf /dev/stderr /var/log/nginx/error.log

EDIT: Please note that this will only work if the applications that do logging is running as PID 0. If you have forked a process or similar, you will have to explicitly write to the stdout/stderr file descriptor of PID 1 (available under /proc/1/fd

# forward request and error logs to docker log collector
RUN ln -sf /proc/1/fd/1 /var/log/nginx/access.log \
    && ln -sf /proc/1/fd/2 /var/log/nginx/error.log

(Please see this answer for more details)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM