Skip to content

Dockerizr

Python 3.8 Python 3.9 Python 3.10 Python 3.11

Introduction

The Dockerizr module is a dedicated sub-component of OuterSpace Apizr. Crafted in Python, its main objective is to simplify and expedite the dockerization process for Python-based projects. The module has undergone rigorous testing and is compatible with Python 3.8 and subsequent versions.


Features

Automated Dockerfile Generation

Dockerizr automatically crafts a Dockerfile tailored to the specific needs of your Python project.

Custom Entrypoint Support

Allows users to specify custom startup scripts or commands that will be executed prior to the main application's launch.

Dependency Analysis

Identifies and generates a requirements.txt file by analyzing imports from the project's source code.

Gunicorn Configuration

Generates an optimal configuration for Gunicorn, enabling your FastAPI application to run efficiently in production environments.

Usage

Python Script

To leverage Dockerizr, ensure you have Python 3.8 or a later version installed. Upon verification, you can incorporate the Dockerizr module into your Python script or notebook as illustrated:

from dockerizr.generator.dockerfileGenerator import DockerGenerator
from dockerizr.configuration import DockerizrConfiguration

# Define the necessary configuration parameters
config = DockerizrConfiguration(
    project_path="path_to_your_project",
    docker_image="desired_docker_image_name",
    python_version=(3, 8),  # example Python version
    # ... any other necessary configurations ...
)

# Initialize the DockerGenerator with the configuration
generator = DockerGenerator(config)

# Generate the Dockerfile content
dockerfile_content = generator.dockerfile_generator()

This will yield the Dockerfile content for the provided project path.

Usecase

Introduction to Usecase

Dockerizr streamlines the creation of a containerized setup for Python applications. Given an API file produced by FastApizr, Dockerizr generates:

  • A Dockerfile that defines how the application should be containerized.
  • A requirements.txt that lists all the necessary Python packages.
  • A wsgi.py that serves as an entry point for the Gunicorn server.
  • A gunicorn.conf.py that configures the Gunicorn server for optimal performance.

With these files in place, your Python application is primed for containerization and deployment using Docker.

Module Structure and Usage

Dockerizr
│
├── main.py - Entry point to interact with Dockerizr functionalities.
│
└── generator
    ├── dockerfileGenerator.py - Handles Dockerfile generation.
    │   ├── generate_dockerfile() - Writes the Dockerfile content to the project's main folder.
    │   ├── dockerfile_generator() - Crafts Dockerfile content using a Jinja2 template.
    |
    ├── requirementsAnalyzr.py - Manages the generation of the `requirements.txt` file.
    │   ├── generate_requirements() - Produces `requirements.txt` based on project imports.
    |
    ├── gunicornGenerator.py - Manages Gunicorn configuration file generation.
    │   ├── generate_gunicorn() - Produces WSGI and Gunicorn configuration files.
    │   ├── gunicorn_conf_generator() - Crafts Gunicorn configuration content using a Jinja2
    |
    ├── errorLogger.py - A decorator to log exceptions raised by decorated functions.
    |
    └── templates
        ├── dockerfile-alpine.jinja - Jinja2 template for Dockerfile generation
        ├── dockerfile-debian.jinja - Jinja2 template for Dockerfile generation
        ├── wsgi.jinja - Jinja2 template for Gunicorn configuration file generation
        ├── wsgi-conf.jinja - Jinja2 template for Gunicorn configuration file generation
        └── start.sh - Script to run the application

Docker

For Docker-based utilization of the Dockerizr module, ensure Docker is set up on your system. Once validated, the Dockerizr image can be fetched from Docker Hub:

docker pull outerspace.alien6.com/dockerizr

The Dockerizr container can then be initiated as:

docker run -it --rm outerspace.alien6.com/dockerizr

Dockerfile Explanation

The Dockerfile is an essential component in dockerizing applications. Here's a breakdown of the configurations and their purposes:

  1. Base Image: The python:3.11-alpine3.18 image is utilized due to its lightweight nature, ensuring reduced image sizes and faster deployment.
  2. Dependencies: Packages are updated and essential dependencies like gcc and musl-dev are installed. These are crucial for the compilation of certain Python libraries.
  3. Non-privileged User: A non-privileged user is created to run the application. This is a security measure to prevent the application from making unintended system-level changes inside the container.
  4. Environment Variables: The PYTHONPATH is set to ensure the source code is correctly detected by Python.
  5. Dependencies Installation: Dependencies are fetched from the requirements.txt to ensure all necessary packages for the application's operation are present.
  6. Port Exposure: Port 5001 is exposed, making the application accessible.
  7. Health Check: A health check ensures the application's continuous operation.
  8. Execution Command: Gunicorn serves the application, ensuring enhanced performance and effective request handling.

Gunicorn Configuration Explanation

Gunicorn is a Python WSGI HTTP server that's employed for serving Python applications in production environments. Here's an understanding of the configuration:

  1. Bind: The application is bound to 0.0.0.0:5001, making it accessible from any IP address.
  2. Workers: Setting workers to 2 allows for request processing parallelism, improving performance for concurrent applications.
  3. Worker Class: Specifying the uvicorn.workers.UvicornWorker class enables Gunicorn to serve ASGI applications like FastAPI.
  4. Timeout: This ensures that overly time-consuming requests are terminated, preventing server overloads.
  5. Logging: Logging levels and formats are defined to effectively monitor the application's operations.