Skip to content

DeanLuus1994/python-slim-dev-container

Repository files navigation

Python Slim Dev Container

Docker     Microsoft     Python     Anthropic     Hugging Face

CI Status Python Version Docker Dev Container Code style: black

Overview

The Python Slim Dev Container provides a lightweight, optimized, and production-ready development environment for Python applications. Designed with performance and maintainability in mind, it follows best practices for containerization, security, and code quality.

💫 Personal Journey & Motivation (Click to expand)

With a decade of experience in IT, I knew nothing at all about Python or AI two years ago. Diving head first into the depths with a deep understanding of programming logic, best practices, and principles, I got lost in a whole new ever-changing and fast-paced evolving world.

This highly minimal implementation of the most current best practices aims to add some color to those grey areas for anybody that feels they're in the same situation that I was in.

Feel free to contribute, just note that the coding governance and standards are extremely strictly aligned with the most current PEP guidelines and DRY modular architecture.

This is done so that at least I can follow the spaghetti packages and code implementations that needed to be ironed out.

🚀 Key Features

  • Multi-stage build with optimized layers for minimal footprint
  • Poetry dependency management with clear separation of production/dev dependencies
  • Remote development through SSH with automatic key display
  • Industry-standard code quality with pre-commit hooks and CI/CD
  • Database integration with PostgreSQL and proper volume management
  • Python optimization flags for maximum performance
  • Security features including non-root user, minimal exposed ports
  • VS Code integration with curated extensions and settings

The only dependencies you need installed are:

📊 Performance Matrix

Component Optimization Standards Security Maintainability Overall
Dockerfile 9/10 ⭐ 9/10 ⭐ 8/10 ✅ 9/10 ⭐ 8.9/10
docker-compose.yml 8/10 ✅ 9/10 ⭐ 7/10 ✅ 10/10 ⭐⭐ 8.6/10
devcontainer.json 9/10 ⭐ 10/10 ⭐⭐ 9/10 ⭐ 10/10 ⭐⭐ 9.3/10
.env 10/10 ⭐⭐ 10/10 ⭐⭐ 7/10 ✅ 10/10 ⭐⭐ 9.3/10
pyproject.toml 9/10 ⭐ 10/10 ⭐⭐ 8/10 ✅ 9/10 ⭐ 9.2/10
pre-commit config 9/10 ⭐ 10/10 ⭐⭐ 10/10 ⭐⭐ 9/10 ⭐ 9.4/10
CI/CD workflow 8/10 ✅ 9/10 ⭐ 8/10 ✅ 8/10 ✅ 8.4/10

📋 Table of Contents

🏗️ Architecture Overview

This development container uses a microservices architecture with two primary services:

  1. Python Application Container: A slim Python environment with development tools
  2. PostgreSQL Database: Persistent data storage with proper volume mapping
Architecture Diagram (Click to expand)
┌─────────────────────────────────────────────────────────────┐
│                    Docker Compose Network                    │
│                                                             │
│  ┌─────────────────────────┐      ┌─────────────────────┐  │
│  │                         │      │                     │  │
│  │   Python Application    │      │    PostgreSQL DB    │  │
│  │     (VS Code Host)      │◄────►│                     │  │
│  │                         │      │                     │  │
│  └─────────────┬───────────┘      └─────────────────────┘  │
│                │                                           │
│                ▼                                           │
│  ┌─────────────────────────┐                               │
│  │                         │                               │
│  │   VS Code Extensions    │                               │
│  │   & Dev Environment     │                               │
│  │                         │                               │
│  └─────────────────────────┘                               │
│                                                             │
└─────────────────────────────────────────────────────────────┘

The architecture emphasizes:

  • Performance: Minimal footprint with binary-only packages and slim images
  • Reproducibility: Precisely pinned dependencies and explicit configurations
  • Security: Non-root user by default with toggle option
  • Standards: PEP-compliant tools with centralized configuration

🔧 Key Technologies

Category Technology Purpose
Container Docker Container runtime and build system
Docker Compose Multi-service orchestration
VS Code Devcontainers Development environment integration
Python Python 3.11.8 Runtime environment with optimization flags
Poetry Dependency management
PyYAML, Rich, Typer, Pydantic Core libraries for application development
Black, Ruff, MyPy Code quality and type checking
AI/ML Hugging Face AI model access and integration
Anthropic Claude API AI assistant integration
CI/CD GitHub Actions Automated testing and deployment
Pre-commit hooks Local validation before commits

📂 Project Structure

The repository is organized into a hierarchical structure that separates concerns:

Root Directory

Contains project-wide configuration and documentation files:

Contains all container configuration files to define the development environment:

Contains GitHub-specific configuration files:

🛠️ Container Components

# Multi-stage build for optimization
FROM python:3.11.8-slim-bullseye AS builder
└── Set optimization flags
└── Install Poetry and dependencies
└── Configure Python environment

FROM python:3.11.8-slim-bullseye
└── Copy Python packages from builder
└── Create non-root user
└── Set up SSH for remote access
└── Configure environment

Defines two services:

  • app: Python development environment with VS Code integration
  • db: PostgreSQL database with volume persistence

Environment Variables (.devcontainer/.env)

Contains essential configuration for the development environment:

  • Container resource allocation
  • Database credentials
  • Development mode toggle

🐍 Python Environment

Dependency Management with Poetry

Poetry provides modern Python dependency management with:

  • Clear separation between main and development dependencies
  • Version pinning for reproducibility
  • Lock file generation
  • Virtual environment management

Example from pyproject.toml:

[tool.poetry.dependencies]
python = "^3.11"
pyyaml = "6.0"
click = "8.1.3"
rich = "10.16.2"
typer = "0.7.0"
pydantic = "1.10.8"

VS Code Integration (.devcontainer/devcontainer.json)

Provides seamless development experience with:

  • Preconfigured Python extensions
  • Code formatting and linting
  • Remote container access
  • SSH server for external connections

✅ Code Quality & Standards

Pre-commit Hooks (.pre-commit-config.yaml)

Ensures code quality with automated checks:

repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    # Basic checks for common issues
  - repo: https://github.com/psf/black
    # Code formatting
  - repo: https://github.com/astral-sh/ruff-pre-commit
    # Fast linting
  - repo: https://github.com/pre-commit/mirrors-mypy
    # Type checking
  - repo: https://github.com/hadolint/hadolint
    # Dockerfile linting
  - repo: https://github.com/gitleaks/gitleaks
    # Secret scanning

Python Tool Configuration (pyproject.toml)

Centralizes configuration for multiple tools:

  • Black for code formatting
  • Ruff for fast linting
  • MyPy for type checking
  • Pytest for testing

🔒 Security Features

  1. Non-root User: Container runs as vscode user by default
  2. SSH Key Management: Secure key generation for remote access
  3. Limited Port Exposure: Only essential ports exposed
  4. Secret Detection: Pre-commit hook to detect secrets in code
  5. Dependency Scanning: Dependabot for security updates
  6. Type Safety: MyPy for type checking

🔄 CI/CD Integration

GitHub Workflows (.github/workflows/python-ci.yml)

Automated testing and validation:

jobs:
  lint:
    # Code quality checks using pre-commit
  test:
    # Run pytest with coverage reporting

Dependabot Configuration (.github/dependabot.yml)

Automated dependency updates for:

  • GitHub Actions workflows
  • Dev container components
  • Python dependencies

🚀 Getting Started

  1. Prerequisites:

  2. Clone and Open:

    git clone https://github.com/yourusername/python-slim-dev-container.git
    cd python-slim-dev-container
    code .
  3. Start Development:

    • When prompted, click "Reopen in Container"
    • The container will build and start automatically
    • Your SSH key will be displayed in the terminal for remote connections

📝 Configuration Files Explained

Click to expand file details
File Purpose Implementation Details
pyproject.toml Central configuration hub Configures Black, Ruff, MyPy, Poetry
.pre-commit-config.yaml Git hook automation Ensures code quality before commits
devcontainer.json VS Code integration Configures editor and extensions
Dockerfile Container definition Multi-stage build with optimizations
docker-compose.yml Service orchestration Defines app and database services
.env Environment variables Configures container behavior
python-ci.yml CI/CD workflow Automates testing and validation
dependabot.yml Dependency updates Manages security and version updates

🤗 HuggingFace Integration

Coming soon! The next phase of this project will include:

  • HuggingFace model loading and inference
  • Optimized container setup for ML workloads
  • GPU passthrough configuration
  • Model fine-tuning capabilities
  • Integration with popular frameworks

SUGGESTIONS WELCOME FOR CROSS COMPATIBILITY LEAN OPTIMIZED SUGGESTIONS THAT ARE PERFORMANT.

Stay tuned for updates as we implement these exciting features!

👥 Contributing

Contributions are welcome and appreciated! Here's how you can help:

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes with descriptive messages
  4. Push to your branch
  5. Submit a Pull Request

Please ensure your code follows our standards by running pre-commit hooks locally:

pip install pre-commit
pre-commit install

Built with ❤️ using Docker, Python, and VS Code Dev Containers

DockerMicrosoftPythonAnthropicHuggingFace

About

Dev Container Implementation of Performant Coding Automation Agent

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors