在ENTRYPOINT中将流程传递给用户有多糟糕? [英] How bad is it to pipe process to consumer in ENTRYPOINT?

查看:97
本文介绍了在ENTRYPOINT中将流程传递给用户有多糟糕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Dockerfile中使用类似的代码有多糟糕:

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.

上述解决方案的主要问题是CMD参数不会追加到node进程吗?我认为他们会改为附加到tee进程?像这样的东西:

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

我认为那样的话:

node . | tee >(send_logs_to_elastic_search) --arg1 --arg2

有人知道吗?

另一个潜在的问题是您的容器的可配置性较差,它是硬编码"的,无法将日志发送到send_logs_to_elastic_search进程.

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.

推荐答案

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.

如果这仅与日志记录有关,我建议设置Docker日志记录驱动程序,而不是尝试在容器内部配置日志记录.这简化了图像设置(它仅需要应用程序,而不是每个可能的日志目标). logstash fluentd 是仅用于移动日志消息的流行工具.

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.

如果您正在寻找更复杂的脚本,我几乎总是将其写入独立的shell脚本中,而不是尝试将其直接写入Dockerfile中.

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", "."]

入口点脚本将接收命令部分作为命令行参数.通常,它会以exec "$@"结尾以仅运行该命令.如果您愿意让外壳程序包装成为主要的容器进程,则可以将命令的输出通过管道传递到某处

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

这篇关于在ENTRYPOINT中将流程传递给用户有多糟糕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆