aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • Containers
  • Programming
  • Software Engineering

Docker Monitoring Tutorial – How To monitor Docker With Telegraf And InfluxDB

  • aster.cloud
  • June 13, 2022
  • 7 minute read

How to Monitor Docker with Telegraf and InfluxDB

Docker is an increasingly popular choice for businesses dealing with containerized applications. However, as with any new technology, Docker introduces complexities that need to be managed. Some of these complexities relate to infrastructure and application monitoring. Due to the abstraction offered by containers, traditional monitoring solutions might not be suitable for Docker-based workloads.

Thankfully, tools like InfluxDB and Telegraf can help you mitigate this complexity, letting you monitor Docker-based applications easily. This tutorial will teach you how to set up and configure InfluxDB and Telegraf to collect metrics from a Docker installation.


Partner with aster.cloud
for your next big idea.
Let us know here.



From our partners:

CITI.IO :: Business. Institutions. Society. Global Political Economy.
CYBERPOGO.COM :: For the Arts, Sciences, and Technology.
DADAHACKS.COM :: Parenting For The Rest Of Us.
ZEDISTA.COM :: Entertainment. Sports. Culture. Escape.
TAKUMAKU.COM :: For The Hearth And Home.
ASTER.CLOUD :: From The Cloud And Beyond.
LIWAIWAI.COM :: Intelligence, Inside and Outside.
GLOBALCLOUDPLATFORMS.COM :: For The World's Computing Needs.
FIREGULAMAN.COM :: For The Fire In The Belly Of The Coder.
ASTERCASTER.COM :: Supra Astra. Beyond The Stars.
BARTDAY.COM :: Prosperity For Everyone.

Why monitor Docker

As with traditional non-containerized workloads, there are several reasons why it’s crucial to monitor your Docker-based applications. Keeping an eye on the critical metrics of your application allows you to be proactive when it comes to things like performance issues and resource allocation optimizations. If your application is routinely consuming far more resources than you would expect, you need to know about this sooner rather than later. If left unattended, these issues can result in unexpected bills from cloud providers or support requests from unsatisfied customers (if there are underlying performance issues).

If you have detailed metrics of how your application behaves at runtime, it puts you in a position to solve problems early. Typically, you want to collect metrics like CPU and memory usage, as well as Disk and Network I/O, and any number of other, more specific metrics depending on your application’s needs.

However, collecting large volumes of this kind of data can quickly become tricky. You need to collect the data and store it in a meaningful way and have a mechanism to pull the data back out for analysis and visualization. This is where InfluxDB and Telegraf come in.

InfluxDB is a purpose-built time series database with a lot of powerful features. In particular, the built-in data visualization tools and the powerful Flux data scripting language are appealing additions to an already robust time series database offering. InfluxDB can then be paired with Telegraf, a server-based agent for collecting metrics from various systems. Thanks to a flexible plugin architecture, Telegraf already supports over 300 plug-ins for collecting metrics from different endpoints, including Docker.

Prerequisites

Before you begin this tutorial, you’ll need to have Docker installed on your system and configured correctly. You can refer to the official documentation for steps to install Docker on your operating system of choice. If you’re using a Unix-based OS, you can also add your user to the Docker group to let you work with Docker as a non-root user.

Once you have Docker installed and configured, you can verify that it’s working as expected by running the following command:

docker run hello-world

This command should print some output to confirm that things are functioning correctly:

Docker run hello-world output

You should also note that this tutorial assumes you are using a Linux-based OS, like Ubuntu, for the purposes of code snippets and commands. There is one section later in the tutorial where a command will fail if you’re using macOS, but rest assured, there is a simple solution if this happens.

Read More  Why Alibaba Cloud Uses KEDA For Application Autoscaling

Monitoring Docker with InfluxDB and Telegraf

With Docker installed, you can now look to install and configure InfluxDB and Telegraf. While you could install both of these pieces of software directly on your machine, this tutorial will show you how to install and integrate them as Docker containers.

Before creating the containers, you need to create a Docker network so that they can communicate with each other. You can do this with the following command:

docker network create --driver bridge influxdb-net

InfluxDB

Now you need to create a new influx-data/ directory. You’ll mount this directory to the Docker volume used by this container, which means that your data will be able to persist even if the container stops running:

<span class="token function">mkdir</span> influx-data <span class="token operator">&&</span> <span class="token builtin class-name">cd</span> influx-data

After entering the directory, you can run the following command to pull the influxdb Docker image and create a container using it (with your influx-data/ directory mapped to the container’s volume):

docker run <span class="token punctuation">\</span>
	--name influxdb <span class="token punctuation">\</span>
	-p <span class="token number">8086</span>:8086 <span class="token punctuation">\</span>
	--volume <span class="token environment constant">$PWD</span>:/var/lib/influxdb2 <span class="token punctuation">\</span>
	--net<span class="token operator">=</span>influxdb-net <span class="token punctuation">\</span>
	-d <span class="token punctuation">\</span>
	influxdb:2.1.1

Running this command will start an instance of InfluxDB in a container, accessible at your host’s IP address on port 8086. Before configuring Telegraf, you need to set up your InfluxDB installation and set some parameters, like your bucket name.

Configuring InfluxDB

Navigate to port 8086 on your Docker host’s IP address (if you are running Docker locally, try http://127.0.0.1:8086/.) Now, you should see the InfluxDB welcome screen. Click Get started. And you’ll be prompted to create your initial user. Make a note of the Initial Organization Name and Initial Bucket Name you provide here, as you will need them shortly for the Telegraf config:

Set up the initial user

Once you’ve filled out these details, click Continue, and on the final screen, choose Configure Later. Then you’ll be taken back to the main dashboard. From there, select Load your data, and on the subsequent page, select the Telegraf tab:

Create a Telegraf config

On this tab, select + Create Configuration. You’ll be asked what kind of system you want to monitor. Select Docker and click Continue. Give the configuration a semantic name (like “Docker”) and click Create and verify. Then InfluxDB will present you with an API token and a URL that will give you a partial config file for Telegraf:

Configure your API key

Take note of this API key, as you will need it shortly.

With this API key, you have the final piece you need to configure your Telegraf container and start logging some metrics. For this tutorial, you don’t need the provided configuration file because one will be provided for you that includes a few extra pieces.

Telegraf

Back in your terminal, if you’re still in the influx-data/ directory, you can leave this directory and create a new one called telegraf-data/. This directory will hold the Telegraf config file that Docker will mount into the container:

<span class="token comment"># If you’re still in the influx-data/ directory</span>
<span class="token builtin class-name">cd</span> <span class="token punctuation">..</span>

<span class="token function">mkdir</span> telegraf-data <span class="token operator">&&</span> <span class="token builtin class-name">cd</span> telegraf-data

In this directory, create a file called telegraf.conf and set its contents as follows:

<span class="token punctuation">[</span>agent<span class="token punctuation">]</span>
  interval <span class="token operator">=</span> <span class="token string">"10s"</span>
  round_interval <span class="token operator">=</span> <span class="token boolean">true</span>
  metric_batch_size <span class="token operator">=</span> <span class="token number">1000</span>
  metric_buffer_limit <span class="token operator">=</span> <span class="token number">10000</span>
  collection_jitter <span class="token operator">=</span> <span class="token string">"0s"</span>
  flush_interval <span class="token operator">=</span> <span class="token string">"10s"</span>
  flush_jitter <span class="token operator">=</span> <span class="token string">"0s"</span>
  precision <span class="token operator">=</span> <span class="token string">""</span>
  <span class="token function">hostname</span> <span class="token operator">=</span> <span class="token string">""</span>
  omit_hostname <span class="token operator">=</span> <span class="token boolean">false</span>
 
<span class="token punctuation">[</span><span class="token punctuation">[</span>inputs.docker<span class="token punctuation">]</span><span class="token punctuation">]</span>
  endpoint <span class="token operator">=</span> <span class="token string">"unix:///var/run/docker.sock"</span>
  gather_services <span class="token operator">=</span> <span class="token boolean">false</span>
  container_names <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  source_tag <span class="token operator">=</span> <span class="token boolean">false</span>
  container_name_include <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  container_name_exclude <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  <span class="token function">timeout</span> <span class="token operator">=</span> <span class="token string">"5s"</span>
  perdevice <span class="token operator">=</span> <span class="token boolean">true</span>
  total <span class="token operator">=</span> <span class="token boolean">false</span>
  docker_label_include <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  docker_label_exclude <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  tag_env <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"JAVA_HOME"</span>, <span class="token string">"HEAP_SIZE"</span><span class="token punctuation">]</span>

<span class="token punctuation">[</span><span class="token punctuation">[</span>outputs.influxdb_v2<span class="token punctuation">]</span><span class="token punctuation">]</span>
  urls <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"http://influxdb:8086"</span><span class="token punctuation">]</span>
  token <span class="token operator">=</span> <span class="token string">"<span class="token variable">$INFLUX_TOKEN</span>"</span>
  organization <span class="token operator">=</span> <span class="token string">"influx-docker-demo"</span>
  bucket <span class="token operator">=</span> <span class="token string">"influx-docker-demo"</span>

You might need to edit the last block in this file if you used a different organization or bucket name. Similarly, if you gave your InfluxDB container a name other than influxdb, you will need to reflect that change in the value for the urls key.

Read More  A Guide To Setting Up Kubernetes Service Level Objectives (SLOs) With Prometheus And Linkerd

Once you’ve created this file, you can run the following command from your telegraf-data/ directory to create the container:

docker run -d --name<span class="token operator">=</span>telegraf <span class="token punctuation">\</span>
	-v <span class="token variable">$(<span class="token builtin class-name">pwd</span>)</span>/telegraf.conf:/etc/telegraf/telegraf.conf <span class="token punctuation">\</span>
	-v /var/run/docker.sock:/var/run/docker.sock <span class="token punctuation">\</span>
	--net<span class="token operator">=</span>influxdb-net <span class="token punctuation">\</span>
	--user telegraf:<span class="token variable">$(<span class="token function">stat</span> -c <span class="token string">'%g'</span> /var/run/docker.sock)</span> <span class="token punctuation">\</span>
	--env <span class="token assign-left variable">INFLUX_TOKEN</span><span class="token operator">=</span><span class="token operator"><</span>your_api_key<span class="token operator">></span> <span class="token punctuation">\</span>
	telegraf

Note: This command may fail if you use macOS because the stat command varies slightly between Mac and Linux. If this happens, try substituting the -c flag for an -f flag like so: –user telegraf:$(stat -f ‘%g’ /var/run/docker.sock) \.

The previous command does a few important things for it to work correctly. Here it is line by line:

  • The first -v flag mounts the config file you just created.
  • The second -v flag mounts the host’s Docker socket, which is what allows Telegraf to collect details about your Docker server.
  • The –net flag ensures that this container and the previously created InfluxDB container are both on the same network, which is how the config file you created can refer to the InfluxDB container by its DNS name – influxdb.
  • The –user flag is necessary because the Telegraf image internally uses a user and group named telegraf, so you must map this to a user who has permission to access the docker.sock file.
  • The –env flag specifies an environment variable containing the API key for your InfluxDB instance used in the config file you created.
Read More  Flux Project Update – July 2021

After running this command, you can check the Docker logs to see if there are any unexpected issues:

docker logs telegraf

If everything is working, you should see some output like this, without errors:

Docker logs

If all looks well, you’re ready to visualize your data in InfluxDB.

If you see errors about permission being denied when trying to access /var/run/docker.sock, you can resolve this error by running the following command, which will alter the permission on the socket in the container, allowing the telegraf user to access it properly:

docker <span class="token builtin class-name">exec</span> -u root -it telegraf /bin/sh -c <span class="token string">"chmod 666 /var/run/docker.sock"</span>
Visualizing the data

Navigate back to InfluxDB in your browser, and select Data from the sidebar. Next, select the Buckets tab and then the name of the bucket that you configured Telegraf to use:

Data buckets

This will take you to the Data Explorer page, where you should be able to see the data that Telegraf is feeding into InfluxDB. By default, there are quite a few metrics captured, including CPU and memory usage and networking statistics. You can click through the different filters available at the bottom of the screen to see all the other available data:

Data explorer

For more information about how to derive meaningful visualizations from your data, you can refer to the official documentation to learn more.

Wrapping up

In this tutorial, you learned how to monitor critical metrics from Docker using Telegraf and InfluxDB.

As a time series database, InfluxDB is perfectly positioned to store and visualize the kind of metrics that application monitoring often deals with, as there are usually lots of data points at regular intervals. With large volumes of data like this, you must have a mechanism to visualize, search, and understand the data to derive insights. InfluxDB fits the bill in this regard thanks to its easy-to-configure visualizations and the powerful Flux data scripting language that allows you to query and analyze your data.

Author Bio

Cameron is a full-stack dev living and working in Melbourne. He’s committed himself to the never-ending journey of understanding the intricacies of quality code, developer productivity, and job satisfaction.

 

By Cameron Pavey
Source CNCF


For enquiries, product placements, sponsorships, and collaborations, connect with us at [email protected]. We'd love to hear from you!

Our humans need coffee too! Your support is highly appreciated, thank you!

aster.cloud

Related Topics
  • CNCF
  • Docker
  • InfluxDB
  • Telegraf
You May Also Like
View Post
  • Software Engineering
  • Technology

Claude 3.7 Sonnet and Claude Code

  • February 25, 2025
View Post
  • Engineering
  • Software Engineering

This Month in Julia World

  • January 17, 2025
View Post
  • Engineering
  • Software Engineering

Google Summer of Code 2025 is here!

  • January 17, 2025
View Post
  • Software Engineering

5 Books Every Beginner Programmer Should Read

  • July 25, 2024
Ruby
View Post
  • Software Engineering

How To Get Started With A Ruby On Rails Project – A Developer’s Guide

  • January 27, 2024
View Post
  • Engineering
  • Software Engineering

5 Ways Platform Engineers Can Help Developers Create Winning APIs

  • January 25, 2024
Clouds
View Post
  • Cloud-Native
  • Platforms
  • Software Engineering

Microsoft Releases Azure Migrate Assessment Tool For .NET Application

  • January 14, 2024
View Post
  • Software Engineering
  • Technology

It’s Time For Developers And Enterprises To Build With Gemini Pro

  • December 21, 2023

Stay Connected!
LATEST
  • 1
    Pure Accelerate 2025: All the news and updates live from Las Vegas
    • June 18, 2025
  • 2
    ‘This was a very purposeful strategy’: Pure Storage unveils Enterprise Data Cloud in bid to unify data storage, management
    • June 18, 2025
  • What is cloud bursting?
    • June 18, 2025
  • 4
    There’s a ‘cloud reset’ underway, and VMware Cloud Foundation 9.0 is a chance for Broadcom to pounce on it
    • June 17, 2025
  • What is confidential computing?
    • June 17, 2025
  • Oracle adds xAI Grok models to OCI
    • June 17, 2025
  • Fine-tune your storage-as-a-service approach
    • June 16, 2025
  • 8
    Advanced audio dialog and generation with Gemini 2.5
    • June 15, 2025
  • 9
    A Father’s Day Gift for Every Pop and Papa
    • June 13, 2025
  • 10
    Global cloud spending might be booming, but AWS is trailing Microsoft and Google
    • June 13, 2025
about
Hello World!

We are aster.cloud. We’re created by programmers for programmers.

Our site aims to provide guides, programming tips, reviews, and interesting materials for tech people and those who want to learn in general.

We would like to hear from you.

If you have any feedback, enquiries, or sponsorship request, kindly reach out to us at:

[email protected]
Most Popular
  • Google Cloud, Cloudflare struck by widespread outages
    • June 12, 2025
  • What is PC as a service (PCaaS)?
    • June 12, 2025
  • 3
    Crayon targets mid-market gains with expanded Google Cloud partnership
    • June 10, 2025
  • By the numbers: Use AI to fill the IT skills gap
    • June 11, 2025
  • 5
    Apple services deliver powerful features and intelligent updates to users this autumn
    • June 11, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.