This repository contains experimental results to determine the most suitable Python base image for a production environment. It compares the performance and size of three different base images:
python:3.12-alpinepython:3.12-slimpython:3.12(full)
- Multi-stage builds: Reduces final image size by separating build and runtime stages.
- Bytecode enabled: Optionally enables
.pycfile generation to improve Python startup time. - Security testing: Basic security checks are included to identify vulnerabilities in the container image.
Before using this repository, make sure Docker is installed on your system. Then run the following commands in sequence:
./build.shdocker compose up -d./migrate.shThis benchmarks the running container using wrk and saves the results in wrk_results.csv.
./benchmark.sh| Base Image | Multi-Stage Build | Bytecode Enabled | Image Size | Build Time |
|---|---|---|---|---|
| alpine | ❌ | ❌ | 100.71 MB | 3s |
| alpine | ✅ | ✅ | 87.05 MB | 29s |
| alpine | ✅ | ❌ | 87.05 MB | 26s |
| slim | ❌ | ❌ | 171.35 MB | 2s |
| slim | ✅ | ✅ | 157.71 MB | 22s |
| slim | ✅ | ❌ | 157.71 MB | 20s |
| full | ❌ | ✅ | 1015.89 MB | 2s |
ℹ️ Enabling Python bytecode (.pyc files) may improve performance by avoiding source recompilation at runtime.
ℹ️ Multi-stage builds significantly reduce image size without sacrificing runtime performance.
The benchmark is based on the average of three test runs using wrk, configured with 2 threads, 10 concurrent connections, and a duration of 10 seconds per test.
| image | requests/sec | latency |
|---|---|---|
| alpine | 259,77 | 38,08 |
| alpine-multi | 261,85 | 37,81 |
| alpine-multi-bytecode | 262,34 | 37,71 |
| slim | 283,03 | 34,97 |
| slim-multi | 282,86 | 35,00 |
| slim-multi-bytecode | 278,98 | 35,47 |
| full | 278,10 | 35,57 |
To check for known security vulnerabilities in your Docker image, you can use Trivy, a simple and fast vulnerability scanner.
Run the following command:
trivy image <image-name>Below are example scan results comparing the three Python base images used in this project:
flask-app:alpine (alpine 3.22.1)
================================
Total: 1 (UNKNOWN: 0, LOW: 0, MEDIUM: 0, HIGH: 0, CRITICAL: 1)
flask-app:slim (debian 12.11)
=============================
Total: 106 (UNKNOWN: 1, LOW: 74, MEDIUM: 22, HIGH: 7, CRITICAL: 2)
flask-app:full (debian 12.11)
=============================
Total: 1711 (UNKNOWN: 6, LOW: 775, MEDIUM: 666, HIGH: 253, CRITICAL: 11)
- Flask
- Flask-SQLAlchemy
- Flask-Migrate
- Gunicorn
- Psycopg2-binary
This project is open source and available under the terms of the MIT License.
Thank you for checking out this repository!
Feel free to fork, contribute, or open an issue if you have feedback or suggestions.
