https://github.com/pkramek/python-devcontainer
๐ Modern Python DevContainer template with UV, Ruff, and zero-config magic. Because life's too short for dependency hell.
https://github.com/pkramek/python-devcontainer
devcontainer docker docker-compose pre-commit python ruff
Last synced: about 1 month ago
JSON representation
๐ Modern Python DevContainer template with UV, Ruff, and zero-config magic. Because life's too short for dependency hell.
- Host: GitHub
- URL: https://github.com/pkramek/python-devcontainer
- Owner: PKramek
- License: bsd-3-clause
- Created: 2025-07-20T13:13:20.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-07-20T15:06:14.000Z (11 months ago)
- Last Synced: 2025-07-20T16:22:30.656Z (11 months ago)
- Topics: devcontainer, docker, docker-compose, pre-commit, python, ruff
- Language: Dockerfile
- Homepage:
- Size: 28.3 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ๐ Python DevContainer Template
[](https://opensource.org/licenses/MIT)
[](https://www.python.org/downloads/)
[](https://docs.astral.sh/uv/)
[](https://github.com/astral-sh/ruff)
[](https://code.visualstudio.com/docs/devcontainers/containers)
[](https://github.com/pre-commit/pre-commit)
> ๐ **A modern, production-ready Python development environment using VS Code DevContainers with UV package management and comprehensive code quality tools.**
This template provides a complete, containerized Python development environment that works consistently across different machines and operating systems. Perfect for teams who want to standardize their development setup and onboard new developers quickly.
## โจ Features
### ๐๏ธ **Modern Python Development**
- **Python 3.12** - Latest stable Python version
- **UV Package Manager** - Lightning-fast Python package installer and resolver
- **PEP 621 Compliant** - Modern `pyproject.toml` configuration
- **Development Dependencies** - Pre-configured with essential dev tools
### ๐ณ **Containerized Development Environment**
- **VS Code DevContainer** - Complete development environment in a container
- **Docker Compose Setup** - Multi-service architecture ready for databases, OTEL collectors, and other services
- **Cross-platform Support** - Works on Intel, AMD, and Apple Silicon
- **Instant Setup** - Zero configuration required for new team members
### ๐ง **Code Quality & Developer Experience**
- **Ruff** - Ultra-fast Python linter and formatter (replaces Black + isort + flake8)
- **MyPy** - Static type checking for better code reliability
- **Pre-commit Hooks** - Automated code quality checks before commits
- **Security Scanning** - Bandit for security vulnerability detection
- **Secret Detection** - Prevent accidental commits of sensitive data
### ๐ ๏ธ **VS Code Integration**
- **Rich Extensions** - Python, Ruff, MyPy, GitLens, GitHub Copilot, and more
- **IntelliSense** - Advanced code completion and navigation
- **Debugging Support** - Full debugging capabilities pre-configured
- **Integrated Terminal** - All tools available in the integrated terminal
### ๐ **Multi-Service Architecture**
- **Docker Compose Ready** - Easily add databases, message queues, OTEL collectors, and other services
- **Service Networking** - Pre-configured backend network for service communication
- **Environment Configuration** - Flexible environment variable management
- **Development Scaling** - Support for complex local development scenarios
## ๐ Table of Contents
- [Quick Start](#-quick-start)
- [Prerequisites](#-prerequisites)
- [Installation](#-installation)
- [Usage](#-usage)
- [Project Structure](#-project-structure)
- [Development Tools](#-development-tools)
- [Code Quality](#-code-quality)
- [Configuration](#-configuration)
- [Adding Services](#-adding-services)
- [Troubleshooting](#-troubleshooting)
- [Contributing](#-contributing)
- [License](#-license)
## ๐ Quick Start
### Using GitHub Template
1. **Click "Use this template"** button at the top of this repository
2. **Clone your new repository**:
```bash
git clone https://github.com/yourusername/your-repo-name.git
cd your-repo-name
```
3. **Open in VS Code** and select "Reopen in Container" when prompted
4. **Start coding!** ๐
### One-Click Setup with Codespaces
[](https://codespaces.new/yourusername/python-devcontainer?quickstart=1)
## ๐ Prerequisites
Before you begin, ensure you have the following installed:
### Required
- **[Docker Desktop](https://www.docker.com/products/docker-desktop/)** (Latest version)
- **[VS Code](https://code.visualstudio.com/)** (Latest version)
- **[Dev Containers Extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers)** for VS Code
### System Requirements
- **Operating System**: Windows 10/11, macOS, or Linux
- **Memory**: 8GB RAM minimum (16GB recommended)
- **Storage**: 10GB free space minimum
- **Architecture**: x86_64 or ARM64 (Apple Silicon supported)
## ๐ ๏ธ Installation
### 1. Clone the Repository
```bash
git clone https://github.com/yourusername/python-devcontainer.git
cd python-devcontainer
```
### 2. Open in VS Code
```bash
code .
```
### 3. Reopen in Container
When VS Code opens, you'll see a notification:
> **"Folder contains a Dev Container configuration file. Reopen folder to develop in a container"**
Click **"Reopen in Container"** or:
- Press `F1` โ Type "Dev Containers: Reopen in Container"
- Use Command Palette: `Ctrl+Shift+P` (Windows/Linux) or `Cmd+Shift+P` (macOS)
### 4. Wait for Container Build
The first time will take a few minutes to:
- Download the base Docker images
- Install Python dependencies
- Configure VS Code extensions
- Set up the development environment
## ๐ Usage
### Running the Sample Application
```bash
# Test the sample application
python main.py
# Expected output:
# Hello from python-devcontainer!
```
### Package Management with UV
```bash
# Install a new package
uv add requests
# Install development dependency
uv add --dev pytest
# Install from lock file
uv sync
# Update dependencies
uv lock --upgrade
# Show installed packages
uv pip list
```
### Code Quality Tools
```bash
# Run linting
ruff check .
# Auto-fix issues
ruff check --fix .
# Format code
ruff format .
# Type checking
mypy main.py
# Run all pre-commit hooks
pre-commit run --all-files
# Security scan
bandit -r .
```
## ๐ Project Structure
```
python-devcontainer/
โโโ .devcontainer/
โ โโโ devcontainer.json # VS Code DevContainer configuration
โ โโโ Dockerfile # Python development environment
โ โโโ docker-compose.yml # Multi-service setup
โ โโโ setup.sh # Post-creation setup script
โ โโโ .env.example # Environment variables template
โโโ .pre-commit-config.yaml # Pre-commit hooks configuration
โโโ pyproject.toml # Python project configuration
โโโ uv.lock # Dependency lock file
โโโ main.py # Sample Python application
โโโ README.md # This file
```
## ๐ง Development Tools
### Included Tools
| Tool | Purpose | Configuration |
|------|---------|---------------|
| **UV** | Package management | `pyproject.toml` |
| **Ruff** | Linting & formatting | `pyproject.toml` |
| **MyPy** | Type checking | `pyproject.toml` |
| **Bandit** | Security scanning | `pyproject.toml` |
| **PyUpgrade | automatically upgrade syntax | `pyproject.toml` |
| **Pre-commit** | Git hooks | `.pre-commit-config.yaml` |
### VS Code Extensions
Pre-installed extensions for the best development experience:
- **Python** - Python language support
- **Ruff** - Python linting and formatting
- **MyPy Type Checker** - Static type checking
- **Python Typehint** - Enhanced type hint support
- **Python Indent** - Correct Python indentation
- **IntelliCode** - AI-assisted development
- **GitLens** - Enhanced Git capabilities
- **GitHub Copilot** - AI pair programming
- **TODO Tree** - Task management
## โ
Code Quality
### Automated Quality Checks
This template enforces code quality through multiple layers:
#### Pre-commit Hooks
- **File checks**: Large files, merge conflicts, YAML/TOML syntax
- **Security**: Private key detection, secret scanning
- **Python**: AST validation, debug statement detection
- **Formatting**: Trailing whitespace, end-of-file fixes
- **Code Quality**: Ruff linting and formatting
#### Static Analysis
- **Ruff**: Comprehensive linting (replaces flake8, isort, and more)
- **MyPy**: Static type checking for better code reliability
- **Bandit**: Security vulnerability scanning
#### Configuration
All tools are configured in `pyproject.toml`:
```toml
[tool.ruff]
line-length = 88
target-version = "py312"
[tool.mypy]
python_version = "3.12"
strict = true
[tool.bandit]
exclude_dirs = ["tests"]
```
## โ๏ธ Configuration
### Environment Variables
The template includes an `.env.example` file for environment variable configuration. Copy and customize it for your needs:
```bash
cp .devcontainer/.env.example .devcontainer/.env
```
Add your environment variables to the `.env` file:
```bash
# Example environment variables
API_KEY=your_api_key_here
DATABASE_URL=postgresql://user:pass@host:port/dbname
DEBUG=true
```
### Customizing the Environment
#### Adding Dependencies
```bash
# Production dependency
uv add package-name
# Development dependency
uv add --dev package-name
# Optional dependency group
uv add --group group-name package-name
```
#### VS Code Settings
Customize VS Code settings in `.devcontainer/devcontainer.json`:
```json
{
"customizations": {
"vscode": {
"settings": {
"python.defaultInterpreterPath": "/usr/local/bin/python"
},
"extensions": [
"ms-python.python"
]
}
}
}
```
## ๐ณ Adding Services
This template uses Docker Compose, making it easy to add additional services for local development. The setup includes a pre-configured backend network for service communication.
### Example: Adding a Database
Add a PostgreSQL database to your `docker-compose.yml`:
```yaml
services:
python-devcontainer:
# ... existing configuration ...
depends_on:
- postgres
postgres:
image: postgres:15
environment:
POSTGRES_DB: myapp
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
networks:
- backend
volumes:
postgres_data:
```
### Example: Adding Redis Cache
```yaml
services:
# ... existing services ...
redis:
image: redis:7-alpine
ports:
- "6379:6379"
networks:
- backend
```
### Example: Adding OTEL Collector
```yaml
services:
# ... existing services ...
otel-collector:
image: otel/opentelemetry-collector:latest
command: ["--config=/etc/otel-collector-config.yml"]
volumes:
- ./otel-collector-config.yml:/etc/otel-collector-config.yml
ports:
- "8888:8888" # Prometheus metrics
- "8889:8889" # Prometheus exporter metrics
- "13133:13133" # health_check extension
- "4317:4317" # OTLP gRPC receiver
- "4318:4318" # OTLP HTTP receiver
networks:
- backend
```
### Service Communication
Services can communicate using their service names as hostnames:
```python
# Connect to PostgreSQL from Python container
import psycopg2
conn = psycopg2.connect(
host="postgres", # Service name as hostname
port=5432,
database="myapp",
user="postgres",
password="postgres"
)
# Connect to Redis
import redis
r = redis.Redis(host="redis", port=6379, db=0)
```
## ๐ Troubleshooting
### Common Issues
#### Container Won't Start
```bash
# Rebuild container
Ctrl+Shift+P โ "Dev Containers: Rebuild Container"
# Or rebuild without cache
Ctrl+Shift+P โ "Dev Containers: Rebuild Container Without Cache"
```
#### Python Package Issues
```bash
# Reinstall all packages
uv sync --refresh
# Clear UV cache
uv cache clean
```
#### Service Connection Issues
```bash
# Check all container status
docker ps
# View logs for specific service
docker logs python-devcontainer--1
# Restart specific service
docker restart python-devcontainer--1
# Check network connectivity
docker network ls
docker network inspect python-devcontainer_backend
```
#### Permission Issues (Linux/macOS)
```bash
# Fix file permissions
sudo chown -R $USER:$USER .
```
### Performance Tips
- **Enable Docker BuildKit** for faster builds
- **Allocate more memory** to Docker (8GB recommended)
- **Use SSD storage** for better I/O performance
- **Close unnecessary VS Code windows** to save resources
- **Use Docker volumes** for data that needs to persist
## ๐ค Contributing
We welcome contributions! Please see our contributing guidelines:
### Getting Started
1. **Fork** the repository
2. **Create** a feature branch: `git checkout -b feature/amazing-feature`
3. **Make** your changes
4. **Run** tests and quality checks: `pre-commit run --all-files`
5. **Commit** your changes: `git commit -m 'Add amazing feature'`
6. **Push** to the branch: `git push origin feature/amazing-feature`
7. **Open** a Pull Request
### Development Guidelines
- Follow **PEP 8** style guidelines (enforced by Ruff)
- Add **type hints** to all functions
- Write **tests** for new functionality
- Update **documentation** as needed
- Ensure all **quality checks pass**
### Code of Conduct
This project follows the [Contributor Covenant](https://www.contributor-covenant.org/) Code of Conduct.
## ๐ Resources
### Documentation
- [VS Code DevContainers](https://code.visualstudio.com/docs/devcontainers/containers)
- [UV Package Manager](https://docs.astral.sh/uv/)
- [Ruff Linter](https://docs.astral.sh/ruff/)
- [Pre-commit Hooks](https://pre-commit.com/)
- [Docker Compose](https://docs.docker.com/compose/)
- [Python Type Hints](https://docs.python.org/3/library/typing.html)
### Learning Materials
- [Python Best Practices](https://realpython.com/python-code-quality/)
- [Docker for Developers](https://docs.docker.com/get-started/)
- [Docker Compose Tutorial](https://docs.docker.com/compose/gettingstarted/)
## ๐ Why This Template?
### For Individual Developers
- โก **Zero setup time** - Start coding immediately
- ๐ง **Best practices built-in** - Modern tooling pre-configured
- ๐ก๏ธ **Quality assurance** - Automated checks prevent issues
- ๐ **Learning opportunity** - Discover modern Python tooling
### For Teams
- ๐ฏ **Consistent environments** - Same setup for everyone
- ๐ **Faster onboarding** - New team members productive in minutes
- ๐ฅ **Better collaboration** - Shared tooling and standards
- ๐ **Security by default** - Built-in security scanning
### For Projects
- ๐๏ธ **Production-ready** - Follows industry best practices
- ๐ **Scalable foundation** - Easy to extend and customize
- ๐งช **Testing-friendly** - Set up for comprehensive testing
- ๐ฆ **Deployment-ready** - Container-based development to production
- ๐ณ **Multi-service ready** - Easy integration with databases, caches, and other services
## ๐ License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## ๐ Acknowledgments
- **UV Team** for the amazing package manager
- **Astral** for Ruff and the excellent Python tooling
- **Microsoft** for VS Code and DevContainers
- **Docker** for containerization technology
- **Python Community** for the incredible ecosystem
---
**[โฌ Back to Top](#-python-devcontainer-template)**
Made with โค๏ธ for the Python community