Docker Logging Basics
Docker captures container logs and provides multiple logging drivers to forward logs to different destinations. By default, Docker logs are written to the host’s filesystem.
Viewing Logs
To view logs for a running container, use:
docker logs container-name-or-id
Add the `-f` flag to follow logs in real-time:
docker logs -f container-name-or-id
Configuring Logging Drivers
Docker supports various logging drivers, such as `json-file`, `syslog`, and `fluentd`. To configure a logging driver for a container, use the `--log-driver` flag:
docker run --log-driver json-file my-image
Specify additional options with `--log-opt`:
docker run --log-driver syslog --log-opt syslog-address=tcp://192.168.1.100:514 my-image
Change the default logging driver globally by editing the `daemon.json` configuration file:
{ "log-driver": "json-file", "log-opts": { "max-size": "10m", "max-file": "3" } }
Restart the Docker daemon to apply changes:
sudo systemctl restart docker
Monitoring Docker Containers
Monitoring provides insights into container performance, resource usage, and potential issues.
Using Docker Stats
Docker’s built-in `docker stats` command displays real-time resource usage for running containers:
docker stats
The output includes metrics such as CPU usage, memory usage, network I/O, and block I/O.
Third-Party Monitoring Tools
1. Prometheus and Grafana: Collect and visualize container metrics using Prometheus for data scraping and Grafana for dashboards.
2. ELK Stack: Use Elasticsearch, Logstash, and Kibana for centralized log storage and analysis.
3. Datadog: A cloud-based solution for monitoring and logging Docker containers.
4. Sysdig: Provides deep visibility into container activity and resource usage.
5. cAdvisor: A lightweight monitoring tool for container resource usage and performance metrics.
Integrating Prometheus with Docker
To monitor Docker with Prometheus, use the `prom/node-exporter` and `prom/cadvisor` images. Here’s an example `docker-compose.yml` configuration:
version: "3.9" services: prometheus: image: prom/prometheus volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml ports: - "9090:9090" cadvisor: image: google/cadvisor ports: - "8080:8080" node-exporter: image: prom/node-exporter ports: - "9100:9100"
Access Prometheus at `http://localhost:9090` to query metrics collected from your containers.
Best Practices for Logging and Monitoring
1. Centralize Logs: Use a logging driver or external tools to collect logs in one place.
2. Set Resource Limits: Prevent excessive logging by configuring log rotation with size and file count limits.
3. Monitor Critical Metrics: Focus on CPU, memory, disk I/O, and network I/O usage.
4. Alert on Anomalies: Configure alerts for unusual resource usage or performance degradation.
5. Automate Cleanup: Prune unused logs and containers to save resources.
Conclusion
Effective logging and monitoring are essential for maintaining container health and ensuring smooth application performance. By leveraging Docker’s built-in features and integrating third-party tools, you can gain deep visibility into your containerized environments. Start implementing these techniques to enhance your Docker management today.