Search code examples
dockeramazon-ecsdocker-run

How bad is it to pipe process to consumer in ENTRYPOINT?


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.


Solution

  • 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