Jacob Tomlinson's profile picture Jacob Tomlinson
Home Blog Talks About

Running Telegraf inside a docker container

3 minute read #telegraf, #tick, #influxdata, #monitoring, #docker

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:

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

Have thoughts?

I love hearing feedback on my posts. You should head over to Twitter and let me know what you think!

Spotted a mistake? Why not suggest an edit!