I've got several Docker containers running together as a Docker swarm. The software running in each of the containers is a Python code. Each of the components writes into their own log. When I debug the whole system, I constantly find myself greping through many logs in parallel trying to arrange the log entries in order...:
> docker container logs -f container1
> docker container logs -f container2
> docker container logs -f container3
Is there a way to make all (or just some) Docker containers log into the same log?
I've got several Docker containers running together as a Docker swarm. The software running in each of the containers is a Python code. Each of the components writes into their own log. When I debug the whole system, I constantly find myself greping through many logs in parallel trying to arrange the log entries in order...:
> docker container logs -f container1
> docker container logs -f container2
> docker container logs -f container3
Is there a way to make all (or just some) Docker containers log into the same log?
This is possible, You can redirect all Docker logs into a central file using a custom script that combines docker logs for each container and timestamps them.
#!/bin/bash
output_file="/path/to/centralized-log.log"
containers=$(docker ps --format "{{.Names}}")
for container in $containers; do
docker logs -f "$container" | sed "s/^/[$container] /" >> "$output_file" &
done
wait
In Docker Swarm, you can use services like Fluentd, Graylog, or a shared space for all logs
version: '3.*'
services:
app:
image: my-app
logging:
driver: "fluentd"
options:
fluentd-address: "fluentd:****"
(Replace the * with numbers that you are using)