Poetry
A Poetry plugin that integrates pycontainer-build to build OCI container images directly from your Poetry projects — no Docker required.
Features
Section titled “Features”- 🐍 Native Poetry Integration - Build containers with
poetry build-container - 🚫 No Docker Required - Pure Python OCI image generation
- ⚡ Fast Builds - Efficient layer caching and reuse
- 🔧 Configuration via pyproject.toml - Use
[tool.pycontainer]section - 📦 Automatic Dependency Packaging - Includes Poetry dependencies
- 🔒 SBOM Generation - Built-in security compliance
Installation
Section titled “Installation”poetry-pycontainer depends on pycontainer-build, so the core is installed
into the same Poetry environment that loads the plugin.
After the separately released plugin is published:
poetry self add poetry-pycontainerFor source development or before plugin publication, install both packages from a repository checkout into Poetry’s own environment. The plugin accepts core 0.3.1 through 0.4.x:
poetry self add /path/to/pycontainer-buildpoetry self add /path/to/pycontainer-build/plugins/poetry-pycontainerCompatibility
Section titled “Compatibility”Python 3.11 or newer, Poetry 1.2.x through 2.x, and Cleo 2.x are supported. Versions outside these ranges, including Poetry 3.x and Cleo 3.x, are unsupported. CI exercises the latest Poetry version 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”# Build a container image from your Poetry projectpoetry build-container
# Build with a specific tagpoetry build-container --tag myapp:v1.0.0
# Build and push to registrypoetry build-container --tag ghcr.io/user/myapp:latest --pushConfiguration
Section titled “Configuration”Add configuration to your pyproject.toml:
[tool.poetry]name = "myapp"version = "1.0.0"description = "My Python application"
[tool.pycontainer]tag = "myapp:latest"base_image = "python:3.11-slim"registry = "ghcr.io/user/myapp"include_deps = truepush = false
[tool.pycontainer.env]ENV = "production"PORT = "8080"
[tool.pycontainer.labels]maintainer = "team@example.com"Commands
Section titled “Commands”poetry build-container
Section titled “poetry build-container”Build an OCI container image from your Poetry project.
Options:
--tag, -t- Container image tag (default:{name}:{version})--base-image, -b- Base container image (default:python:3.11-slim)--registry, -r- Container registry URL--push, -p- Push image to registry--include-deps, -d- Include Poetry dependencies--no-include-deps- Exclude Poetry dependencies--sbom, -s FORMAT- Generate an SBOM (spdxorcyclonedx)--verbose, -v- Verbose output--dry-run- Show what would be built without building--no-cache- Disable layer caching--image-user USER[:GROUP]- ImageUser(overrides[tool.pycontainer]user; an empty value inherits the base image’s user). Metadata only: no UID/GID mapping or ownership change Examples:
# Basic buildpoetry build-container
# Build with custom tagpoetry build-container --tag myapp:v2.0.0
# Build on different base imagepoetry build-container --base-image python:3.12-alpine
# Build with SBOMpoetry build-container --sbom spdx
# Build and push to GHCRpoetry build-container \ --tag ghcr.io/myorg/myapp:latest \ --push \ --verbose
# Dry run to previewpoetry build-container --dry-run --verboseConfiguration Reference
Section titled “Configuration Reference”All configuration is under [tool.pycontainer] in pyproject.toml:
[tool.pycontainer]# Container image tag (optional, defaults to {name}:{version})tag = "myapp:latest"
# Base container imagebase_image = "python:3.11-slim"
# Container registry URLregistry = "ghcr.io/user/myapp"
# Push image to registry after buildpush = false
# Include Poetry dependencies in the imageinclude_deps = true
# Generate SBOM: true defaults to SPDX; a string selects its formatsbom = "cyclonedx"
# Enable verbose outputverbose = false
# Dry run modedry_run = false
# Disable layer cachingno_cache = false
# Environment variables to set in the container[tool.pycontainer.env]ENV = "production"PORT = "8080"DEBUG = "false"
# Container labels[tool.pycontainer.labels]maintainer = "team@example.com"version = "1.0.0"org.opencontainers.image.source = "https://github.com/user/repo"How It Works
Section titled “How It Works”- Reads Poetry Configuration - Extracts project name, version, dependencies from
pyproject.toml - Applies Tool Configuration - Merges
[tool.pycontainer]settings - Builds Container - Uses pycontainer-build to create OCI image
- Adds Metadata - Automatically adds Poetry metadata as OCI labels
- Pushes (Optional) - Pushes to registry if
--pushis specified
Integration Examples
Section titled “Integration Examples”With GitHub Actions
Section titled “With GitHub Actions”This example uses published packages. Until they are published, install both packages from a repository checkout as described in Installation.
name: Build and Push Container
on: push: branches: [main]
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 Poetry run: pipx install poetry
- name: Install plugin run: poetry self add poetry-pycontainer
- name: Install dependencies run: poetry install
- name: Build and push container env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | poetry build-container \ --tag ghcr.io/${{ github.repository }}:${{ github.sha }} \ --push \ --verboseWith Azure Developer CLI
Section titled “With Azure Developer CLI”This example uses published packages. Until they are published, install both packages from a repository checkout as described in Installation.
name: myapp
services: api: project: ./ language: python host: containerapp hooks: prebuild: shell: sh run: | pip install poetry poetry self add poetry-pycontainer build: shell: sh run: | poetry build-container \ --tag ${SERVICE_IMAGE_NAME}:${SERVICE_IMAGE_TAG} \ --pushLocal Development
Section titled “Local Development”# Install dependenciespoetry install
# Build container locallypoetry build-container --tag myapp:dev
# Test with Docker (for validation only)docker run myapp:devAdvanced Usage
Section titled “Advanced Usage”Custom Base Images
Section titled “Custom Base Images”# Use distroless for smaller imagespoetry build-container --base-image gcr.io/distroless/python3-debian12
# Use Alpine for minimal sizepoetry build-container --base-image python:3.11-alpineMulti-Stage Configuration
Section titled “Multi-Stage Configuration”[tool.pycontainer]base_image = "python:3.11-slim"
[tool.pycontainer.env]# DevelopmentENV = "development"
# Override in CI with --env flag or environment variablesFramework Auto-Detection
Section titled “Framework Auto-Detection”pycontainer-build automatically detects and configures:
- FastAPI - Sets up uvicorn with correct host/port
- Flask - Configures Flask development server
- Django - Sets up Django runserver
No manual configuration needed!
Troubleshooting
Section titled “Troubleshooting”Plugin Not Found
Section titled “Plugin Not Found”# Ensure plugin is installedpoetry self show plugins
# Reinstall if neededpoetry self remove poetry-pycontainerpoetry self add poetry-pycontainerBuild Fails: “pycontainer-build not found”
Section titled “Build Fails: “pycontainer-build not found””Install the core package into Poetry’s plugin environment as described in the Installation section. Adding it to the application project does not install it into the environment that loads this plugin.
Dependencies Not Included
Section titled “Dependencies Not Included”Ensure include_deps = true in your config:
[tool.pycontainer]include_deps = trueRequirements
Section titled “Requirements”- Python 3.11+
- Poetry 1.2.0+
- pycontainer-build (installed automatically as dependency)
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 - The core container builder
- hatch-pycontainer - Hatch plugin for pycontainer-build
- Poetry - Python dependency management