From ab51af6e19ac9a87d54f66b7775e14f1cd36c683 Mon Sep 17 00:00:00 2001 From: Tanmay Ambekar Date: Sun, 28 Jul 2024 14:09:43 +0530 Subject: [PATCH 1/8] Initial commit --- Dockerfile | 17 +++++++++++++++++ docker-compose.yml | 0 2 files changed, 17 insertions(+) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b0bc8df --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +# Use an official Python runtime as a parent image +FROM python:3.9 + +# Set the working directory in the container +WORKDIR /usr/src/app + +# Copy requirements.txt into the container at /usr/src/app +COPY requirements.txt ./ + +# Install dependencies listed in requirements.txt +RUN pip install --no-cache-dir --no-deps -r requirements.txt + +# Copy the rest of the application code into the container at /usr/src/app +COPY . . + +# Command to run the app +CMD ["python", "./your-app-script.py"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e69de29 From 1967001fc29726f9715044a5d01b7c0331883dca Mon Sep 17 00:00:00 2001 From: Tanmay Ambekar Date: Sun, 28 Jul 2024 14:26:38 +0530 Subject: [PATCH 2/8] Initial commit --- Dockerfile | 2 +- docker-compose.yml | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index b0bc8df..dee550c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,5 +13,5 @@ RUN pip install --no-cache-dir --no-deps -r requirements.txt # Copy the rest of the application code into the container at /usr/src/app COPY . . -# Command to run the app +# Command to run the ap CMD ["python", "./your-app-script.py"] diff --git a/docker-compose.yml b/docker-compose.yml index e69de29..4db5668 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +version: '3.12' + +services: + api: + build: . + volumes: + - ./:/usr/src/application:ro + command: uvicorn aop.main:app --host 0.0.0.0 --port 8080 --reload + ports: + - "8000:8000" + \ No newline at end of file From dd8d74b5e33cff929f69832bb6bbbf469c59b618 Mon Sep 17 00:00:00 2001 From: Tanmay Ambekar Date: Sun, 28 Jul 2024 14:26:47 +0530 Subject: [PATCH 3/8] Initial commit --- docker-compose.yml | 2 +- jenkinsfile | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 jenkinsfile diff --git a/docker-compose.yml b/docker-compose.yml index 4db5668..d7b1a96 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,5 +7,5 @@ services: - ./:/usr/src/application:ro command: uvicorn aop.main:app --host 0.0.0.0 --port 8080 --reload ports: - - "8000:8000" + - "8000:8000" \ No newline at end of file diff --git a/jenkinsfile b/jenkinsfile new file mode 100644 index 0000000..e7c6517 --- /dev/null +++ b/jenkinsfile @@ -0,0 +1,26 @@ +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' + } + } + } +} \ No newline at end of file From d30ad379c0fef0b0f46b07b7d7aa8cd5b742be8d Mon Sep 17 00:00:00 2001 From: TANMAY ABHAYRAO AMBEKAR <107787552+tanmayambekar123@users.noreply.github.com> Date: Sun, 28 Jul 2024 14:30:13 +0530 Subject: [PATCH 4/8] Delete jenkinsfile --- jenkinsfile | 26 -------------------------- 1 file changed, 26 deletions(-) delete mode 100644 jenkinsfile diff --git a/jenkinsfile b/jenkinsfile deleted file mode 100644 index e7c6517..0000000 --- a/jenkinsfile +++ /dev/null @@ -1,26 +0,0 @@ -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' - } - } - } -} \ No newline at end of file From 3160635182fdaa973a62c45076443627f9450268 Mon Sep 17 00:00:00 2001 From: Tanmay Ambekar Date: Sun, 28 Jul 2024 14:31:24 +0530 Subject: [PATCH 5/8] Initial commit --- jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jenkinsfile b/jenkinsfile index e7c6517..e2b6138 100644 --- a/jenkinsfile +++ b/jenkinsfile @@ -2,7 +2,7 @@ pipeline { agent any environment { - GIT_REPO = 'https://github.com/jayan/docker-fastapi-test.git' + GIT_REPO = 'https://github.com/tanmayambekar123/docker-fastapi-test.git' } stages { From cf44ee0c2e137c8e2ebaa5bec284cef63eeab539 Mon Sep 17 00:00:00 2001 From: TANMAY ABHAYRAO AMBEKAR <107787552+tanmayambekar123@users.noreply.github.com> Date: Sun, 28 Jul 2024 14:45:48 +0530 Subject: [PATCH 6/8] Delete Terraform.tf --- Terraform.tf | 68 ---------------------------------------------------- 1 file changed, 68 deletions(-) delete mode 100644 Terraform.tf diff --git a/Terraform.tf b/Terraform.tf deleted file mode 100644 index a2e7fb7..0000000 --- a/Terraform.tf +++ /dev/null @@ -1,68 +0,0 @@ -#provider -provider "aws" { - region = "us-east-1" # Change to your desired region -} - -# security group -resource "aws_security_group" "web_sg" { - name_prefix = "web-sg-" - - ingress { - from_port = 3000 - to_port = 3000 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - ingress { - from_port = 9090 - to_port = 9090 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - ingress { - from_port = 8000 - to_port = 8000 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - ingress { - from_port = 8008 - to_port = 8008 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - ingress { - from_port = 8080 - to_port = 8080 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - egress { - from_port = 0 - to_port = 0 - protocol = "-1" - cidr_blocks = ["0.0.0.0/0"] - } - - tags = { - Name = "web-sg" - } -} - -#EC2 instance -resource "aws_instance" "web_server" { - ami = "ami-0996381efe4d88b5b" - instance_type = "t2.micro" - key_name = "webserver" - - security_groups = [aws_security_group.web_sg.name] - - tags = { - Name = "web-server-instance" - } -} \ No newline at end of file From 77c5c6e86986e984c6b110e598f043ca7a418bda Mon Sep 17 00:00:00 2001 From: Tanmay Ambekar Date: Sun, 28 Jul 2024 14:50:13 +0530 Subject: [PATCH 7/8] Initial commit --- Terraform.tf | 68 ---------------------------------------------- docker-compose.yml | 30 ++++++++++++++++---- 2 files changed, 24 insertions(+), 74 deletions(-) delete mode 100644 Terraform.tf diff --git a/Terraform.tf b/Terraform.tf deleted file mode 100644 index a2e7fb7..0000000 --- a/Terraform.tf +++ /dev/null @@ -1,68 +0,0 @@ -#provider -provider "aws" { - region = "us-east-1" # Change to your desired region -} - -# security group -resource "aws_security_group" "web_sg" { - name_prefix = "web-sg-" - - ingress { - from_port = 3000 - to_port = 3000 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - ingress { - from_port = 9090 - to_port = 9090 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - ingress { - from_port = 8000 - to_port = 8000 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - ingress { - from_port = 8008 - to_port = 8008 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - ingress { - from_port = 8080 - to_port = 8080 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - egress { - from_port = 0 - to_port = 0 - protocol = "-1" - cidr_blocks = ["0.0.0.0/0"] - } - - tags = { - Name = "web-sg" - } -} - -#EC2 instance -resource "aws_instance" "web_server" { - ami = "ami-0996381efe4d88b5b" - instance_type = "t2.micro" - key_name = "webserver" - - security_groups = [aws_security_group.web_sg.name] - - tags = { - Name = "web-server-instance" - } -} \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index d7b1a96..52e7598 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,11 +1,29 @@ -version: '3.12' +version: '3.8' services: - api: + fastapi-app: build: . volumes: - - ./:/usr/src/application:ro - command: uvicorn aop.main:app --host 0.0.0.0 --port 8080 --reload + - ./app:/app ports: - - "8000:8000" - \ No newline at end of file + - "8000:8000" + command: uvicorn main:app --host 0.0.0.0 --port 8000 --reload + + prometheus: + image: prom/prometheus + volumes: + - ./prometheus:/etc/prometheus/ + ports: + - "9090:9090" + + grafana: + image: grafana/grafana + ports: + - "3000:3000" + volumes: + - grafana-storage:/var/lib/grafana + depends_on: + - prometheus + +volumes: + grafana-storage: From 52b23eca6d38d0e98bacf5d60f3a6325465f3cc7 Mon Sep 17 00:00:00 2001 From: Tanmay Ambekar Date: Sun, 28 Jul 2024 14:54:54 +0530 Subject: [PATCH 8/8] Initial commit --- README.md | 218 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 217 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4b7a613..5b3e040 100644 --- a/README.md +++ b/README.md @@ -1 +1,217 @@ -# docker-fastapi-test \ No newline at end of file +# docker-fastapi-test DevOps Machine Test + +# docker-fastapi-test- Devops-Machine Test + +### Step 1:Cloning a Repository in Visual Studio + + +1. Launch VS Code:- Start by opening Visual Studio on your computer. + + +2. Access the Clone Dialog:- +From the start window, select "Clone a repository." +Alternatively, if you have a project open, you can find this option under File > Clone Repository.... + + +3. Enter Repository Details:- +In the "Repository location" field, paste the URL of the repository you wish to clone. This URL can typically be found on the repository page in your version control system (e.g., GitHub, GitLab, Azure Repos). + + + git clone https://github.com/RohitPatil18/docker-fastapi-test + + +4. Select a Local Path:- +Specify the local directory where you want to clone the repository. This is where the repository will be downloaded and stored on your local machine. + + +5. Initiate the Clone:- +Click the "Clone" button to begin the cloning process. Visual Studio will then download the repository to your specified local path. + + + +### Step 2:- Setting Up a Virtual Environment on Windows + + +Install Python then check the version of python + +1. Create the Virtual Environment:- + +python -m venv env + +2. Activate the Virtual Environment:- + +.\env\Scripts\activate + +Step 3:- Create Dockerfile + +1. Create Dockerfile in the docker-fastapi-test + +2. Choose a Base Image + +* Use an official Python runtime as a parent image * + +FROM python:3.9 + +* Set the working directory in the container * + +WORKDIR /usr/src/app + +* Copy requirements.txt into the container at /usr/src/app * + +COPY requirements.txt ./ + +* Install dependencies listed in requirements.txt * + +RUN pip install --no-cache-dir --no-deps -r requirements.txt + +* Copy the rest of the application code into the container at /usr/src/app * +COPY . . + +* Command to run the app * +CMD ["python", "./your-app-script.py"] + + + + +### Step 3: Create a requirements.txt file + + +pip install -r requirements.txt + +pip freeze > .\requirements.txt + + +# You need to install additional commands to fulfill the requirements. + + + + +pip install fastapi + +pip install fastapi-slim + +pip uninstall fastapi-slim starlette + +pip install fastapi==0.92.0 + +pip install fastapi + +pip install fastapi-slim --upgrade + +pip install uvicorn + +pip install python-dotenv + + + + + +### Step 4: Create a docker-compose.yml file + + +# create a docker-compose.yml file in the root of your repository. + +version: '3.12' + +services: + api: + build: . + volumes: + - ./:/usr/src/application:ro + command: uvicorn aop.main:app --host 0.0.0.0 --port 8080 --reload + ports: + - "8000:8000" + + +# Commonds is used to build yml file + +docker-compose up -d --build + +docker-compose build --no-cache + + + +### Step 5: Structure Your Project +. +├── Dockerfile +├── docker-compose.yml +├── requirements.txt +├── main.py +└── data + + +#### +### Step 6: Build and Run the Docker Container +### + +1. **docker build --help** + Displays help information for the `docker build` command, including usage and options. + +2. **docker build -t app** + Builds a Docker image from the Dockerfile in the current directory and tags it as `app`. + +3. **$env:BUILDX_EXPERIMENTAL=1** + Sets an environment variable to enable Docker Buildx experimental features. + +4. **docker buildx build -t app .** + Builds a Docker image using Buildx (a Docker CLI plugin) with the tag `app` from the Dockerfile in the current directory. + +5. **Get-ChildItem** + Lists files and directories in the current directory in PowerShell. + +7. **docker image ls** + Lists all Docker images available on your system. + +8. **docker run -d -p 8000:8000** + Runs a Docker container in detached mode and maps port 8000 of the container to port 8000 on the host. + +9. **docker run -d -p 8000:8000 docker/getting-started** + Runs a Docker container in detached mode from the `docker/getting-started` image and maps port 8000 of the container to port 8000 on the host. + +10. **docker ps** + Lists all currently running Docker containers. + +11. **docker stop interesting_lederberg** + Stops the Docker container with the name `interesting_lederberg` (repeated command). + +12. **netstat -ano | findstr :8000** + Checks which process is using port 8000 on the host system. + +13. **docker run -d -p 8001:8000 docker/getting-started** + Runs a Docker container in detached mode from the `docker/getting-started` image and maps port 8000 of the container to port 8001 on the host. + +14. **docker-compose down** + Stops and removes all containers, networks, and volumes defined in `docker-compose.yml`. + + +Your FastAPI application should now be running and accessible at http://127.0.0.1:8000/ + + +### +### Step 7: Destroy and Recreate Containers +### + +1. Stop and Remove Containers: + +docker-compose down + +2. Remove Volumes (Optional) + +docker-compose down -v + +3. Rebuild and Start Containers + +docker-compose up --build -d + +4. Verify the Containers + +docker-compose ps + +5. Stops the Docker container with the name `interesting_lederberg`. + +docker stop interesting_lederberg + +6. Starts the services defined in `docker-compose.yml` in detached mode. + +docker-compose up -d +