Docker and logs

How docker logging works ?
It’s very simple, to log from a container, you have to write in stdout !
Where container logs are sent ? How can I see it ?
It’s a best practice to log on stdout from a container, it’s an unified interface and directly available from docker logs -f [container] or through docker-compose logs -f. Howerver docker save the output produced by containers in /var/lib/docker/containers/[CONTAINER ID]/[CONTAINER_ID]-json.log by default. Json file is the default log driver, the most easier.
Docker many built in log driver like as following :
- none: No logs will be available for the container and docker logs will not return any output.
- json-file: The logs are formatted as JSON. The default logging driver for Docker.
- syslog: Writes logging messages to the syslog facility. The syslog daemon must be running on the host machine.
- journald: Writes log messages to journald. The journald daemon must be running on the host machine.
- gelf: Writes log messages to a Graylog Extended Log Format (GELF) endpoint such as Graylog or Logstash.
- fluentd: Writes log messages to fluentd (forward input). The fluentd daemon must be running on the host machine.
- awslogs: Writes log messages to Amazon CloudWatch Logs.
- splunk: Writes log messages to splunk using the HTTP Event Collector.
- etwlogs: Writes log messages as Event Tracing for Windows (ETW) events. Only available on Windows platforms.
- gcplogs: Writes log messages to Google Cloud Platform (GCP) Logging.
- nats: NATS logging driver for Docker. Publishes log entries to a NATS server.
docker logs command is only available with json-file and journald, than mean you need to bring your infrastructure to visualize your logs!
With json-file driver you will out of disk space, mostly if you work with worker (or application) which produce a huge amount of logs. You specify some parameters in order to avoid that.
- max-size The maximum size of the log before it is rolled. A positive integer plus a modifier representing the unit of measure (k, m, or g).
--log-opt max-size=0m
- max-file The maximum number of log files that can be present. If rolling the logs creates excess files, the oldest file is removed. Only effective when max-size is also set. A positive integer.
--log-opt max-file=3
- labels Applies when starting the Docker daemon. A comma-separated list of logging-related labels this daemon will accept. Used for advanced log tag options.
--log-opt labels=production_status,geo
- env Applies when starting the Docker daemon. A comma-separated list of logging-related environment variables this daemon will accept. Used for advanced log tag options.
--log-opt env=os,customer
If you want to learn more about docker logging driver and configuration, you can checkout this page https://docs.docker.com/engine/admin/logging/overview/
Logrotate
If you prefer that your host manage logs via logrotate rather than via container definition.
vim /etc/logrotate.d/docker
/var/lib/docker/containers/*/*.log {
rotate 7
daily
compress
size=1M
missingok
delaycompress
copytruncate
}
And then
logrotate -fv /etc/logrotate.d/docker
comments powered by Disqus