Prometheus Monitoring: Install using Docker – Ubuntu, CentOS

What is Prometheus? Prometheus is an open-source monitoring and alerting software written in Go.

It collects metrics from configured targets and stores them in a local time series database.

Prometheus can run rules over collected data to either aggregate and record new time series from existing data or generate alerts.

To visualize the collected data Prometheus can be integrated with Grafana.

In the following article i will show how to install Prometheus using Docker on Ubuntu and CentOS.

Install Docker

To run Prometheus as a Docker container, it needs to install docker and docker-compose:

  • How to install Docker on Ubuntu-16.04
  • How to install Docker on Ubuntu-18.04
  • How to install Docker on CentOS-7
  • How to install Docker Compose on Ubuntu, CentOS or MacOS
  • Install Prometheus Monitoring Using Docker

    Create directories to store Prometheus data and configuration files:

    $ sudo mkdir -p /opt/prometheus/{conf,data}
    

    Change the ownership of the /opt/prometheus/data directory:

    $ sudo chown 65534:65534 /opt/prometheus/data
    

    If you don’t change the ownership you may get the following error:

    msg=”Opening storage failed” err=”open DB in /prometheus: open /prometheus/123456789: permission denied”

    Create a minimal Prometheus configuration file /opt/prometheus/conf/prometheus.yml:

    scrape_configs:
      - job_name: 'prometheus'
        static_configs:
          - targets: ['$PROMETHEUS_IP:9090']
    

    More details about Prometheus configuration options can be found here.

    Create a Docker Compose file /opt/prometheus/docker-compose.yml:

    version: "3"
    
    services:
      prometheus:
        image: prom/prometheus:latest
        container_name: prometheus
        volumes:
          - /opt/prometheus/conf:/etc/prometheus
          - /opt/prometheus/data:/prometheus
        command:
          - '--config.file=/etc/prometheus/prometheus.yml'
          - '--storage.tsdb.path=/prometheus'
        ports:
          - "9090:9090"
    

    Create a systemd service file /etc/systemd/system/prometheus.service:

    [Unit]
    Description=Prometheus monitoring docker container
    After=docker.service
    BindsTo=docker.service
    
    [Service]
    Restart=always
    WorkingDirectory=/opt/prometheus/
    # Ubuntu
    ExecStart=/usr/local/bin/docker-compose up
    ExecStop=/usr/local/bin/docker-compose down
    # CentOS
    #ExecStart=/usr/bin/docker-compose up
    #ExecStop=/usr/bin/docker-compose down
    
    [Install]
    WantedBy=multi-user.target
    

    Reload systemd configuration and configure prometheus to auto-start on system boot:

    $ sudo systemctl daemon-reload
    $ sudo systemctl enable prometheus
    

    Start the prometheus service:

    $ sudo systemctl start prometheus
    

    Prometheus Monitoring Web Interface

    Prometheus provides a basic web interface to monitor status of itself and its exporters, execute queries and generate graphs: http://$PROMETHEUS_IP:9090

    At the moment our Prometheus is configured to collect its own metrics only.

    In the next articles i will show how to make Prometheus collect metrics from the other targets using node_exporter.

    As you can see, the Prometheus monitoring web interface is too simple and for anything more complex than testing and debugging it is recommended to use Grafana.