0
0
Dockerdevops~5 mins

Grafana dashboards for containers in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Grafana helps you see how your containers are doing by showing graphs and stats in one place. It solves the problem of checking many containers by hand and guessing their health.
When you want to watch CPU and memory use of your containers in real time.
When you need to find out why a container is slow or crashing.
When you want to share container performance reports with your team.
When you want to compare resource use between different containers.
When you want alerts if a container stops or uses too much memory.
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  prometheus:
    image: prom/prometheus:latest
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
    ports:
      - "9090:9090"
  grafana:
    image: grafana/grafana:9.5.2
    ports:
      - "3000:3000"
    depends_on:
      - prometheus
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=admin
    volumes:
      - grafana-storage:/var/lib/grafana
volumes:
  grafana-storage: {}

This file sets up two services: Prometheus and Grafana.

Prometheus collects container metrics and listens on port 9090.

Grafana runs on port 3000 and connects to Prometheus to get data.

We set an admin password for Grafana and save its data in a volume.

Commands
This command starts Prometheus and Grafana containers in the background so they run continuously.
Terminal
docker-compose up -d
Expected OutputExpected
Creating network "default" with the default driver Creating volume "grafana-storage" with default driver Creating prometheus ... done Creating grafana ... done
-d - Run containers in detached mode (in background)
This command lists running containers to confirm Prometheus and Grafana are up.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES abcd1234efgh prom/prometheus:latest "/bin/prometheus --config.file=/etc/prometheus/prometheus.yml" 10 seconds ago Up 9 seconds 0.0.0.0:9090->9090/tcp default_prometheus_1 ijkl5678mnop grafana/grafana:9.5.2 "/run.sh" 10 seconds ago Up 9 seconds 0.0.0.0:3000->3000/tcp default_grafana_1
This command checks if Prometheus is exposing metrics by requesting its metrics endpoint.
Terminal
curl http://localhost:9090/metrics
Expected OutputExpected
# HELP go_gc_duration_seconds A summary of the GC invocation durations. # TYPE go_gc_duration_seconds summary ...
This reminds you how to open Grafana in your browser and log in to start creating dashboards.
Terminal
echo "Access Grafana at http://localhost:3000 with user 'admin' and password 'admin'"
Expected OutputExpected
Access Grafana at http://localhost:3000 with user 'admin' and password 'admin'
Key Concept

If you remember nothing else from this pattern, remember: Grafana visualizes container metrics collected by Prometheus to help you monitor container health easily.

Common Mistakes
Not mapping ports in docker-compose.yml
Without port mapping, you cannot access Grafana or Prometheus from your browser or tools.
Always map container ports to host ports like '3000:3000' for Grafana and '9090:9090' for Prometheus.
Starting Grafana before Prometheus is ready
Grafana depends on Prometheus data source; if Prometheus is not running, Grafana dashboards won't show data.
Use 'depends_on' in docker-compose.yml to start Prometheus before Grafana.
Not setting admin password for Grafana
Grafana requires a password to log in; without setting it, you may face login issues or security risks.
Set 'GF_SECURITY_ADMIN_PASSWORD' environment variable in docker-compose.yml.
Summary
Use docker-compose.yml to run Prometheus and Grafana containers together.
Start containers with 'docker-compose up -d' and verify with 'docker ps'.
Access Grafana on port 3000 to create dashboards using Prometheus metrics.