0

i'm setting up Promtheus & Grafana of my local Ubuntu machine as docker containers ,

my steps were :

  1. running prometheus : docker run -t -d -p 9090:9090 prom/prometheus
  2. running Grafana : docker run -t -d --name grafana -p 3000:3000 grafana/grafana

as you can see prometheus run on the mapped 9090 port , same for grafana running on 3000

Now when configuring grafana dashborad for prometheus in grafana , i need to indicate the url of prometheus :

-> since both of them are running on local containers.

What address ton give to grafana to make it point on prometheus ?

Raj Paliwal
  • 943
  • 1
  • 9
  • 22
firasKoubaa
  • 6,439
  • 25
  • 79
  • 148
  • 1
    It would be better to run them in docker-compose so they're linked automatically to each other. – tkausl Nov 26 '19 at 11:15
  • you can login to grafana and add prometheus as a datasource – ankidaemon Nov 26 '19 at 11:17
  • @ankidaemon and what address to give for it ? , th sthe question :) – firasKoubaa Nov 26 '19 at 11:18
  • @firasKoubaa that you can get with this command docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' – ankidaemon Nov 26 '19 at 11:19
  • 1
    @ankidaemon thans a lot that seems working : you would post it as an answer – firasKoubaa Nov 26 '19 at 11:28
  • You need to create a Docker network, then run both containers with `--net` and that network name, and then they can reach each other using their `--name` as hostnames. [This answer](https://stackoverflow.com/a/35141454/10008173) has a walkthrough (the other answers in that question are for very old versions of Docker). The Compose suggestion is good in that Compose does this work for you automatically. – David Maze Nov 26 '19 at 11:59

3 Answers3

1

For an easy setup, you can use docker-compose as commented. An example of docker-compose.yaml file with prometheus and grafana:

docker-compose.yaml

version: "3"
services:
  prometheus:
    image: prom/prometheus:latest
    volumes:
      - ./prometheus.yaml:/etc/prometheus/prometheus.yml
    ports:
      - 9090:9090
  grafana:
    image: grafana/grafana:latest
    volumes:
      - ./grafana-storage:/var/lib/grafana
      - ./grafana/config.ini:/etc/grafana/config.ini
      - ./grafana/provisioning:/etc/grafana/provisioning
      - ./grafana/dashboards:/var/lib/grafana/dashboards
    ports:
      - 3001:3000

prometheus.yaml

# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
      monitor: 'codelab-monitor'

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first.rules"
  # - "second.rules"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'your-app'
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    static_configs:
      - targets: ['your-app:3000']

config.ini

[paths]
provisioning = /etc/grafana/provisioning

[server]
enable_gzip = true

[users]
default_theme = light
wolmi
  • 1,659
  • 12
  • 25
1

Your Grafana container will not be able to contact (discover) your Prometheus container because when docker starts each container, it creates a virtual interface on host system with unique name like vethef766ac, and isolates the containers.

If you don't want to use docker compose AND if you want to access your Grafana container using its host IP, you have to run your Grafana container in the host network using the --network option.

You can then run Grafana as so:

docker run -t -d --name grafana --network="host" grafana/grafana

Note: --network="host" gives the container full access to local system services such as D-bus and is therefore considered insecure.

The URL you will want to specify in Grafana would be http://localhost:9090.

Amine Zaine
  • 165
  • 1
  • 21
  • Error response from daemon: conflicting options: port publishing and the container type network mode – firasKoubaa Nov 26 '19 at 16:35
  • @firasKoubaa are you sure you're not running other containers that are publishing the same port(s)? – Amine Zaine Nov 26 '19 at 16:39
  • Ok, according to the documentation, publishing ports only works with the default network (bridge). The problem is if you don't publish your port, you won't be able to access your Grafana using your host's IP. If you want that, you will want to use the host network instead of the Prometheus container's network. `--network="host"` and get rid of `-p` option. NOTE: `--network="host"` gives the container full access to local system services such as D-bus and is therefore considered insecure. I'll update the answer – Amine Zaine Nov 26 '19 at 16:50
  • the url http://:9090 is not valid i get this msg : HTTP Error Bad Gateway – firasKoubaa Nov 26 '19 at 17:12
  • Well, since you're publishing the port 9090, just access it using localhost:9090. It should work. – Amine Zaine Nov 26 '19 at 17:15
0

For a running docker container if needs to look for their address, following command can be helpful.

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container-name>

This will return an IP address associated.

Ankur Loriya
  • 3,276
  • 8
  • 31
  • 58
ankidaemon
  • 1,363
  • 14
  • 20
  • 3
    This really isn't a best practice. This IP address will change whenever you delete and recreate the containers. It's better to use Docker's internal DNS system. – David Maze Nov 26 '19 at 11:53
  • @DavidMaze good point. For dynamic better stick to compose/DNS – ankidaemon Nov 26 '19 at 12:09