OLS
OLS

Reputation: 345

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?

Upvotes: 1

Views: 2958

Answers (1)

danielorn
danielorn

Reputation: 6167

Assuming that you know the name of the log file beforehand, you can let the application continue to log as is (i.e 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)

Upvotes: 2

Related Questions