|
| 1 | +# Python Runner |
| 2 | + |
| 3 | +A flexible Docker-based Python runner that simplifies deployment and execution of Python applications with configurable environments. |
| 4 | + |
| 5 | +## Available Images |
| 6 | + |
| 7 | +Three variants are provided to suit different needs: |
| 8 | + |
| 9 | +- **Regular** (`mantissoftware/python-runner:[version]`): Based on the official Python image, includes typical packages for general use. |
| 10 | +- **Slim** (`mantissoftware/python-runner:[version]-slim`): Based on python:[version]-slim, smaller size with minimal dependencies. |
| 11 | +- **Lite** (`mantissoftware/python-runner:[version]-lite`): Based on python:[version]-alpine, ultra-lightweight for minimal deployments. |
| 12 | + |
| 13 | +Replace `[version]` with a Python version like `3.12`. |
| 14 | + |
| 15 | +## Features |
| 16 | + |
| 17 | +- Creates a Python virtual environment automatically |
| 18 | +- Supports installation of packages from custom repositories |
| 19 | +- Installs OS-level dependencies on demand |
| 20 | +- Provides development tools when needed |
| 21 | +- Configurable startup commands |
| 22 | +- Support for pre-start and post-start scripts |
| 23 | + |
| 24 | +## Environment Variables |
| 25 | + |
| 26 | +### Package Installation |
| 27 | + |
| 28 | +| Variable | Description | Example | |
| 29 | +|----------|-------------|---------| |
| 30 | +| `REQUIREMENTS_PACKAGES` | Space-separated list of packages to install. Usable when third party tools/libraries is required but they're not dependency of main package. | `"numpy pandas requests"` | |
| 31 | +| `PACKAGE_NAME` | Main package to install | `"flask"` | |
| 32 | +| `PACKAGE_VERSION` | Version of the main package (Optional). Latest available version will install when not specified. | `"2.0.1"` | |
| 33 | +| `UPDATE_PIP` | Update pip before run (default: true) | `"false"` | |
| 34 | + |
| 35 | +### Repository Configuration |
| 36 | + |
| 37 | +| Variable | Description | Example | |
| 38 | +|----------|-------------|---------| |
| 39 | +| `REPOSITORY_URL` | URL of custom PyPI repository | `"https://pypi.mycompany.com"` | |
| 40 | +| `REPOSITORY_HOST` | Host of custom PyPI repository | `"pypi.mycompany.com"` | |
| 41 | + |
| 42 | +### System Configuration |
| 43 | + |
| 44 | +| Variable | Description | Example | |
| 45 | +|----------|-------------|---------| |
| 46 | +| `OS_DEPENDENCIES` | Space-separated list of system packages to install. | `"curl git"` | |
| 47 | +| `INSTALL_DEV_TOOLS` | Whether to install build tools. Use it (default: false) | `"true"` | |
| 48 | + |
| 49 | +### Execution Control |
| 50 | + |
| 51 | +| Variable | Description | Example | |
| 52 | +|----------|-------------|---------| |
| 53 | +| `STARTUP_COMMAND` | Command to execute | `"flask"` or `"python app.py"` | |
| 54 | +| `PRE_START_SCRIPT` | Path to script to run before startup | `"/app/pre_start.sh"` | |
| 55 | +| `POST_START_SCRIPT` | Path to script to run after startup | `"/app/post_start.sh"` | |
| 56 | + |
| 57 | +## Usage Examples |
| 58 | + |
| 59 | +### Simple Run |
| 60 | +```bash |
| 61 | +docker run -it --rm mantissoftware/python-runner:3.12 \ |
| 62 | +-e REQUIREMENTS_PACKAGES="requests" \ |
| 63 | +-e STARTUP_COMMAND="python" |
| 64 | +``` |
| 65 | + |
| 66 | +### Run a Flask Application |
| 67 | +```yaml |
| 68 | +version: '3' |
| 69 | + |
| 70 | +services: |
| 71 | + web: |
| 72 | + image: mantissoftware/python-runner:3.12-slim |
| 73 | + environment: |
| 74 | + - REQUIREMENTS_PACKAGES=flask gunicorn |
| 75 | + - STARTUP_COMMAND=gunicorn -b 0.0.0.0:5000 app:app |
| 76 | + ports: |
| 77 | + - "5000:5000" |
| 78 | + volumes: |
| 79 | + - ./:/app/src |
| 80 | + working_dir: /app/src |
| 81 | +``` |
| 82 | +
|
| 83 | +### Data Science Environment |
| 84 | +```yaml |
| 85 | +version: '3' |
| 86 | + |
| 87 | +services: |
| 88 | + jupyter: |
| 89 | + image: mantissoftware/python-runner:3.12 |
| 90 | + environment: |
| 91 | + - REQUIREMENTS_PACKAGES=jupyter pandas matplotlib scikit-learn |
| 92 | + - STARTUP_COMMAND=jupyter notebook --ip=0.0.0.0 --no-browser --allow-root |
| 93 | + - INSTALL_DEV_TOOLS=true |
| 94 | + ports: |
| 95 | + - "8888:8888" |
| 96 | + volumes: |
| 97 | + - ./notebooks:/app/notebooks |
| 98 | + working_dir: /app/notebooks |
| 99 | +``` |
| 100 | +## Building Locally |
| 101 | +To build the images locally: |
| 102 | +```bash |
| 103 | +# Set Python version |
| 104 | +PYTHON_VERSION=3.12 |
| 105 | + |
| 106 | +# Regular image |
| 107 | +docker build -t python-runner:${PYTHON_VERSION} \ |
| 108 | + -f Dockerfile-regular \ |
| 109 | + --build-arg PYTHON_VERSION=${PYTHON_VERSION} . |
| 110 | + |
| 111 | +# Slim image |
| 112 | +docker build -t python-runner:${PYTHON_VERSION}-slim \ |
| 113 | + -f Dockerfile-slim \ |
| 114 | + --build-arg PYTHON_VERSION=${PYTHON_VERSION} . |
| 115 | + |
| 116 | +# Lite image |
| 117 | +docker build -t python-runner:${PYTHON_VERSION}-lite \ |
| 118 | + -f Dockerfile-lite \ |
| 119 | + --build-arg PYTHON_VERSION=${PYTHON_VERSION} . |
| 120 | +``` |
| 121 | + |
| 122 | +## How It Works |
| 123 | + |
| 124 | +The container starts and installs any requested OS dependencies |
| 125 | + |
| 126 | +1. A virtual environment is created in /app/venv |
| 127 | +2. Pip is configured for custom repositories if needed |
| 128 | +3. Required packages are installed |
| 129 | +4. Pre-start script is executed (if specified) |
| 130 | +5. The startup command or package is executed |
| 131 | +6. Post-start script is executed (if specified) |
| 132 | + |
| 133 | +## License |
| 134 | + |
| 135 | +This project is licensed under the MIT License - see the LICENSE file for details. |
0 commit comments