Score:0

How to write docker stats to logs?

cn flag

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?

Score:0
cn flag

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

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.