← ENGINEERING BLOG · AI & DOCKER SECURITY · 8 MIN READ · NOVEMBER 27, 2026

Ship FastAPI & AI Containers Without Leaking Code or Prompts.

AI startups and B2B SaaS vendors deploying FastAPI microservices into customer AWS VPCs or on-premises GPU servers face an uncomfortable reality: a standard Python Docker image is just a tarball of plaintext .py files. One docker save or kubectl exec command exposes your proprietary orchestration logic, scoring weights, and engineered system prompts.

1. Why Multi-Stage Dockerfiles & .pyc-Only Builds Don't Protect IP

A common misconception is that deleting .py files inside a Dockerfile and leaving only __pycache__/*.pyc protects a FastAPI container. On Python 3.12–3.14, neural decompilers like PyLingual and pycdc reconstruct .pyc files with near-100% fidelity—and worse, every string literal (system prompts, SQL queries, proprietary regexes, and internal endpoints) sits completely unencrypted inside co_consts, readable via plain strings.

2. Compiling FastAPI + Pydantic v2 + Uvicorn Into One Sealed ELF64 Binary

Unlike full-program C++ transpilers that struggle with Pydantic v2 Rust extensions (pydantic-core), dynamic FastAPI dependency injection, or torch C++ bindings, PyKevlar preserves 100% native CPython C-extension compatibility while sealing your first-party packages inside ChaCha20-Poly1305 function vaults and C machine code:

# Build directly on Linux CI—or cross-compile from Apple Silicon macOS with zero Docker:
pip install pykevlar
pykevlar register "PKA1-..."

pykevlar build app/main.py \
    --name inference-gateway \
    --target linux-x86_64 \
    --protect app --protect agents --protect prompts --protect pricing \
    --mix-str --enable-bcc --anti-llm --outer \
    --collect-submodules uvicorn --collect-submodules fastapi \
    -F -O dist-linux

# Verify zero plaintext .py modules or prompt strings exist in the output binary:
pykevlar verify dist-linux/inference-gateway --protect app,agents,prompts,pricing

3. Minimal Distroless Dockerfile (Zero .py, Zero pip, Zero Shell)

Once compiled, your customer-facing container copies only the single sealed ELF64 binary (and gets its hardware-bound pyarmor.rkey mounted at runtime):

FROM gcr.io/distroless/cc-debian12:nonroot
WORKDIR /app
COPY dist-linux/inference-gateway /app/inference-gateway
EXPOSE 8000
ENTRYPOINT ["/app/inference-gateway", "--host", "0.0.0.0", "--port", "8000"]

4. What an Attacker Sees Inside the Container

  • strings /app/inference-gateway: System prompts and secret constants are encrypted by --mix-str into masked C byte pools—zero prompt leakage.
  • pyinstxtractor / binwalk: Fails immediately against the SPZ2 stream-encrypted payload container.
  • GhidraMCP / Frida / PEP 669: Blocked at runtime by C-level anti-instrumentation checks and stripped symbol visibility (see our 7-Layer Threat Matrix and FastAPI & Django recipes).
Compile FastAPI into a zero-source Linux binary in under 2 seconds
Try free bundling via pip install pykevlar. Unlock --anti-llm, --mix-str & zero-Docker Linux cross-builds with Team ($79/yr).
Compare Pro & Team →
$pip install pykevlar && pykevlar bench
MORE FROM PYKEVLAR ENGINEERING
Python Hardware Lock & Offline License Expiration: Binding Binaries to Machine UUIDs Read → Shipping Commercial Django & FastAPI On-Premises Without Leaking Source Code Read → PyKevlar vs. PyArmor vs. Nuitka: Production Benchmarks on 802 Python Modules Read →