Simplifying Log Shipping with Filebeat Autodiscover: A Practical Guide
Filebeat’s autodiscover feature lets a single config file dynamically create log inputs based on container metadata. Learn how to set it up for Docker, test it, and understand the trade‑offs in this practical guide.
04 Dec 2025, 11:22 UTC

Why Autodiscover Matters
When you run dozens of containers or micro‑services, writing a separate Filebeat input for each log source is a maintenance nightmare. Every new container, image update, or environment change forces you to edit configuration files, restart Filebeat, and risk mis‑configuring a stream. Filebeat’s autodiscover feature solves this by letting a single configuration file generate inputs on the fly based on container or host metadata.
How Autodiscover Works
Autodiscover listens to a metadata source—Docker, Kubernetes, or a custom API—and applies templates that match labels, annotations, or other attributes. When a match is found, Filebeat automatically creates an input that starts reading the container’s log file and forwards it to the configured output.
Key points:
- Templates are written in Filebeat’s standard config language.
- Matching is done by simple key/value look‑ups (e.g.,
docker.labels.foo == "bar"). - Generated inputs are added to Filebeat’s runtime configuration; no manual file edits are required.
Step‑by‑Step Example: Shipping Docker Logs with a Label
- Prerequisites
- Filebeat 8.x installed on the host.
- Docker socket access (or Docker API endpoint) granted to the Filebeat user.
- Container(s) running with a custom label, e.g.,
com.example.log=true.
- Enable Autodiscover in
filebeat.ymlfilebeat.autodiscover: providers: - type: docker hints.enabled: true hints.default_config.enabled: falseThis tells Filebeat to watch Docker for containers with the
hints.enabledflag. Thehints.default_config.enabled: falseline prevents Filebeat from creating a generic input for every container. - Create a Template for Labeled Containers
filebeat.autodiscover: providers: - type: docker hints.enabled: true hints.default_config.enabled: false templates: - condition: equals: docker.labels.com.example.log: "true" config: - type: container paths: "/var/lib/docker/containers/*/*.log" multiline.pattern: ^\d{4}-\d{2}-\d{2} multiline.negate: true multiline.match: afterThe
conditionblock matches containers that carry the specified label. When a match occurs, Filebeat creates acontainerinput that reads the container’s JSON log file and applies a multiline pattern to group stack traces. - Verify the Configuration
sudo filebeat test config -eLook for lines such as
autodiscover: template matched for container ...in the output. This confirms the template is syntactically correct and will be applied when a matching container starts. - Run Filebeat
sudo systemctl start filebeat sudo systemctl status filebeatCheck the logs for entries like
autodiscover: created input for container .... If you see the input, logs from the container should now be forwarded to your Elasticsearch or Logstash endpoint. - Test with a Sample Container
docker run -d --label com.example.log=true nginx:latestAfter a few seconds, use
curl http://localhost:9200/_cat/indices(or your monitoring UI) to confirm the index is receiving data.
Trade‑offs and Limitations
- Permissions: Filebeat must have read access to the Docker socket or Kubernetes API. On Kubernetes, this means setting up a ServiceAccount with the
cluster-readerrole or similar. Granting too many permissions can expose sensitive metadata. - Duplicate Streams: If multiple templates match the same container, Filebeat will create multiple inputs, potentially causing duplicate log entries. Carefully design template conditions to be mutually exclusive.
- Runtime Overhead: Autodiscover polls the metadata source at a configurable interval (default 30 s). The added CPU and network usage is usually negligible but can add up in very large clusters.
- Version Support: Autodiscover is available from Filebeat 7.0 onward. On older releases, you’ll need to manually edit the
filebeat.ymlfor each container.
Actionable Next Steps
- Audit your container labels or Kubernetes annotations to ensure they are unique and consistently applied.
- Write a small test harness that spins up a container with the required label and verifies that Filebeat creates the input and forwards logs.
- Implement a
filebeat.test.configCI step that runs against your productionfilebeat.ymlto catch template syntax errors before deployment. - Monitor Filebeat’s
autodiscoverlogs for any unexpected template matches or failures. - Consider adding custom processors (e.g.,
drop_fields) in your template to filter out noisy logs from specific services.
By leveraging autodiscover, you turn a static, error‑prone configuration into a dynamic, maintainable system that scales with your container fleet. Once the templates are in place, adding a new micro‑service is as simple as attaching the right label—no Filebeat restart required.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.