Host & Container Monitoring with Grafana Alloy

Monitoring your systems and containers is essential for maintaining a reliable homelab or home server. This guide uses Grafana Alloy’s built-in exporters to collect host and container metrics, with Prometheus for storage and Grafana for visualization — all managed through a single agent instead of separate containers per exporter.

Prerequisites

Host Metrics

Create an Alloy config file for host system metrics:

BASH
nano alloy/config/unix.alloy
Click to expand and view more
unix.alloy
// Host system metrics (CPU, memory, disk, network)
prometheus.exporter.unix "host" {
  set_collectors = ["cpu", "diskstats", "filesystem", "loadavg", "meminfo", "netdev", "uname"]
  filesystem {
    mount_points_exclude = "^/(sys|proc|dev|host|etc)($$|/)"
  }
  netdev {
    device_exclude = "^(veth.*|br.*|docker.*|virbr.*|lo)$$"
  }
}

prometheus.scrape "host" {
  targets    = prometheus.exporter.unix.host.targets
  forward_to = [prometheus.remote_write.default.receiver]
  job_name   = "host"
}
Click to expand and view more

The set_collectors list controls which metrics are gathered. The filesystem and netdev excludes prevent virtual mounts and Docker bridge interfaces from cluttering your dashboards.

Container Metrics

Create an Alloy config file for Docker container metrics:

BASH
nano alloy/config/docker-metrics.alloy
Click to expand and view more
docker-metrics.alloy
// Docker container metrics (CPU, memory, network per container)
prometheus.exporter.cadvisor "dockermetrics" {
  docker_host      = "unix:///var/run/docker.sock"
  storage_duration = "5m"
}

prometheus.relabel "docker_filter" {
  forward_to = [prometheus.remote_write.default.receiver]

  rule {
    target_label = "job"
    replacement  = "docker"
  }
  rule {
    target_label = "instance"
    replacement  = constants.hostname
  }
  // Drop container_spec metrics that frequently contain NaN values
  rule {
    source_labels = ["__name__"]
    regex         = "container_spec_(cpu_period|cpu_quota|cpu_shares|memory_limit_bytes|memory_swap_limit_bytes|memory_reservation_limit_bytes)"
    action        = "drop"
  }
}

prometheus.scrape "dockermetrics" {
  targets         = prometheus.exporter.cadvisor.dockermetrics.targets
  forward_to      = [prometheus.relabel.docker_filter.receiver]
  scrape_interval = "10s"
}
Click to expand and view more

Docker Socket Access

Alloy needs access to the Docker socket to discover containers. Add the following volume mount to your Alloy docker-compose.yml and recreate the container:

docker-compose.yml
volumes:
  - /var/run/docker.sock:/var/run/docker.sock:ro
Click to expand and view more
BASH
docker compose up -d alloy
Click to expand and view more

Apply Configuration

Restart Alloy to load the new config files:

BASH
docker restart alloy
Click to expand and view more

Verification

Open the Alloy Web UI and confirm both components are healthy:

  • prometheus.exporter.unix
  • prometheus.exporter.cadvisor

Then verify data is flowing in Grafana’s Explore view:

PROMQL
node_cpu_seconds_total{job="host"}
Click to expand and view more
PROMQL
container_cpu_usage_seconds_total{job="docker"}
Click to expand and view more

Grafana Dashboards

You can create your own dashboards or use these as a starting point:

Copyright Notice

Author: Sven van Ginkel

Link: https://svenvg.com/posts/host-container-monitoring-with-grafana-alloy/

License: CC BY-NC-SA 4.0

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Please attribute the source, use non-commercially, and maintain the same license.

Start searching

Enter keywords to search articles

↑↓
ESC
⌘K Shortcut