Skip to content

arifnd/python-base-image-test

Repository files navigation

Python Base Image Test

Try in Firebase Studio

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-alpine
  • python:3.12-slim
  • python:3.12 (full)

Key Features

  • Multi-stage builds: Reduces final image size by separating build and runtime stages.
  • Bytecode enabled: Optionally enables .pyc file generation to improve Python startup time.
  • Security testing: Basic security checks are included to identify vulnerabilities in the container image.

How to Build & Run

Before using this repository, make sure Docker is installed on your system. Then run the following commands in sequence:

Build Docker Image

./build.sh

Run Docker Compose

docker compose up -d

Run Database Migration

./migrate.sh

Run Benchmark

This benchmarks the running container using wrk and saves the results in wrk_results.csv.

./benchmark.sh

Image Size Comparison

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.

Performance Benchmark

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.

Performance Results

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

Security

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>

Sample Results

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)

Dependencies

  • Flask
  • Flask-SQLAlchemy
  • Flask-Migrate
  • Gunicorn
  • Psycopg2-binary

License

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.

About

Experimental results to determine a suitable python base image for a production environment

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages