Hatch
A Hatch plugin that integrates pycontainer-build to build OCI container images directly from your Hatch projects — no Docker required.
Features
Section titled “Features”- 🐍 Native Hatch Integration - Build containers during
hatch build - 🚫 No Docker Required - Pure Python OCI image generation
- ⚡ Fast Builds - Efficient layer caching and reuse
- 🔧 Configuration via pyproject.toml - Use
[tool.hatch.build.hooks.pycontainer]section - 📦 Automatic Dependency Packaging - Includes project dependencies
- 🔒 SBOM Generation - Built-in security compliance
Installation
Section titled “Installation”The plugin depends on pycontainer-build and hatchling. Install it in the
same Python environment as the Hatch CLI:
pip install hatch hatch-pycontainerFor source development or before the separately released plugin is published,
install both packages from a repository checkout. The plugin accepts core 0.3.1
through 0.4.x. Because Hatch builds in an isolated environment, the project’s
build-system.requires must also point at that checkout:
python -m pip install hatch[build-system]requires = [ "hatchling", "pycontainer-build @ file:///absolute/path/to/pycontainer-build", "hatch-pycontainer @ file:///absolute/path/to/pycontainer-build/plugins/hatch-pycontainer",]build-backend = "hatchling.build"Compatibility
Section titled “Compatibility”Python 3.11 or newer, Hatch 1.6.x through 1.x, and Hatchling 1.18.x through 1.x are supported. Hatch 2.x and Hatchling 2.x are unsupported. CI exercises the latest Hatch and Hatchling versions allowed by the package constraints; it does not claim coverage of every earlier release.
Quick Start
Section titled “Quick Start”Basic Usage
Section titled “Basic Usage”Add the plugin to your pyproject.toml:
[build-system]requires = ["hatchling", "hatch-pycontainer"]build-backend = "hatchling.build"
[tool.hatch.build.hooks.pycontainer]enable = trueThen build your container:
hatch buildThis will:
- Build your Python package (wheel)
- Create a container image with pycontainer-build
- Output to
dist/image/
Configuration
Section titled “Configuration”Basic Configuration
Section titled “Basic Configuration”[tool.hatch.build.hooks.pycontainer]# Container image tagtag = "myapp:latest"
# Base container imagebase-image = "python:3.11-slim"
# Include dependenciesinclude-deps = trueComplete Configuration
Section titled “Complete Configuration”[tool.hatch.build.hooks.pycontainer]# Enable/disable the hookenable = true
# Skip container build (useful for local dev)skip = false
# Container image tagtag = "myapp:v1.0.0"
# Base container imagebase-image = "python:3.11-slim"
# Container registry URLregistry = "ghcr.io/user/myapp"
# Push to registry after buildpush = false
# Include project dependenciesinclude-deps = true
# Generate SBOM: true defaults to SPDX; a string selects its formatsbom = "cyclonedx"
# Enable verbose outputverbose = true
# Disable layer cachingno-cache = false
# Preview without buildingdry-run = false
# Image User (user, uid, user:group, uid:gid, uid:group or user:gid);# unset inherits the base image's user. Metadata only.user = "1000:1000"
# Environment variables[tool.hatch.build.hooks.pycontainer.env]ENV = "production"PORT = "8080"DEBUG = "false"
# Container labels[tool.hatch.build.hooks.pycontainer.labels]maintainer = "team@example.com"org.opencontainers.image.source = "https://github.com/user/repo"Usage Examples
Section titled “Usage Examples”Build Container with Package
Section titled “Build Container with Package”# Build both wheel and containerhatch build
# The container image will be at dist/image/Build and Push
Section titled “Build and Push”[tool.hatch.build.hooks.pycontainer]tag = "ghcr.io/myorg/myapp:latest"push = true# Build and push in one commandGITHUB_TOKEN=$GITHUB_TOKEN hatch buildDevelopment vs Production
Section titled “Development vs Production”Use environment-specific configuration:
[tool.hatch.envs.default.build.hooks.pycontainer]tag = "myapp:dev"base-image = "python:3.11"push = false
[tool.hatch.envs.production.build.hooks.pycontainer]tag = "myapp:latest"base-image = "python:3.11-slim"push = truesbom = "spdx"Skip Container Build
Section titled “Skip Container Build”# Skip container build for local devhatch build -e local
# Or set in pyproject.toml[tool.hatch.envs.local.build.hooks.pycontainer]skip = trueIntegration Examples
Section titled “Integration Examples”With GitHub Actions
Section titled “With GitHub Actions”name: Build and Release
on: push: tags: - 'v*'
jobs: build: runs-on: ubuntu-latest permissions: packages: write steps: - uses: actions/checkout@v4
- uses: actions/setup-python@v5 with: python-version: '3.11'
- name: Install Hatch run: pip install hatch hatch-pycontainer
- name: Build package and container env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: hatch buildWith Azure Developer CLI
Section titled “With Azure Developer CLI”name: myapp
services: api: project: ./ language: python host: containerapp hooks: prebuild: shell: sh run: pip install hatch hatch-pycontainer build: shell: sh run: hatch buildWith CI/CD Pipeline
Section titled “With CI/CD Pipeline”build: image: python:3.11 script: - pip install hatch hatch-pycontainer - hatch build artifacts: paths: - dist/How It Works
Section titled “How It Works”- Registers Build Hook - Hatch calls the pycontainer hook during build
- Reads Configuration - Extracts settings from
pyproject.toml - Builds Container - Uses pycontainer-build to create OCI image
- Adds Metadata - Automatically adds version and build info
- Pushes (Optional) - Pushes to registry if configured
Advanced Usage
Section titled “Advanced Usage”Custom Build Targets
Section titled “Custom Build Targets”[tool.hatch.build.targets.wheel.hooks.pycontainer]tag = "myapp:wheel-build"
[tool.hatch.build.targets.sdist.hooks.pycontainer]skip = true # Don't build container for sdistMulti-Architecture
Section titled “Multi-Architecture”[tool.hatch.build.hooks.pycontainer]platform = "linux/amd64"
# Note: Full multi-arch builds coming in future releaseFramework Auto-Detection
Section titled “Framework Auto-Detection”pycontainer-build automatically detects:
- FastAPI - Configures uvicorn entrypoint
- Flask - Sets up Flask server
- Django - Configures Django runserver
Custom Base Images
Section titled “Custom Base Images”[tool.hatch.build.hooks.pycontainer]# Minimal distroless imagebase-image = "gcr.io/distroless/python3-debian12"
# Alpine for small sizebase-image = "python:3.11-alpine"
# Microsoft Azure-optimizedbase-image = "mcr.microsoft.com/python/distroless"Environment Variables
Section titled “Environment Variables”Set these during build for authentication:
# GitHub Container RegistryGITHUB_TOKEN=ghp_... hatch build
# Docker HubREGISTRY_USERNAME=myuser REGISTRY_PASSWORD=mypass hatch build
# Azure Container Registry (uses az cli)az acr login --name myregistryhatch buildTroubleshooting
Section titled “Troubleshooting”Hook Not Running
Section titled “Hook Not Running”Ensure the hook is enabled:
[tool.hatch.build.hooks.pycontainer]enable = trueskip = falsepycontainer-build Not Found
Section titled “pycontainer-build Not Found”Follow the source-install instructions in Installation to install the core into the Hatch build environment. The build-system requirements must also include both source packages until publication.
After publication, add the plugin and core packages to build requirements:
[build-system]requires = ["hatchling", "hatch-pycontainer", "pycontainer-build"]Dependencies Not Included
Section titled “Dependencies Not Included”Check configuration:
[tool.hatch.build.hooks.pycontainer]include-deps = truePush Fails
Section titled “Push Fails”Verify authentication:
# For GHCRecho $GITHUB_TOKEN | docker login ghcr.io -u USERNAME --password-stdin
# For ACRaz acr login --name myregistryComparison with Other Tools
Section titled “Comparison with Other Tools”| Feature | hatch-pycontainer | Dockerfile | Buildpacks |
|---|---|---|---|
| No Docker daemon | ✅ | ❌ | ❌ |
| Pure Python | ✅ | ❌ | ❌ |
| Hatch native | ✅ | ❌ | ❌ |
| Auto-config | ✅ | ❌ | ✅ |
| Layer caching | ✅ | ✅ | ✅ |
| SBOM generation | ✅ | ❌ | ✅ |
Requirements
Section titled “Requirements”- Python 3.11+
- Hatch 1.18.0+
- pycontainer-build (installed automatically)
License
Section titled “License”MIT License - See LICENSE file for details
Contributing
Section titled “Contributing”Contributions welcome! Please see contributor guidance for guidelines.
Related Projects
Section titled “Related Projects”- pycontainer-build - Core container builder
- poetry-pycontainer - Poetry plugin
- Hatch - Modern Python project manager