diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..6201562 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,7 @@ +__pycache__/ +*.pyc +*.pyo +*.pyd +.git +.env + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..398c4fd --- /dev/null +++ b/Dockerfile @@ -0,0 +1,24 @@ +FROM python:3.11-slim + +# Prevents Python from buffering stdout/stderr and ensures imports work +ENV PYTHONUNBUFFERED=1 +ENV PYTHONPATH=/app + +# Set working directory +WORKDIR /app + +# Install dependencies +COPY requirements.txt /app/requirements.txt +RUN pip install --upgrade pip && pip install -r /app/requirements.txt + +# Copy application code +COPY ./app /app + +# Ensure data folder exists +RUN mkdir -p /app/data + +EXPOSE 8000 + +# ✅ Load main.py directly (not "app.main") +CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"] + diff --git a/README.md b/README.md index 4b7a613..4c87090 100644 --- a/README.md +++ b/README.md @@ -1 +1,142 @@ -# docker-fastapi-test \ No newline at end of file +# docker-fastapi-test +# Dockerized FastAPI App – Persistent Users JSON + +This project is a **FastAPI application** that demonstrates how to run a simple API inside a **Docker container** with **docker-compose**. +It uses a persistent `users.json` file (mounted as a volume) to store user data so that data is not lost even if the container restarts. + +--- + +## Project Structure +docker-fastapi-test/ +├── app/ +│ ├── main.py # FastAPI application code +│ ├── requirements.txt # Python dependencies +│ └── ... +├── data/ +│ └── users.json # Persistent user data (mounted volume) +├── Dockerfile # Docker image definition +├── docker-compose.yml # Compose file for running the container +└── .dockerignore # Files to ignore during build + + + +--- + +## Prerequisites + +- **Docker** installed → [Install Docker](https://docs.docker.com/get-docker/) +- **Docker Compose** installed → [Install Compose](https://docs.docker.com/compose/install/) + +--- +# Docker FastAPI Project on AWS EC2 + +This project demonstrates how to deploy a FastAPI application using Docker on an AWS EC2 instance. +It is a step-by-step guide from launching the EC2 instance to running the application. + +--- + +## Prerequisites + +Before starting, ensure you have: +- An **AWS account** +- Basic knowledge of: + - Linux commands + - Git + - Docker +- Installed locally (for development): + - [VS Code](https://code.visualstudio.com/) + - [Git](https://git-scm.com/) + +--- + +## Launch an EC2 Instance + +1. Log in to **AWS Management Console** → Go to **EC2**. +2. Click **Launch Instance**. +3. Configure: + - **Name:** `docker-fastapi-test` + - **AMI:** Ubuntu 22.04 LTS + - **Instance type:** t2.micro (Free Tier) + - **Key pair:** Create new key pair (or use existing). + - **Security group:** Allow: + - SSH (port 22) + - HTTP (port 80) + - Custom TCP (port 8000 for FastAPI) +4. Launch the instance. + +--- + +## Connect to the EC2 Instance + +```bash +ssh -i "your-key.pem" ubuntu@ +``` +***Install Required Packages*** + +Update system & install dependencies: +```bash +sudo apt update && sudo apt upgrade -y +sudo apt install git docker.io docker-compose -y +sudo systemctl enable docker +sudo systemctl start docker +``` +### Check Docker is running: +```bash + docker --version +``` +## Setup & Run + +1. **Clone the Repository** + ```bash + git clone https://github.com//docker-fastapi-test.git + cd docker-fastapi-test + +2 **Build & Start Containers** +```bash + docker-compose up -d --build +``` +3 **Check Running Containers** +```bash +docker ps +``` +***Access the API*** +```bash +Open browser: http://localhost:8000 + +Interactive API docs: http://localhost:8000/docs +``` +**Example Usage** +1 Get All Users +```bash +curl http://localhost:8000/users +``` +2 Add a New User +```bash +curl -X POST http://localhost:8000/users \ +-H "Content-Type: application/json" \ +-d '{"first_name": "Abhishek", "last_name": "Jirage", "age": 25}' +``` +**Stop & Remove Containers** +```bash +docker-compose down +``` +***Development Notes*** + +Persistent Data → The data/users.json file is mounted as a Docker volume. Your data will remain even after restarting the container + +***Rebuild Image (if you change code):*** +```bash +docker-compose up -d --build +``` +## Output +![](./screenshot/op1.png) +![](./screenshot/op2.png) +![](./screenshot/op3.png) +![](./screenshot/op4.png) +![](./screenshot/op5.png) +![](./screenshot/op6.png) +![](./screenshot/op7.png) +![](./screenshot/op8.png) +![](./screenshot/op9.png) +![](./screenshot/op10.png) +![](./screenshot/op11.png) \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..4c1f12d --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +version: "3.8" + +services: + web: + build: . + ports: + - "8000:8000" + volumes: + - ./data:/app/data + restart: unless-stopped + diff --git a/screenshot/op1.png b/screenshot/op1.png new file mode 100644 index 0000000..2238421 Binary files /dev/null and b/screenshot/op1.png differ diff --git a/screenshot/op10.png b/screenshot/op10.png new file mode 100644 index 0000000..45b1dd5 Binary files /dev/null and b/screenshot/op10.png differ diff --git a/screenshot/op11.png b/screenshot/op11.png new file mode 100644 index 0000000..a23d535 Binary files /dev/null and b/screenshot/op11.png differ diff --git a/screenshot/op2.png b/screenshot/op2.png new file mode 100644 index 0000000..e4b7e1e Binary files /dev/null and b/screenshot/op2.png differ diff --git a/screenshot/op3.png b/screenshot/op3.png new file mode 100644 index 0000000..9c7897a Binary files /dev/null and b/screenshot/op3.png differ diff --git a/screenshot/op4.png b/screenshot/op4.png new file mode 100644 index 0000000..49dc4ec Binary files /dev/null and b/screenshot/op4.png differ diff --git a/screenshot/op5.png b/screenshot/op5.png new file mode 100644 index 0000000..e9b8fc1 Binary files /dev/null and b/screenshot/op5.png differ diff --git a/screenshot/op6.png b/screenshot/op6.png new file mode 100644 index 0000000..82fb4c1 Binary files /dev/null and b/screenshot/op6.png differ diff --git a/screenshot/op7.png b/screenshot/op7.png new file mode 100644 index 0000000..5fe400c Binary files /dev/null and b/screenshot/op7.png differ diff --git a/screenshot/op8.png b/screenshot/op8.png new file mode 100644 index 0000000..4640f12 Binary files /dev/null and b/screenshot/op8.png differ diff --git a/screenshot/op9.png b/screenshot/op9.png new file mode 100644 index 0000000..de25854 Binary files /dev/null and b/screenshot/op9.png differ