dockerfile-backend
Multi-stage Dockerfiles for Python/FastAPI backends — builder vs runtime, slim vs alpine, system deps, layer caching, non-root users.
Skill by ajyadav013
Install
Works with Claude Code, Claude apps, Codex, Cursor, Gemini CLI, GitHub Copilot, VS Code, OpenCode, Goose, Windsurf. Read the repository before you give any tool access to your data.
npx agentmarket install skill/ajyadav013-dockerfile-backend
Asks which of your tools to add it to (Claude Code, Codex, Cursor, VS Code, Windsurf, Gemini CLI, Claude Desktop) and shows the change first. See https://agentmart.dev/cli.
Get the weekly diff
The weekly diff: one email a week with new agent tools, notable updates and grade changes, plus security alerts for tools you follow. Double opt-in, one-click unsubscribe.
About
Use cases: backend, agent-building
Reviews
Write a reviewNo reviews yet. Installed it? Reviews come from verified installers only. How reviews work
Permissions
What this skill can do and what it asks for, from its SKILL.md and code. Checked Oct 26, 2026.
- Tools
- No tools found
- Secrets
- None asked for
- Install scripts
- None found
Network hosts it talks to 1
codeload.github.com· found in install command
Files it touches
~/.claude/skills/dockerfile-backend
What's inside
- Skill name
dockerfile-backend- Description
- Multi-stage Dockerfiles for Python/FastAPI backends — builder vs runtime, slim vs alpine, system deps, layer caching, non-root users. Use when containerizing or optimizing Python backend images.
- License
- MIT
- Instructions
- 363 lines in SKILL.md · view on GitHub
Before installing
- Instructions call the network
Files (4)
- SKILL.md
- backend-dockerfile-anatomy.md · references/
- repo-evidence.md · references/
- system-deps-and-caching.md · references/
SKILL.md (first lines)
# Dockerfile for Python Backend Services Multi-stage Dockerfile patterns for Python/FastAPI backend services, covering base image selection, system dependencies, layer caching, security hardening, and multi-mode entrypoints. ## When to use - Containerizing Python backend services (FastAPI, Flask, Django) - Optimizing Docker build times through effective layer caching - Minimizing production image sizes with multi-stage builds - Installing system dependencies for Kafka clients, PostgreSQL, or Kerberos authentication - Implementing secure container images with non-root users - Setting up multi-mode entrypoints (server/consumer/worker/cron from one image) - Debugging production Dockerfile build failures or runtime issues ## Core conventions ### Base Image Selection: Slim vs Alpine **Python slim (Debian-based)**: Use `python:3.11-slim` or `python:3.12-slim` for broader compatibility with compiled extensions (psycopg2, cryptography, numpy). Larger base (~150MB) but fewer build issues. **Python alpine**: Use `python:3.10-alpine3.20` or `python:3.11-alpine` for minimal footprint (~50MB). Requires more build dependencies (g++, musl-dev, libffi-dev) and may have compatibility issues with some wheels. **Recommendation**: Start with slim for faster development; switch to alpine only if image size is critical and you've verified all dependencies build correctly. ### Multi-Stage Builds: Builder + Runtime **Builder stage**: Install all build tools, compile dependencies, create virtual environment. This stage can be large. **Runtime stage**: Copy only the compiled venv and application code. Install minimal runtime-only dependencies. **Why**: Keeps the final image small by excluding build tools (gcc, g++, git, build-essential) that are only needed during pip install. **Key pattern**: Use `COPY --from=builder /opt/venv /opt/venv` to transfer the virtual environment from builder to runtime. ### System Dependencies for Python Backend Stacks **PostgreSQL client**: - Slim: `libpq-dev` (build), `libpq5` (runtime) - Alpine: `postgresql-dev` (build), `libpq` (runtime) **Kafka (confluent-kafka, kafka-python with librdkafka)**: - Slim: `librdkafka-dev` (build + runtime), plus `libssl-dev zlib1g-dev liblz4-dev libzstd-dev` for compression - Alpine: `librdkafka-dev` (build + runtime) **Kerberos (for GSSAPI/Kafka SASL_GSSAPI)**: - Slim: `libkrb5-dev krb5-user` (build), `krb5-user` (runtime) - Alpine: `krb5-dev` (build), `krb5-libs` (runtime) **Common build dependencies**: - Slim: `build-essential gcc g++ make python3-dev libffi-dev libssl-dev pkg-config` - Alpine: `gcc g++ make musl-dev libffi-dev openssl-dev` **Always clean up**: `rm -rf /var/lib/apt/lists/*` (slim) or `rm -rf /var/cache/apk/*` (alpine) after installing to reduce layer size. ### Dependency Caching and Layer Optimization **Copy dependency manifest FIRST**: `COPY requirements.txt .` or `COPY pyproject.toml .` before `COPY . .`. Docker caches layers; if only application code changes, dependencies won't reinstall. **For requirements.txt projects**: ```dockerfile COPY requirements/requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY . . ``` **For pyproject.toml projects**: ```dockerfile COPY pyproject.toml . RUN pip install --no-cache-dir ".[prod]" COPY . . ``` **For Poetry projects**: ```dockerfile COPY pyproject.toml poetry.lock ./ RUN poetry install --no-dev --no-root COPY . . RUN poetry install --no-dev # Install the project itself ``` **Use `--no-cache-dir`**: Skips pip's download cache, reducing image size by ~100-200MB. ### Virtual Environments in Docker **Builder pattern**: Create venv in `/opt/venv`, activate it in builder, then copy to runtime stage. **Why use venv in Docker**: Isolates dependencies from system Python; makes COPY from builder to runtime stage clean. **Activation pattern**: ```dockerfile RUN python -m venv /opt/venv ENV VIRTUAL_ENV=/opt/venv ENV PATH="$VIRTUAL_ENV/bin:$PATH" ``` **Alternative**: Skip venv and install directly to system Python if using single-stage builds for simpler services. ### Security Hardening **Non-root user**: Create and use a non-root user for running the application (especially important for k8s/Cloud Run production deployments). **Remove .git directory**: After capturing commit SHA for versioning, remove .git to avoid leaking history/secrets: `RUN git rev-parse HEAD > gitsha && rm -rf .git` **Set `PYTHONUNBUFFERED=1`**: Forces Python stdout/stderr to be unbuffered; critical for seeing logs in Docker/k8s. **Set `PYTHONDONTWRITEBYTECODE=1`**: Prevents Python from writing .pyc files, reducing image size and avoiding permission issues. **Use specific image tags**: Never use `:latest` in production; pin to `python:3.11-slim-bookworm` or `python:3.10.15-alpine3.20`. ### Multi-Mode Entrypoint Pattern **Single image, multiple roles**: Use `entrypoint.py` that reads `MODE` env var and dispatches to different main functions (server, consumer, worker, cron). **Why**: Deploy the same Docker image to multiple services by varying only env vars; simplifies CI/CD (one build, many deploys). **Standard modes**: `server` → FastAPI/uvicorn/gunicorn, `consumer` → Kafka consumer, `worker` or `temporal_worker` → background task worker, `cron` → scheduled job. **See the broader containerization-and-deployment skill for full entrypoint.py patterns.**
From ajyadav013/claude-kit, MIT.
More from ajyadav013/claude-kit
- 0.602score
design-patterns-and-conventions
ajyadav013
Cross-cutting design patterns, naming, folder layout, and anti-patterns for production Python/FastAPI backends.
SkillBFreshness: B★ 14Claude Code +9 - 0.602score
auth-and-rbac
ajyadav013
FastAPI auth patterns — session/JWT chains, argon2id hashing, pyotp TOTP/Email OTP, role-based access control, x-user-data gateway forwarding.
SkillBFreshness: B★ 14Claude Code +9 - 0.602score
backend-repo-architecture
ajyadav013
Canonical backend repository structure for Python/FastAPI — multi-mode entrypoint, app factory, ConnectionManager, BaseDao, flat vs versioned layout.
SkillBFreshness: B★ 14Claude Code +9 - 0.602score
data-engineering-bigquery-gcs
ajyadav013
BigQuery medallion architecture (bronze/silver/gold), parameterized queries, MERGE upserts, partitioning, CDC dedup, Temporal orchestration.
SkillBFreshness: B★ 14Claude Code +9 - 0.602score
api-pagination-filtering-sorting
ajyadav013
HTTP query parameter conventions for pagination, filtering, sorting, and search with standardized response metadata.
SkillBFreshness: B★ 14Claude Code +9 - 0.602score
async-python-patterns
ajyadav013
Async Python for high-performance services.
SkillBFreshness: B★ 14Claude Code +9 - 0.602score
code-simplification
ajyadav013
Simplifies/refactors working code for clarity without changing behavior.
SkillBFreshness: B★ 14Claude Code +9 - 0.602score
anthropic-vertex-integration
ajyadav013
Claude on Vertex AI via the AnthropicVertex SDK — lazy ADC client init, env-based project/region resolution, async helpers, backoff retry.
SkillBFreshness: B★ 14Claude Code +9 - 0.602score
docker-shared
ajyadav013
Shared Docker building blocks — base images for private registries, .dockerignore conventions, compose fragment reuse with YAML anchors.
SkillBFreshness: B★ 14Claude Code +9 - 0.602score
cron-and-scheduled-jobs
ajyadav013
Scheduled jobs across two mechanisms — Kubernetes CronJobs (schedule in Helm values, MODE=cron) and Temporal Schedules.
SkillBFreshness: B★ 14Claude Code +9 - 0.602score
api-and-interface-design
ajyadav013
Stable API and interface design patterns.
SkillBFreshness: B★ 14Claude Code +9 - 0.602score
distributed-systems-patterns
ajyadav013
Distributed data placement and replication — consistent hashing/vnodes, sharding, N/W/R quorums, KV-store and cache anatomy, gossip failure detection, B-tree…
SkillBFreshness: B★ 14Claude Code +9
Signals
- Stars
- 14
- Forks
- 5
- Open issues
- 9
- Last commit (this folder)
- Aug 22, 2026 (2 months ago)
- Last release
- Sep 7, 2026 (1 month ago)
From GitHub (fetched Oct 24, 2026).
Details
- License
- MIT
- Repository
- github.com/ajyadav013/claude-kit
- First seen
- Oct 24, 2026
- Last checked
- Oct 24, 2026
- Source
- GitHubgithub-graphql
More from ajyadav013
- 0.602score
anthropic-vertex-integration
ajyadav013
Claude on Vertex AI via the AnthropicVertex SDK — lazy ADC client init, env-based project/region resolution, async helpers, backoff retry.
SkillBFreshness: B★ 14Claude Code +9 - 0.602score
api-and-interface-design
ajyadav013
Stable API and interface design patterns.
SkillBFreshness: B★ 14Claude Code +9 - 0.602score
code-simplification
ajyadav013
Simplifies/refactors working code for clarity without changing behavior.
SkillBFreshness: B★ 14Claude Code +9 - 0.602score
cron-and-scheduled-jobs
ajyadav013
Scheduled jobs across two mechanisms — Kubernetes CronJobs (schedule in Helm values, MODE=cron) and Temporal Schedules.
SkillBFreshness: B★ 14Claude Code +9 - 0.602score
docker-shared
ajyadav013
Shared Docker building blocks — base images for private registries, .dockerignore conventions, compose fragment reuse with YAML anchors.
SkillBFreshness: B★ 14Claude Code +9 - 0.602score
api-pagination-filtering-sorting
ajyadav013
HTTP query parameter conventions for pagination, filtering, sorting, and search with standardized response metadata.
SkillBFreshness: B★ 14Claude Code +9
Cite this page
dockerfile-backend skill. Agent Mart. https://agentmart.dev/skills/ajyadav013-dockerfile-backend (last updated 2026-10-24)