python-containers
БесплатноБез исполняемых скриптовНе проверенPython container optimization — slim images (not Alpine), virtualenv, multi-stage, pip/poetry/uv, musl gotchas (1GB to ~120MB). Use when working with Python…
от laurigates★ 61 звезда на GitHubОб этом скилле
Python Container Optimization
Expert knowledge for building optimized Python container images using slim base images, virtual environments, modern package managers (uv, poetry), and multi-stage build patterns.
When to Use This Skill
| Use this skill when... | Use container-development instead when... |
|---|---|
| Building Python-specific Dockerfiles | General multi-stage build patterns |
| Optimizing Python image sizes | Language-agnostic container security |
| Handling pip/poetry/uv in containers | Docker Compose configuration |
| Dealing with musl/glibc issues | Non-Python container optimization |
Core Expertise
Python Container Challenges:
- Large base images with unnecessary packages (~1GB)
- Critical: Alpine causes issues with Python (musl vs glibc)
- Complex dependency management (pip, poetry, pipenv, uv)
- Compiled C extensions requiring build tools
- Virtual environment handling in containers
Key Capabilities:
- Slim-based images (NOT Alpine for Python)
- Multi-stage builds with modern tools (uv recommended)
- Virtual environment optimization
- Compiled extension handling
- Non-root user configuration
Why NOT Alpine for Python
Use slim instead of Alpine for Python containers. Alpine uses musl libc which causes:
- Many wheels don't work (numpy, pandas, scipy)
- Forces compilation from source (slow builds)
- Larger final images due to build tools
- Runtime errors with native extensions
Optimized Dockerfile Pattern (uv)
The recommended pattern achieves ~80-120MB images:
# Both stages MUST share this interpreter version — see below.
ARG PYTHON_VERSION=3.11
# Build stage
FROM python:${PYTHON_VERSION}-slim AS builder
WORKDIR /app
RUN pip install --no-cache-dir uv
# Copy dependency files
COPY pyproject.toml uv.lock ./
# Install dependencies with uv (much faster than pip)
RUN uv sync --frozen --no-dev
COPY . .
# Runtime stage
FROM python:${PYTHON_VERSION}-slim
WORKDIR /app
# Install only runtime dependencies (if needed)
RUN apt-get update && \
apt-get install -y --no-install-recommends \
libpq5 \
&& rm -rf /var/lib/apt/lists/*
# Create non-root user
RUN addgroup --gid 1001 appgroup && \
adduser --uid 1001 --gid 1001 --disabled-password appuser
# Copy only what's needed
COPY --from=builder --chown=appuser:appgroup /app/.venv /app/.venv
COPY --chown=appuser:appgroup app/ /app/app/
COPY --chown=appuser:appgroup pyproject.toml /app/
ENV PATH="/app/.venv/bin:$PATH" \
PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1
USER appuser
EXPOSE 8000
HEALTHCHECK --interval=30s CMD python -c "import requests; requests.get('http://localhost:8000/health')" || exit 1
CMD ["python", "-m", "app"]
Interpreter Coupling — Both Stages, One Version
The pattern above copies a virtualenv across a stage boundary:
COPY --from=builder /app/.venv /app/.venv
That venv is not portable across Python minor versions. Its packages live in
/app/.venv/lib/python<X.Y>/site-packages, and any compiled extension inside it
carries that version's ABI tag. Run it on a different minor version and the
interpreter looks in a directory that does not exist, so the venv is dropped from
sys.path entirely — not one missing package, all of them:
runtime python : 3.14.7
venv libdirs : python3.12
sys.path : ['', '/usr/local/lib/python314.zip',
'/usr/local/lib/python3.14',
'/usr/local/lib/python3.14/lib-dynload']
The symptom is ModuleNotFoundError on the first non-stdlib import the
entrypoint reaches, which reliably sends people hunting for a missing dependency
rather than a mismatched interpreter.
Drive both FROM lines from one ARG, as the example does. Renovate expands
ARG defaults used in FROM, so the version stays updatable while the halves
cannot drift apart.
Why a bot can split the stages
Renovate matches images by name. When both stages name the same image it updates
them together; when they name different images — a builder like
ghcr.io/astral-sh/uv:python3.12-alpine next to a runtime of
python:3.12-alpine — its python rule matches only the second and moves that one
alone. Nothing in the diff shows a coupling was broken.
If you need uv in the builder, copy the binary in rather than switching base
images, so both stages stay on the same python: base:
ARG PYTHON_VERSION=3.12
FROM ghcr.io/astral-sh/uv:0.12.7 AS uv
FROM python:${PYTHON_VERSION}-slim AS build
COPY --from=uv /uv /uvx /bin/
FROM python:${PYTHON_VERSION}-slim AS runtime
COPY --from=build /app/.venv /app/.venv
Verify it, and give the check teeth
A docker build that succeeds proves the layers assembled, not that anything can
start. Run the image before publishing it, and assert the venv is on the path
rather than just importing something:
docker run --rm -i "$IMAGE" /app/.venv/bin/python
Установить python-containers в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Bash(docker build *)Bash(docker images *)Bash(docker history *)Bash(head *)ReadGrepGlobEditWriteTodoWriteWebSearchWebFetchВложенные файлы
FAQ
Что делает скилл python-containers?
Python container optimization — slim images (not Alpine), virtualenv, multi-stage, pip/poetry/uv, musl gotchas (1GB to ~120MB). Use when working with Python containers or optimizing image sizes.
Как установить скилл python-containers?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл python-containers запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
theme-factory
Toolkit for styling artifacts with a theme. These artifacts can be slides, docs, reportings, HTML landing pages, etc. There are 10 pre-set themes with…
от Anthropicaccess
Manage Discord channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
от Anthropicconfigure
Set up the Discord channel — save the bot token and review access policy. Use when the user pastes a Discord bot token, asks to configure Discord, asks "how…
от Anthropicaccess
Manage iMessage channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
от Anthropic