I have a scenario where I run an executable (as an entrypoint) from a docker container.
The problem is, that executable doesn't write logs to stdout, but to a file.
I need a way to run that executable in the foreground (so that if it crashes, it crashes the container as well), but pipe logs from a file to stdout at the same time.
Any suggestion on how to do that?
CodePudding user response:
The Linux environment provides a couple of special files that actually relay to other file descriptors. If you set the log file to /dev/stdout
or /dev/fd/1
, it will actually appear on the main process's stdout.
The Docker Hub nginx
image has a neat variation on this. If you look at its Dockerfile it specifies:
RUN ln -sf /dev/stdout /var/log/nginx/access.log
The Nginx application configuration specifies its log file as /var/log/nginx/access.log
. If you do nothing, that is a symlink to /dev/stdout
, and so access logs appear in the docker logs
output. But if you'd prefer to have the logs in files, you can bind-mount a host directory on /var/log/nginx
and you'll get access.log
on the host as a file instead.