Setup Grafana and Prometheus in Docker

Setup Grafana and Prometheus in Docker

Grafana and Prometheus are great tools for monitoring your servers and alerting you of any issues. This is how you set it all up in Docker which is a nice and easy way of managing them.

If you haven't already done so, start by installing Docker. Instructions here.

Once done, create a file called docker-compose.yaml

Paste the following into this file...

version: "3.6"
services:
  grafana:
    environment:
      - "PATH=/usr/share/grafana/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
      - "GF_PATHS_CONFIG=/etc/grafana/grafana.ini"
      - "GF_PATHS_DATA=/var/lib/grafana"
      - "GF_PATHS_HOME=/usr/share/grafana"
      - "GF_PATHS_LOGS=/var/log/grafana"
      - "GF_PATHS_PLUGINS=/var/lib/grafana/plugins"
      - "GF_PATHS_PROVISIONING=/etc/grafana/provisioning"
    image: "grafana/grafana-oss:latest"
    network_mode: "bridge"
    ports:
      - "80:3000/tcp"
    restart: "unless-stopped"
    user: "472"
    volumes:
      - "/docker/grafana/lib:/var/lib/grafana"
    working_dir: "/usr/share/grafana"
  prometheus:
    command:
      - "--config.file=/etc/prometheus/prometheus.yml"
      - "--storage.tsdb.path=/prometheus"
      - "--web.console.libraries=/usr/share/prometheus/console_libraries"
      - "--web.console.templates=/usr/share/prometheus/consoles"
    image: "prom/prometheus:latest"
    network_mode: "bridge"
    ports:
      - "9090:9090/tcp"
    restart: "unless-stopped"
    user: "nobody"
    volumes:
      - "/docker/prometheus:/etc/prometheus"
    working_dir: "/prometheus"

Once done, save and exit the file then enter the following command to start the containers...

docker-compose up -d

If all goes well, Grafana will now be accessible by entering the IP address of your server into your web browser at port 80 and Prometheus on port 9090.

The first thing you'll want to do is link Grafana and Prometheus together which you can do by clicking on the menu button in the top-left corner, going to Connections, then add Prometheus. The Prometheus server URL will be http://localhost:9090

Then you can start adding your servers into the Prometheus configuration once you've some exporters installed and running. This configuration file will be located on your host system at /docker/prometheus/prometheus.yml - the easiest way to add additional servers is to duplicate the line that starts - targets: [ipaddress:9100'] then swap the IP address.

Each time you change this configuration file, you'll need to run the following command to restart the stack to reload your changes...

docker-compose up -d --force-recreate