Skip to content

jayan/docker-fastapi-test

 
 

Repository files navigation

docker-fastapi-test

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-test

Dockerized 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.yml
version: '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 jenkinsfile
pipeline {
    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'
            }
        }
    }
}

upload it into the git hub using these commands

  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.

6114142690867264183

6114142690867264183

These is my Fast api application

   http://13.232.27.191:8008/docs

6114142690867264184

monitoring tools Prometheus and Grafana

   http://13.232.27.191:9090/
   http://13.232.27.191:3000/

6114142690867264182

6114142690867264185

#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

6114142690867264188 (1)

After destrying the containers i recreated the containers these is the output

6114142690867264189

Previous data was still existed

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 47.2%
  • HCL 27.4%
  • Shell 25.4%