I'm trying to run my docker-compose containers via a systemd service, and have fail2ban read their logs. However, docker-compose adds a prefix like:
container_name_1 | Actual log message
So what I'm trying to do is something like docker-compose up --no-color 2>&1 | sed 's/^[^ ]* *| //'
to strip that prefix so that fail2ban can match on the log lines correctly. But when I do that, I only see Started mydockercontainers.service
in the journalctl logs. Removing the pipe to sed fixes everything.
How can I remove this prefix and keep the log messages in the journal?
Most programs including sed will use block buffering, not line buffering, when their output is something else than an interactive tty device. (For services, the stdout is a pipe and you would see a similar effect with sed | cat
.)
This means output will be delayed while it accumulates in the buffer – it will only be written in chunks of 10-20 lines or so (however many fit in a 4kB buffer). Use the sed -u
or --unbuffered
option to disable the buffering.
In cases where the program doesn't have such an option, wrap it with stdbuf -i0 -o0
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments