Dockerfile Doctor: harden, shrink & speed up builds

    by Ship-Safe Skills

    1

    Audits any Dockerfile with 21 checks (root user, secrets in ENV/ARG, unpinned images, cache-busting COPY order, missing multi-stage) via a tested script, then writes a corrected, production-ready Dockerfile.

    Free

    Security scanned

    Works with the AI Agents you already use

    Dockerfile Doctor: harden, shrink & speed up builds

    Example session with this skill installed

    Audit my Dockerfile. Every one-line code change triggers a full pip reinstall, the image is over 1 GB, and security flagged it. Fix it.

    FROM python:3.9
    WORKDIR /app
    COPY . .
    RUN apt-get update && apt-get install -y gcc libpq-dev
    RUN pip install -r requirements.txt
    ENV DB_PASSWORD=changeme
    EXPOSE 8000
    CMD python app.py
    
    • Read your context and instructions
    • Compiled the dockerfile doctor

    Summary

    Ran scripts/dockerfile_audit.py:

    2 HIGH, 4 MEDIUM, 3 LOW, 1 INFO. The slow builds come from COPY . . before pip install; the size from shipping gcc in a single stage; the security flags from running as root and a password baked into the image.

    Findings (real script output)

    SevRuleLineProblem
    HIGHDF0031Final image runs as root
    HIGHDF0146ENV DB_PASSWORD is stored in image history/metadata
    MEDIUMDF0181Single-stage build ships build tooling (gcc) to runtime
    MEDIUMDF0044apt install without --no-install-recommends
    MEDIUMDF0054apt lists not removed in the same layer
    MEDIUMDF0175Dependencies install after COPY . .: every code change busts the cache
    LOWDF0021Base pinned by tag, not digest
    LOWDF0085pip cache kept in image
    LOWDF0208Shell-form CMD: app won't receive SIGTERM

    Corrected Dockerfile

    
    # syntax=docker/dockerfile:1
    FROM python:3.9.20-slim AS builder
    ENV PIP_NO_CACHE_DIR=1
    WORKDIR /app
    RUN apt-get update && apt-get install -y --no-install-recommends gcc libpq-dev \
        && rm -rf /var/lib/apt/lists/*
    COPY requirements.txt .
    RUN pip wheel --wheel-dir /wheels -r requirements.txt
    
    FROM python:3.9.20-slim
    ENV PYTHONUNBUFFERED=1
    WORKDIR /app
    RUN apt-get update && apt-get install -y --no-install-recommends libpq5 \
        && rm -rf /var/lib/apt/lists/* \
        && useradd --create-home --uid 10001 app
    COPY --from=builder /wheels /wheels
    RUN pip install --no-cache-dir /wheels/* && rm -rf /wheels
    COPY --chown=app:app . .
    USER app
    EXPOSE 8000
    CMD ["python", "app.py"]
    

    What changed and why

    • Manifest copied before source: code edits no longer reinstall dependencies.
    • Multi-stage: gcc and headers stay in builder; runtime keeps only libpq5.
    • Non-root USER app: clears DF003.
    • DB_PASSWORD removed from the image: pass it at runtime (docker run -e / your orchestrator's secrets).
    • Exec-form CMD: clean shutdowns on SIGTERM.

    Behaviour changes to check

    • DB_PASSWORD must now be supplied at runtime.
    • The app runs as UID 10001: make sure it doesn't write outside /app or /tmp.

    Next steps

    1. Add a .dockerignore (.git, __pycache__, .env*, .venv).
    2. Rebuild and re-run the audit: no HIGH findings should remain.

    Connects securely to your tools. The creator never sees your data.

    About this skill

    Docker builds often result in bloated images, slow cache invalidation, and critical security holes like running as root. This skill audits your container configuration to produce hardened, multi-stage Dockerfiles optimized for speed and production safety.

    What it does

    • Security hardening identifies root user usage, leaked secrets in layers, and unpinned base images.
    • Image optimization implements multi-stage builds and slim base images to reduce total footprint.
    • Cache engineering restructures commands to ensure dependency layers only rebuild when manifests change.
    • Context analysis reviews .dockerignore and dependency files to prevent accidental sensitive data exposure.
    • Automated auditing uses specialized scripts to flag specific rule violations from DF001 to DF021.

    How it works

    1. Context gathering analyzes the Dockerfile, lockfiles, and deployment manifests to understand the runtime requirements.
    2. Audit execution runs a diagnostic script to generate a checklist of severity-rated violations.
    3. Manual verification checks for non-automatable issues like missing .dockerignore patterns or hardcoded credentials.
    4. Code generation rewrites the Dockerfile using multi-stage patterns and non-root users while preserving original runtime behavior.

    Frameworks & tools

    This skill works with standard Docker, BuildKit, and language-specific package managers including npm, pip, go modules, and maven. It supports Debian, Alpine, and Distroless base image patterns.

    Why this beats prompting it yourself

    Generic prompts often miss the specific ordering required for layer caching or fail to provide the non-root user setup needed for production. This skill uses a structured audit script and a library of proven templates to ensure no security rules are skipped.

    Use cases

    • Shrink massive production images that are slowing down CI/CD pipelines.
    • Remove root user privileges to meet SOC2 or enterprise security requirements.
    • Fix broken layer caching that forces full re-installs on every small code change.
    • Migrate legacy single-stage Dockerfiles to modern multi-stage architectures.

    Known limitations

    The skill cannot guarantee compatibility when switching C libraries, such as moving from Debian to Alpine. It requires access to dependency manifests for optimal cache layering.

    How to use

    1. 1

      Connect your agent to the Agensi MCP

      Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup

    2. 2

      Sign in to Agensi

      Your agent shows a sign-in link. Sign in and click Allow.

    3. 3

      Ask for what you need

      Your agent loads this skill and uses it on your task. It always asks before buying a skill.

    Reviews

    No reviews yet

    Be one of the first to try it. Every listed skill goes through the trust checks below.

    Security scanned

    Checked by our 8-point scan before listing

    Fresh listing

    Recently published to Agensi

    Free to use

    No account required to browse

    Trust & safety

    Security scan

    Security scan passed 14 days ago

    • Free with an Agensi account

    Software
    Python 3.8+
    Optional
    Docker (to verify image size)

    Permissions

    Terminal / Shell
    Read Files
    Write Files

    Shell runs the bundled audit script (python, standard library only). Read: your Dockerfile, .dockerignore and dependency manifests. Write: the corrected Dockerfile/.dockerignore, only after you approve. The scripts make no network calls; only the optional `docker build` verification step pulls base images.

    Listed14 days ago

    What's inside

    Frequently Asked Questions

    The skill analyzes your Dockerfile and dependency manifests (like package-lock.json or requirements.txt) to reorder layers, ensuring that time-consuming installs only re-run when dependencies actually change.

    Yes, the skill automatically generates the necessary instructions to create non-root users and set appropriate permissions, helping your containers meet SOC2 and enterprise security standards.

    It is compatible with standard Docker, BuildKit, and major package managers (npm, pip, go, maven), supporting Debian, Alpine, and Distroless base image patterns.

    The skill identifies bloated layers and implements multi-stage build patterns to separate build-time dependencies from the final runtime, significantly reducing the production image size.

    While it excels at hardening and optimization, it cannot guarantee 100% compatibility if you choose to switch underlying C libraries, such as migrating from a Debian-based image to Alpine.

    You receive the automated auditing scripts, a library of hardened production templates, and the specialized logic required to perform multi-stage rewrites and security scans.

    New in DevOps & Cloud

    Find more skills like this on the Claude Skills Marketplace, the Codex Skills Marketplace and the Gemini Skills Marketplace, or browse Claude Skills for DevOps & Cloud.

    Free