Skip to content

Setup

Set Up Grafana + Prometheus (Docker)

docker-compose.yml

docker-compose.yml
version: "3"

services:
  prometheus:
    image: prom/prometheus
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
    ports:
      - "9090:9090"

  grafana:
    image: grafana/grafana
    ports:
      - "3000:3000"

  fastapi:
    build: .
    ports:
      - "8000:8000"

prometheus.yml

prometheus.yml
global:
  scrape_interval: 5s

scrape_configs:
  - job_name: "fastapi"
    static_configs:
      - targets: ["host.docker.internal:8000"]

Dockerfile

Solution: Create a Dockerfile for FastAPI

In your project root (same directory as docker-compose.yml), create a file named exactly Dockerfile (no extension) with the following contents:

# Dockerfile for FastAPI app

FROM python:3.12-slim

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

# Set working directory
WORKDIR /app

# Install pipenv
RUN pip install pipenv

# Copy Pipfile and install dependencies
COPY Pipfile Pipfile.lock ./
RUN pipenv install --system --deploy --ignore-pipfile

# Copy the application code
COPY . .

# Expose FastAPI port
EXPOSE 8000

# Start the FastAPI app
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

✅ Then Try Again

Once the Dockerfile is in place, run:

sudo docker-compose up --build

Optional: .dockerignore

Create a .dockerignore file to avoid copying unnecessary files into the contain

__pycache__/
*.pyc
*.pyo
*.pyd
.env
.git

Build and Run All Services

docker-compose up

# or

sudo docker-compose up

Access Grafana

Add Prometheus as a data source:

  • URL: http://prometheus:9090

Then create a dashboard using the metric request_count.


Check Docker Socket

See if the Docker socket exists:

ls -l /var/run/docker.sock

You should see something like:

srw-rw---- 1 root docker 0 Aug 2 12:34 /var/run/docker.sock

If you’re not in the docker group, add yourself:

sudo usermod -aG docker $USER

Then log out and back in, or run:

newgrp docker
Try Using Docker with sudo

If nothing else works:

sudo docker-compose up

If this works, it's definitely a permissions issue.