This project is a simple CRUD (Create, Read, Update, Delete) API built with Flask and SQLAlchemy, using SQLite as the database. The application is containerized using Docker and Docker Compose.
app.py
: The main application file containing the Flask API logic.Dockerfile
: Defines the Docker image for the Flask application.docker-compose.yml
: Configures the Docker services and volumes.requirements.txt
: Lists the Python dependencies for the project.
- Create: Add new products.
- Read: List all products and get details of a specific product.
- Update: Modify existing products.
- Delete: Remove products from the database.
This project utilizes distroless images for the final Docker image build. Distroless images are minimal container images that include only the essential components required to run the application, significantly reducing the attack surface and potential vulnerabilities. By using a distroless image for the Flask application, this project ensures a more secure and optimized production environment.
- Security: Distroless images contain only what is necessary to run the application, eliminating unnecessary packages and reducing the risk of vulnerabilities.
- Smaller Image Size: These images are smaller, which speeds up deployment and reduces the storage requirements.
- Consistency: Distroless images provide a consistent runtime environment, ensuring that the application behaves the same in different environments.
The Dockerfile for this project uses a multi-stage build process:
- Builder Stage: A full-featured Python image (
python:3.9-slim
) is used to install dependencies and build the application. - Production Stage: The final image is built using a distroless base (
gcr.io/distroless/python3-debian11
), containing only the Python runtime and the application code.
-
Clone the Repository
git clone https://github.com/lisazevedo/basic-crud-api cd basic-crud-api
-
Build and Start the Containers
docker compose up --build
This command will:
- Build the Docker image for the Flask application.
- Start a
busybox
container to handle the SQLite volume. - Start the Flask application container, linking it with the SQLite volume.
-
Access the API
The Flask API will be available at
http://localhost:5000
.
-
Create Product
POST /product
Request Body (JSON):
{ "name": "Sample Product", "price": 19.99 }
Response (JSON):
{ "message": "Product created successfully", "product": { "id": 1, "name": "Sample Product", "price": 19.99 } }
-
Get All Products
GET /products
Response (JSON):
[ { "id": 1, "name": "Sample Product", "price": 19.99 } ]
-
Get Product by ID
GET /product/<id>
Response (JSON):
{ "id": 1, "name": "Sample Product", "price": 19.99 }
-
Update Product by ID
PUT /product/<id>
Request Body (JSON):
{ "name": "Updated Product", "price": 24.99 }
Response (JSON):
{ "message": "Product updated successfully", "product": { "id": 1, "name": "Updated Product", "price": 24.99 } }
-
Delete Product by ID
DELETE /product/<id>
Response (JSON):
{ "message": "Product deleted successfully" }
To make changes to the Flask application:
- Modify
app.py
: Update the application logic as needed. - Rebuild the Docker Image: Run
docker compose build
to apply the changes. - Restart the Containers: Use
docker compose up
to restart the application.
- Database Table Not Found: Ensure that
db.create_all()
is called before handling requests. Check the Docker Compose volume mount to ensure the SQLite database file is correctly shared.