This is a FastAPI application that provides a simple CRUD (Create, Read, Update, Delete) interface for managing books. The application uses MongoDB as the database and is containerized using Docker.
- List all books
- Get a specific book by ID
- Add a new book
- Update an existing book by ID
- Delete a book by ID
- Python 3.7 or higher
- Docker
- Docker Compose
-
Clone the repository:
git clone https://github.com/jutoken/fastapi-mongodb-docker-book-crud.git
-
Build and run the Docker containers using Docker Compose:
docker-compose up --build
This command will start both the FastAPI application and the MongoDB database.
- Endpoint:
GET /books/
- Response: List of books
- Endpoint:
GET /books/{id}
- Parameters:
id
: The ID of the book (string)
- Response: The book object
- Error Response: 404 if the book does not exist
- Endpoint:
POST /books/
- Body: JSON object representing the book
- Response: The created book object
- Endpoint:
PUT /books/{id}
- Parameters:
id
: The ID of the book (string)
- Body: JSON object with the updated book details
- Response: The updated book object
- Error Response: 400 if the update fails
- Endpoint:
DELETE /books/{id}
- Parameters:
id
: The ID of the book (string)
- Response: Success message
- Error Response: 400 if the delete fails
Here is the docker-compose.yml
file used to configure the application and MongoDB:
version: '3.8'
services:
app:
container_name: "tersea-group"
build:
dockerfile: ./Dockerfile
ports:
- 8080:8080
depends_on:
- db
volumes:
- .:/core
db:
image: mongo
ports:
- 27017:27017
After starting the Docker containers, you can access the FastAPI application at:
http://localhost:8080
You can also view the automatically generated API documentation at:
http://localhost:8080/docs