0

I'm using docker logging plugin to send logs to storage. Now I want to store not only application logs, but also it's docker container's stats (stats command output: CPU, MEM, etc.) collected every few seconds.

Is there a way to configure docker/docker-compose to write container's stats to the same output as logs?

1 Answer 1

0

Have not found a built-in way to include container stats to the container's log, but part of our solution might get you part-way there:

We have a CRON job on the Docker host that executes this script every minute, resulting in a reverse-chronological* ordered log of the container's stats:

#!/bin/bash
# statshot.sh

MAX_LINES=1440
CONTAINER_NAME=my_container
LOG=~/${CONTAINER_NAME}.stats.log

# docker stats-heading + stats to new log
docker stats --no-stream | grep -e 'CONTAINER' -e ${CONTAINER_NAME} | ts '[%Y-%m-%d %H:%M]' > ${LOG}_

# append lines from running log to new log
# (pre-touch incase it does not exist yet)
touch ${LOG}
grep ${CONTAINER_NAME} ${LOG} | head -${MAX_LINES} >> ${LOG}_

# replace running log with new log
mv ${LOG}_ ${LOG}

Run every minute and rolling off after 1440 lines, this gives us 24-hour history of the container's vitals.

For your needs (including stats in the container log) the log file generated on the host could just contain the single/current stats line, but written to a location that the container can read from, and a corresponding CRON job in the container echo it to console

* reverse-order is a side effect of wanting to include the Docker stats header but with a simpler script and less file I/O ... any script-fu suggestions to reduce file I/O further are welcome

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .