Application:- clone the below repositry
Repo URL:- https://github.com/jayan/docker-fastapi-test
To clone the these use
git clone https://github.com/jayan/docker-fastapi-testDockerized the application
nano dockerfile# Dockerfile
# pull the official docker image
FROM python:3.11.1-slim
# set work directory
WORKDIR /app
# set env variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install dependencies
COPY requirements.txt .
RUN pip install -r requirements.txt
# copy project
COPY . .create docker-compose.yml file
nano docker-compose.ymlversion: '3.8'
services:
web:
build: .
command: uvicorn app.main:app --host 0.0.0.0
volumes:
- .:/app
ports:
- 8008:8000
prometheus:
image: prom/prometheus
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
ports:
- "9090:9090"
grafana:
image: grafana/grafana
environment:
GF_SECURITY_ADMIN_USER: admin
GF_SECURITY_ADMIN_PASSWORD: admin
ports:
- "3000:3000"
volumes:
- grafana-data:/var/lib/grafana
depends_on:
- prometheus
volumes:
grafana-data:create prometheus.yml
nano prometheus.yml global:
scrape_interval: 15s
scrape_configs:
- job_name: "api"
metrics_path: "/metrics"
static_configs:
- targets: ["13.232.27.191:8008"]
create a jenkins file
nano jenkinsfilepipeline {
agent any
environment {
GIT_REPO = 'https://github.com/jayan/docker-fastapi-test.git'
}
stages {
stage('Checkout') {
steps {
// Checkout the code from GitHub
checkout([
$class: 'GitSCM',
branches: [[name: '*/main']],
userRemoteConfigs: [[url: "${env.GIT_REPO}"]]
])
}
}
stage('Deploy') {
steps {
// Run docker compose up --build -d
sh 'docker compose up --build -d'
}
}
}
} git add .
git commit -m "commit"
git push origin main#install jenkins and docker using jenkins.sh and docker.sh
After configuring, create a job to run the Jenkinsfile in Jenkins and check whether the FastAPI application is up and running.
These is my Fast api application
http://13.232.27.191:8008/docsmonitoring tools Prometheus and Grafana
http://13.232.27.191:9090/
http://13.232.27.191:3000/#Once the application runs successfully, make sure to destroy containers and recreate another one and check if previous data is still present.
These is the data Before destroying the previous containers
After destrying the containers i recreated the containers these is the output
Previous data was still existed






