Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Docker Integration with Prometheus

Introduction

Docker is a platform that allows developers to automate the deployment of applications inside lightweight, portable containers. Prometheus is an open-source monitoring and alerting toolkit designed for reliability and scalability. Integrating Docker with Prometheus allows you to monitor your containerized applications easily and effectively.

Prerequisites

Before diving into Docker integration with Prometheus, ensure that you have the following tools installed:

  • Docker: Installed on your local machine or server.
  • Docker Compose: For managing multi-container Docker applications.
  • Prometheus: Either running locally or within a Docker container.

Setting Up Prometheus in Docker

To begin, we will create a Docker container that runs Prometheus. First, create a directory for your Prometheus configuration.

mkdir prometheus && cd prometheus

Next, create a configuration file named prometheus.yml with the following content:

nano prometheus.yml
global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'docker'
    static_configs:
      - targets: ['host.docker.internal:9090']
                

This configuration specifies that Prometheus will scrape metrics from Docker containers running on the host at port 9090. Now, let's run the Prometheus container.

docker run -d --name=prometheus -p 9090:9090 -v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

The above command runs Prometheus in a detached mode, mapping port 9090 of the container to port 9090 on the host.

Running a Sample Application

To test the Prometheus integration, we need a sample application that exposes metrics. We will use a simple Node.js application for this purpose.

mkdir app && cd app

Create a Dockerfile with the following content:

nano Dockerfile
FROM node:14

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["node", "server.js"]
                

Next, create a server.js file that exposes metrics:

nano server.js
const express = require('express');
const client = require('prom-client');

const app = express();
const port = 3000;

const register = new client.Registry();
const httpRequestDurationMicroseconds = new client.Histogram({
    name: 'http_request_duration_seconds',
    help: 'Duration of HTTP requests in seconds',
    labelNames: ['method', 'route'],
    registers: [register],
});

app.get('/', (req, res) => {
    res.send('Hello World!');
});

app.get('/metrics', (req, res) => {
    res.set('Content-Type', register.contentType);
    res.end(register.metrics());
});

app.listen(port, () => {
    console.log(`App listening at http://localhost:${port}`);
});
                

Build and run the Docker container for the application:

docker build -t myapp .
docker run -d --name=myapp -p 3000:3000 myapp

Configuring Prometheus to Scrape the Application

Now that we have our application running, we need to update the prometheus.yml file to scrape metrics from the application. Edit the file to include:

nano prometheus.yml
global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'docker'
    static_configs:
      - targets: ['host.docker.internal:9090']

  - job_name: 'myapp'
    static_configs:
      - targets: ['host.docker.internal:3000']
                

After updating the configuration, restart the Prometheus container:

docker restart prometheus

Accessing Prometheus Dashboard

You can access the Prometheus dashboard by navigating to http://localhost:9090 in your web browser. From here, you can explore the metrics collected from your application.

Conclusion

In this tutorial, you learned how to integrate Docker with Prometheus to monitor containerized applications. This setup provides a powerful way to collect and visualize metrics, enabling better insights into application performance. You can extend this integration by adding more applications and adjusting the Prometheus configuration to suit your needs.