diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1792ff1 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM python:3.10-slim + +WORKDIR /app + +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +COPY . . + +EXPOSE 8000 + +CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"] + diff --git a/README.md b/README.md index 4b7a613..16c9612 100644 --- a/README.md +++ b/README.md @@ -1 +1,124 @@ -# docker-fastapi-test \ No newline at end of file + +# Dockerized FastAPI Application + +This project provides a solution for a machine test to dockerize a simple FastAPI application. The application exposes a few endpoints to interact with a JSON file, and the entire setup can be managed using Docker Compose. + +--- + +## 🚀 Project Overview + +The goal was to containerize a basic FastAPI application and ensure it can be run and managed with `docker-compose`. A key requirement was to persist user data in a `users.json` file, even after the containers are shut down and restarted. + +--- + +## 🛠️ Technology Stack + +* **FastAPI**: A modern, fast (high-performance) web framework for building APIs with Python 3.7+. +* **Docker**: A platform for developing, shipping, and running applications in containers. +* **Docker Compose**: A tool for defining and running multi-container Docker applications. + +--- + +## 📂 Project Structure + +``` + +. +├── app/ +│ ├── main.py +│ └── users.json +├── data/ +│ └── users.json +├── docker-compose.yml +├── Dockerfile +├── images/ +│ ├── docker compse down and up.png +│ ├── fast api.png +│ ├── hello message.png +│ ├── user data after compose up and down.png +│ └── user data.png +└── requirements.txt + +```` + +--- + +## 📋 How to Run the Application + +### Prerequisites + +* **Docker**: Ensure Docker is installed on your system. +* **Docker Compose**: Ensure Docker Compose is installed. + +### Steps + +1. Clone this repository to your local machine: + ```bash + git clone [https://github.com/govindkotalwar8/docker-fastapi-test.git](https://github.com/govindkotalwar8/docker-fastapi-test.git) + cd docker-fastapi-test + ``` + +2. Build and run the containers using Docker Compose: + ```bash + docker-compose up --build + ``` + This command will build the Docker image and start the FastAPI application in a container. The `-d` flag can be used to run in detached mode (`docker-compose up -d`). + +3. The application will be accessible at `http://localhost:8000`. You can test the API endpoints using the interactive documentation at `http://localhost:8000/docs`. + +--- + +## 🎯 API Endpoints + +The application provides the following endpoints: + +| Method | Endpoint | Description | +| :--- | :--- | :--- | +| **GET** | `/` | Returns a simple "Hello" message. | +| **GET** | `/users` | Returns a list of users stored in the `data/users.json` file. | +| **POST** | `/users` | Accepts new user data and appends it to the `data/users.json` file. | + +--- + +## 📸 Screenshots + +To demonstrate the application's functionality and persistence, here are screenshots showing key steps. + +### Initial API Access (`/docs`) +This shows the interactive API documentation provided by FastAPI. + +![FastAPI Interactive Documentation](images/fast%20api.png) + +### The "Hello" Message Endpoint +A successful response from the root endpoint. + +![Hello Message](images/hello%20message.png) + +### Adding a User +A screenshot showing data being posted to the `/users` endpoint. + +![User Data](images/user%20data.png) + +### Data Persistence After Restart +This screenshot proves that the user data is correctly stored and persists even after the container is shut down and brought back up, fulfilling a key project requirement. + +![User Data After Compose Restart](images/user%20data%20after%20compose%20up%20and%20down.png) + +--- + +## ✅ Task Completion + +* **Application Containerization**: The FastAPI application is successfully containerized using a `Dockerfile`. +* **Docker Compose Integration**: A `docker-compose.yml` file is provided to orchestrate the application and its dependencies. +* **Data Persistence**: The `users.json` file is correctly mounted as a volume, ensuring that user data is not lost when the container is stopped or restarted. This is verified by the screenshot of data persisting after a `docker-compose down` and `up` cycle. + +--- + +
+ +_This project was completed as part of a machine test for Nimap Infotech._ +```` +![FastAPI Interactive Documentation](images/fast api.png) +![Hello Message](images/hello message.png) +![User Data](images/user data.png) +![User Data After Compose Restart](images/user data after compose up and down.png) \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..7dd2748 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +version: "3.8" + +services: + fastapi: + build: . + container_name: fastapi_app + ports: + - "8000:8000" + volumes: + - ./data:/app/app/data + diff --git a/images/docker compse down and up.png b/images/docker compse down and up.png new file mode 100644 index 0000000..f5f62e7 Binary files /dev/null and b/images/docker compse down and up.png differ diff --git a/images/fast api.png b/images/fast api.png new file mode 100644 index 0000000..f4351cc Binary files /dev/null and b/images/fast api.png differ diff --git a/images/hello message.png b/images/hello message.png new file mode 100644 index 0000000..b60ede3 Binary files /dev/null and b/images/hello message.png differ diff --git a/images/user data after compose up and down.png b/images/user data after compose up and down.png new file mode 100644 index 0000000..06d1ba4 Binary files /dev/null and b/images/user data after compose up and down.png differ diff --git a/images/user data.png b/images/user data.png new file mode 100644 index 0000000..06d1ba4 Binary files /dev/null and b/images/user data.png differ