This example demonstrates building a CLI application using Poetry for dependency management.
- ✅ Using Poetry for dependency management
- ✅ Building a CLI tool with Click
- ✅ Exporting requirements from Poetry for Docker optimization
- ✅ Clean separation between dev and production dependencies
poetry-cli/
├── Dockerfile
├── pyproject.toml
├── poetry.lock
└── cli.py
docker build -t poetry-cli-example .# Show help
docker run --rm poetry-cli-example --help
# Greet someone
docker run --rm poetry-cli-example greet "World"
# Show version
docker run --rm poetry-cli-example --version
# Count words in text
docker run --rm poetry-cli-example count "Hello world from Python"# Export Poetry dependencies to requirements.txt format
RUN poetry export --without-hashes --format=requirements.txt > requirements.txtThis approach:
- Uses Poetry for local development
- Exports to requirements.txt for Docker (faster, more compatible)
- Avoids installing Poetry in the final image
# If you prefer to use Poetry directly in the container:
RUN poetry config virtualenvs.create false && \
poetry install --only main --no-root- Lock file ensures reproducible builds
- Easy dependency management (
poetry add requests) - Separates dev dependencies from production
- Better dependency resolution
- Faster installation with uv
- Smaller image (no Poetry needed)
- Standard format for CI/CD
- Better layer caching
# Install dependencies
poetry install
# Add new dependency
poetry add requests
# Run locally
poetry run python cli.py greet "Local Dev"
# Update dependencies
poetry updateEdit cli.py and add new Click commands:
@click.command()
def mycommand():
click.echo("My new command!")
cli.add_command(mycommand)Update pyproject.toml:
[tool.poetry.dependencies]
python = "^3.11"Then change Dockerfile:
FROM ghcr.io/jski/python-container-builder:3.11 as build-venv
FROM gcr.io/distroless/python3-debian12- Commit poetry.lock - Ensures reproducible builds
- Use --without-hashes - Faster builds, still secure with lock file
- Separate dev dependencies - Keep production images lean
- Export for Docker - Faster than installing Poetry in container