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.
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.
- 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.
.
├── 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
- Docker: Ensure Docker is installed on your system.
- Docker Compose: Ensure Docker Compose is installed.
-
Clone this repository to your local machine:
git clone [https://github.com/govindkotalwar8/docker-fastapi-test.git](https://github.com/govindkotalwar8/docker-fastapi-test.git) cd docker-fastapi-test -
Build and run the containers using Docker Compose:
docker-compose up --build
This command will build the Docker image and start the FastAPI application in a container. The
-dflag can be used to run in detached mode (docker-compose up -d). -
The application will be accessible at
http://localhost:8000. You can test the API endpoints using the interactive documentation athttp://localhost:8000/docs.
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. |
To demonstrate the application's functionality and persistence, here are screenshots showing key steps.
This shows the interactive API documentation provided by FastAPI.
A successful response from the root endpoint.
A screenshot showing data being posted to the /users endpoint.
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.
- Application Containerization: The FastAPI application is successfully containerized using a
Dockerfile. - Docker Compose Integration: A
docker-compose.ymlfile is provided to orchestrate the application and its dependencies. - Data Persistence: The
users.jsonfile 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 adocker-compose downandupcycle.
This project was completed as part of a machine test for Nimap Infotech.







