Thumbnail: influxdata

Running Telegraf inside a docker container

by on under Monitoring
3 minute read

Telegraf is an application for collecting server and application telemetry and metrics and sending them to a time series datastore like InfluxDB. Like me you may prefer running all of your applications in Docker containers, however this means Telegraf will only collect data for the container. This article will cover the configuration options to allow Telegraf to collect host metrics from inside a container.


This article assumes you have already done the following:

  • Installed Docker
  • Installed and configured InfluxDB somewhere
  • Installed and configured you preferred dashboard for InfluxDB (Grafana or Chronograf are good options)

Generate config

First we need a copy of the default Telegraf config file to edit. Let’s create a directory to store it in and use the Telegraf docker image to run the -sample-config command to generate the file and direct the output into our directory.

mkdir telegraf
docker run --rm telegraf -sample-config > telegraf/telegraf.conf

Configure output

Open the configuration file telegraf/telegraf.conf in you favourite text editor and find the [[outputs.influxdb]] section. Change the url option to point at your influxdb instance. If you do not have a dns address for your influxdb host then leave it as influxdb and we’ll configure it in docker.

urls = ["http://influxdb:8086"] # required

Configure inputs

The configuration file will also have configuration sections for the different inputs you wish to collect. By default things like system cpu/memory/network usage are already enabled.

To enable collection of Docker data uncomment [[inputs.docker]] and the default entrypoint line.

Run the container

As Telegraf will be run inside a container we need to pass some host resources through including the docker socket, /proc, /sys and /etc. This will allow Telegraf to collect data from the whole host, not just what is visible to the container. We also need to give the container a specific hostname as this will be passed to InfluxDB, if we don’t set this the container ID will be used.

docker command

docker run -d --restart=always --add-host="influxdb:" --hostname=myhostname -e "HOST_PROC=/rootfs/proc" -e "HOST_SYS=/rootfs/sys" -e "HOST_ETC=/rootfs/etc" -v $(pwd)/telegraf/telegraf.conf:/etc/telegraf/telegraf.conf:ro -v /var/run/docker.sock:/var/run/docker.sock:ro -v /sys:/rootfs/sys:ro -v /proc:/rootfs/proc:ro -v /etc:/rootfs/etc:ro telegraf

docker compose

  image: telegraf
  restart: always
   - "influxdb:"
    HOST_PROC: /rootfs/proc
    HOST_SYS: /rootfs/sys
    HOST_ETC: /rootfs/etc
  hostname: myhostname
   - ./telegraf/telegraf.conf:/etc/telegraf/telegraf.conf:ro
   - /var/run/docker.sock:/var/run/docker.sock:ro
   - /sys:/rootfs/sys:ro
   - /proc:/rootfs/proc:ro
   - /etc:/rootfs/etc:ro

Checking in InfluxDB

Connect to the InfluxDB web GUI at http://your-influxdb-host:8083/ and select the telegraf database from the dropdown at the top. Then from the Query Templates dropdown select Show Measurements or run the query SHOW MEASUREMENTS.

You should then see a list of the measurements Telegraf is collection that looks like this.

InfluxDB Telegraf Measurements

telegraf, tick, influxdata, monitoring, docker
Spotted a mistake in this article? Why not suggest an edit!
comments powered by Disqus