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:
Optional: .dockerignore
Create a .dockerignore
file to avoid copying unnecessary files into the contain
Build and Run All Services
Access Grafana
-
Grafana UI: http://localhost:3000
-
Default login:
admin / admin
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:
You should see something like:
If you’re not in the docker
group, add yourself:
Then log out and back in, or run: