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.

    4 Replies to “Prometheus Monitoring: Install using Docker – Ubuntu, CentOS”

    1. great step by step guide! Managed to start my prometheus docker using your guide

    2. How do you add basic authentication with this implementation? I don’t want the outside world to access this port

    3. Hi guys, I had the following error with the service:
      ● prometheus.service – Prometheus monitoring docker container
      Loaded: loaded (/etc/systemd/system/prometheus.service; enabled; vendor preset: disabled)
      Active: failed (Result: start-limit) since mar 2020-01-21 11:29:54 -03; 5s ago
      Process: 16528 ExecStop=/usr/bin/docker-compose down (code=exited, status=203/EXEC)
      Process: 16526 ExecStart=/usr/bin/docker-compose up (code=exited, status=203/EXEC)
      Main PID: 16526 (code=exited, status=203/EXEC)

      ene 21 11:29:53 localhost systemd[1]: prometheus.service: control process exited, code=exited status=203
      ene 21 11:29:53 localhost systemd[1]: Unit prometheus.service entered failed state.
      ene 21 11:29:53 localhost systemd[1]: prometheus.service failed.
      ene 21 11:29:54 localhost systemd[1]: prometheus.service holdoff time over, scheduling restart.
      ene 21 11:29:54 localhost systemd[1]: Stopped Prometheus monitoring docker container.
      ene 21 11:29:54 localhost systemd[1]: start request repeated too quickly for prometheus.service
      ene 21 11:29:54 localhost systemd[1]: Failed to start Prometheus monitoring docker container.
      ene 21 11:29:54 localhost systemd[1]: Unit prometheus.service entered failed state.
      ene 21 11:29:54 localhost systemd[1]: prometheus.service failed.
      A few days ago it worked perfect and now I can’t start prometheus. Any ideas to fix it?

    4. Hi,
      I followed this blog to create prometheus container but the container keeps failing.
      I have also changed the onwnership of folder /opt/prometheus/data as mentioned in this blog, but it still fails. Please help me on this.

      Docker logs shows below error :
      level=error ts=2020-05-19T12:50:45.304Z caller=query_logger.go:87 component=activeQueryTracker msg=”Error opening query log file” file=/prometheus/queries.active err=”open /prometheus/queries.active: permission denied”
      panic: Unable to create mmap-ed active query log

      goroutine 1 [running]:
      github.com/prometheus/prometheus/promql.NewActiveQueryTracker(0x7ffdd23e9ec2, 0xb, 0x14, 0x2e940a0, 0xc0006fc870, 0x2e940a0)
      /app/promql/query_logger.go:117 +0x4cd
      main.main()
      /app/cmd/prometheus/main.go:368 +0x4ed8

    Leave a Reply