问题
How bad would it be to use something like this in a Dockerfile:
ENTRYPOINT node . | tee >(send_logs_to_elastic_search)
most of the logging solutions require some pretty nasty configuration. The above would be a way for us to capture the logs programmatically and write our own glue code.
The main problem with the above solution is that CMD
arguments would not append to the node
process? I assume they would get append to the tee
process instead? something like this:
docker run foo --arg1 --arg2
I assume that would then look like:
node . | tee >(send_logs_to_elastic_search) --arg1 --arg2
anybody know?
The other potentially problem is that your container is less configurable it's "hardcoded" to send the logs to the send_logs_to_elastic_search
process.
回答1:
The Dockerfile documentation indicates that if you use the shell form of ENTRYPOINT
then CMD
is completely ignored. If it weren't, then the CMD
would be appended in essentially the way you show.
If this is just about logging, I'd recommend setting up a Docker logging driver over trying to configure logging inside the container. This simplifies your image setup (it only needs the application and not every possible log target). Both logstash and fluentd are popular tools for just moving log messages around.
If you're looking at more complicated scripting, I would almost always write this into a standalone shell script, rather than trying to write it directly into the Dockerfile.
...
COPY docker-entrypoint.sh /
RUN chmod +x /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["node", "."]
The entrypoint script will receive the command part as command-line arguments. Typically it would end with exec "$@"
to just run that command. If you're willing to let the shell wrapper be the main container process, you could pipe the command's output somewhere
#!/bin/sh
"$@" | send_logs_to_elasticsearch
回答2:
How are you running your containers?
Typically you can connect filebeats, or something else directly to /var/lib/docker/containers/*/*.json.log
and have the host send all the logs for all the containers
Here is a tutorial for filebeats which is nice as they do a volume mount and do the log extract with another container
来源:https://stackoverflow.com/questions/58001832/how-bad-is-it-to-pipe-process-to-consumer-in-entrypoint