Run Prometheus using your own docker image and integrate it with Grafana

Anju
2 min readApr 4, 2021

--

  1. Create Dockefile
FROM debian:stable-slimWORKDIR /etc/prometheusRUN set -ex && apt-get update -y \
&& apt-get install wget -y
RUN wget "https://github.com/prometheus/prometheus/releases/download/v2.26.0/prometheus-2.26.0.linux-amd64.tar.gz"RUN tar -xzf prometheus-2.26.0.linux-amd64.tar.gz \
&& rm -rf prometheus-2.26.0.linux-amd64.tar.gz \
&& mv prometheus-2.26.0.linux-amd64/* /etc/prometheus \
&& rm -rf prometheus-2.26.0.linux-amd64 \
&& mv /etc/prometheus/prometheus /usr/local/bin/
EXPOSE 9090COPY prometheus.yml .ENTRYPOINT ["/usr/local/bin/prometheus"]CMD ["--config.file=/etc/prometheus/prometheus.yml"]

2. Create prometheus.yml

global:
scrape_interval: 60s # By default, scrape targets every 15 seconds.
evaluation_interval: 120s # By default, scrape targets every 15 seconds.
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']

3. Build you image

docker build -t prometheus-dev:1 .

4. Push the image to docker hub repository

a. login to docker
b. create a repository if not already exist for your dockerized application "prometheus-dev"
c. Go to your terminal an run below commands:
docker login #login to your dockerhub account
docker tag prometheus-dev:1 <your-dockerhub-id>/prometheus-dev:1
docker push <your-dockerhub-id>/prometheus-dev:1

5. Create a docker-compose.yml file

services:
prometheus:
image: prometheus-dev:1
container_name: monitoring_prometheus
restart: unless-stopped
ports:
- 9090:9090
grafana:
image: grafana/grafana
container_name: grafana
restart: unless-stopped
volumes:
- /opt/data/grafana:/var/lib/grafana
ports:
- 3000:3000

6. Using docker-compose.yml file

#To Run the docker-compose.yml file
docker-compose up -d
#To Bring down the compose file
docker-compose down

7. Check the prometheus and grafana status :

8. Check the webui for both the applications on the respected ports.

9. Go to Grafana Web UI => Configuration => data sources => Add data source => select Proemetheus

10. Enter your prometheus service url (service is mentioned in the docker-compose file) and create dashboard:

Notes:

1. You can optimize your image size using very light base image. 
2. Layering will help you to cache intermediate images which will help to run the image fast.
3. Remove unwanted files and packages
4. For security, specify a user do not run it using root.
5. Use multistaging

--

--

Anju
Anju

Written by Anju

A DevOps engineer who loves automating everything (almost), exploring new places, and finding peace in nature. Always looking for the next adventure!

No responses yet