mirror of
https://github.com/hanzoai/skills.git
synced 2026-11-19 00:47:36 +00:00
Every page now loads /webmcp.js, scripts/webmcp.ts with its types stripped (npm run webmcp). Where the browser has document.modelContext it registers two read-only tools: search_skills over the published skills index, and get_skill, which returns a skill's markdown checked against the index digest. The markdown alternate link moves from build.py to site.py, so CI holds every page to it. The API catalog gains the site itself (llms.txt and the skills index) and hanzo.ai/agents; the live discovery check now fetches every catalog link. tests/site.mjs drives the tools through a recording modelContext, and checks a browser without one loads clean.
713 lines
32 KiB
HTML
713 lines
32 KiB
HTML
<!doctype html>
|
|
<html lang="en"><head>
|
|
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
|
<title>containers-container-security</title>
|
|
<meta name="color-scheme" content="dark">
|
|
<style>
|
|
/* Committed to one visual world: a dark instrument panel. No light variant —
|
|
the dot field and the hairline rules only read on black, and a naive
|
|
inversion would wash both out. Every colour is painted explicitly so the page
|
|
never borrows a host background. */
|
|
@font-face{font-family:'Zen';src:url('/fonts/Zen-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
|
|
@font-face{font-family:'Zen';src:url('/fonts/Zen-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
|
|
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
|
|
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
|
|
@font-face{font-family:'Zen Pixel Square';src:url('/fonts/ZenPixel-Square.woff2') format('woff2');font-style:normal;font-display:swap}
|
|
|
|
:root{
|
|
--ink:#f4f4f2; --soft:#9c9c98; --faint:#6a6a66; --ground:#000; --panel:#0a0a0a;
|
|
--line:#232323; --line-hi:#3a3a3a; --accent:#f4f4f2;
|
|
--font-zen-sans:'Zen',-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif;
|
|
--font-zen-mono:'Zen Mono',ui-monospace,"SF Mono",SFMono-Regular,Menlo,Consolas,monospace;
|
|
--font-zen-pixel-square:'Zen Pixel Square',monospace;
|
|
--mono:var(--font-zen-mono);
|
|
--sans:var(--font-zen-sans);
|
|
/* Controls are pills and surfaces are soft, as on hanzo.ai. */
|
|
--r:999px; --r-soft:14px;
|
|
}
|
|
*{box-sizing:border-box}
|
|
html{-webkit-text-size-adjust:100%}
|
|
body{margin:0;background:var(--ground);color:var(--ink);font-family:var(--sans);
|
|
font-size:16px;line-height:1.65;-webkit-font-smoothing:antialiased}
|
|
.wrap{max-width:60rem;margin:0 auto;padding:0 1.75rem}
|
|
|
|
/* The dot field. A halftone made of one repeating radial-gradient, faded out
|
|
along the diagonal by a mask so it reads as a wave rather than wallpaper. */
|
|
/* No overflow clip here: the dot field is inset to the box already, and a clip
|
|
would cut off the copy-prompt menu. */
|
|
.hero{position:relative;border-bottom:1px solid var(--line)}
|
|
.hero::before{content:"";position:absolute;inset:0;
|
|
background-image:radial-gradient(var(--line-hi) 1px, transparent 1px);
|
|
background-size:7px 7px;
|
|
-webkit-mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
|
|
mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
|
|
pointer-events:none}
|
|
.hero .wrap{position:relative;padding-top:5.5rem;padding-bottom:4rem}
|
|
|
|
nav{border-bottom:1px solid var(--line)}
|
|
nav .in{max-width:60rem;margin:0 auto;padding:1.1rem 1.75rem;display:flex;
|
|
justify-content:space-between;align-items:center;gap:1rem}
|
|
.brand{font-family:var(--mono);font-size:.85rem;letter-spacing:.14em;
|
|
text-transform:uppercase;color:var(--ink);text-decoration:none;
|
|
display:inline-flex;align-items:center;gap:.6rem}
|
|
.brand i{font-style:normal;color:var(--faint)}
|
|
.mark{width:1.15em;height:1.15em;display:block;flex:0 0 auto}
|
|
nav a.up{font-family:var(--mono);font-size:.72rem;letter-spacing:.14em;
|
|
text-transform:uppercase;color:var(--soft);text-decoration:none}
|
|
nav a.up:hover{color:var(--ink)}
|
|
|
|
h1{font-family:var(--mono);font-size:clamp(2rem,5.4vw,3.4rem);line-height:1.12;
|
|
letter-spacing:-.02em;margin:0 0 1.1rem;font-weight:500;text-wrap:balance}
|
|
h1 em{font-style:normal;color:var(--faint)}
|
|
.lede{color:var(--soft);font-size:1.05rem;max-width:52ch;margin:0 0 2.25rem}
|
|
|
|
.btns{display:flex;flex-wrap:wrap;gap:.7rem;margin:0 0 .5rem;align-items:center}
|
|
.btn{display:inline-flex;align-items:center;gap:.5rem;font-family:var(--mono);
|
|
font-size:.72rem;letter-spacing:.14em;text-transform:uppercase;
|
|
padding:.8rem 1.4rem;border:1px solid var(--line-hi);border-radius:var(--r);
|
|
color:var(--ink);text-decoration:none;background:transparent;cursor:pointer;
|
|
line-height:1.2;font-weight:400}
|
|
.btn:hover{background:#141414;text-decoration:none;border-color:var(--soft)}
|
|
.btn.solid{background:var(--ink);color:#000;border-color:var(--ink)}
|
|
.btn.solid:hover{background:#d8d8d4}
|
|
.btn .chev{width:.7em;height:.7em;flex:0 0 auto;transition:transform .15s ease}
|
|
.btn[aria-expanded="true"] .chev{transform:rotate(180deg)}
|
|
|
|
/* Copy-prompt selector. Each row copies the prompt for its own target. */
|
|
.copy{position:relative;display:inline-flex}
|
|
.menu{position:absolute;top:calc(100% + .5rem);left:0;z-index:20;min-width:21rem;
|
|
background:var(--panel);border:1px solid var(--line-hi);border-radius:var(--r-soft);
|
|
padding:.4rem;display:flex;flex-direction:column;
|
|
box-shadow:0 12px 32px rgba(0,0,0,.75)}
|
|
.menu[hidden]{display:none}
|
|
.mi{display:flex;align-items:center;gap:.7rem;width:100%;text-align:left;
|
|
padding:.62rem .8rem;border:0;border-radius:calc(var(--r-soft) - .35rem);
|
|
background:transparent;color:var(--ink);font-family:var(--mono);font-size:.76rem;
|
|
letter-spacing:.04em;text-decoration:none;cursor:pointer;line-height:1.3;
|
|
white-space:nowrap}
|
|
.mi:hover,.mi:focus-visible{background:#181818;outline:none}
|
|
.mi .mark{width:1.05rem;height:1.05rem;color:var(--soft)}
|
|
.mi:hover .mark{color:var(--ink)}
|
|
.mi u{margin-left:auto;color:var(--faint);text-decoration:none;font-size:.68rem;
|
|
letter-spacing:.1em;text-transform:uppercase}
|
|
|
|
/* Works-with bar: one scrolling row, name and mark per agent. */
|
|
.strip{border-bottom:1px solid var(--line);background:var(--panel)}
|
|
.strip .in{max-width:60rem;margin:0 auto;padding:.9rem 1.75rem;display:flex;
|
|
align-items:center;gap:1.25rem;font-family:var(--mono);font-size:.72rem;
|
|
letter-spacing:.1em;text-transform:uppercase;color:var(--faint)}
|
|
.strip b{color:var(--soft);font-weight:400;flex:0 0 auto;
|
|
border-right:1px solid var(--line);padding-right:1.25rem;align-self:stretch;
|
|
display:flex;align-items:center}
|
|
.bar{display:flex;align-items:center;gap:.5rem;flex:1 1 auto;min-width:0;
|
|
overflow-x:auto;scrollbar-width:none;scroll-snap-type:x proximity;
|
|
-webkit-overflow-scrolling:touch;
|
|
-webkit-mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent);
|
|
mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent)}
|
|
.bar::-webkit-scrollbar{display:none}
|
|
.agent{display:inline-flex;align-items:center;gap:.45rem;flex:0 0 auto;
|
|
padding:.4rem .85rem;border:1px solid var(--line);border-radius:var(--r);
|
|
color:var(--soft);white-space:nowrap;scroll-snap-align:start;
|
|
text-decoration:none;background:var(--ground)}
|
|
.agent:hover{color:var(--ink);border-color:var(--line-hi);text-decoration:none}
|
|
.agent .mark{width:.95rem;height:.95rem}
|
|
|
|
h2{font-family:var(--mono);font-size:1.1rem;letter-spacing:-.01em;margin:2.75rem 0 .75rem;font-weight:500}
|
|
h3{font-size:1rem;margin:2rem 0 .5rem;font-weight:600}
|
|
h4,h5,h6{font-size:.94rem;margin:1.5rem 0 .5rem;font-weight:600}
|
|
p{margin:0 0 1rem;max-width:70ch}
|
|
ul,ol{margin:0 0 1rem;padding-left:1.35rem;max-width:70ch}
|
|
li{margin:.22rem 0}
|
|
a{color:var(--ink);text-decoration:underline;text-underline-offset:3px;
|
|
text-decoration-color:var(--line-hi)}
|
|
a:hover{text-decoration-color:var(--ink)}
|
|
a:focus-visible{outline:1px solid var(--ink);outline-offset:3px}
|
|
code{font-family:var(--mono);font-size:.85em;background:var(--panel);
|
|
border:1px solid var(--line);border-radius:5px;padding:.1em .36em}
|
|
pre{background:var(--panel);border:1px solid var(--line);padding:1rem 1.1rem;
|
|
border-radius:var(--r-soft);overflow-x:auto;margin:0 0 1.25rem}
|
|
pre code{background:none;border:0;padding:0;font-size:.82rem;line-height:1.7}
|
|
blockquote{margin:0 0 1rem;padding-left:1rem;border-left:1px solid var(--line-hi);color:var(--soft)}
|
|
hr{border:0;border-top:1px solid var(--line);margin:2.5rem 0}
|
|
table{border-collapse:collapse;width:100%;margin:0 0 1.25rem;font-size:.92rem;
|
|
display:block;overflow-x:auto}
|
|
th,td{border:1px solid var(--line);padding:.5rem .7rem;text-align:left}
|
|
|
|
.curl{display:block;background:var(--panel);border:1px solid var(--line);
|
|
border-radius:var(--r-soft);padding:.95rem 1.1rem;font-family:var(--mono);
|
|
font-size:.8rem;color:var(--soft);overflow-x:auto;margin:0 0 2.5rem;
|
|
white-space:pre;line-height:1.9}
|
|
.curl b{color:var(--ink);font-weight:400}
|
|
.curl i{font-style:normal;color:var(--faint)}
|
|
|
|
/* index */
|
|
#q{width:100%;padding:.85rem 1.2rem;font-family:var(--mono);font-size:.85rem;color:var(--ink);
|
|
background:var(--panel);border:1px solid var(--line);border-radius:var(--r);margin:0 0 .6rem}
|
|
#q::placeholder{color:var(--faint);text-transform:uppercase;letter-spacing:.1em;font-size:.72rem}
|
|
#q:focus{outline:none;border-color:var(--soft)}
|
|
#count{color:var(--faint);font-family:var(--mono);font-size:.72rem;letter-spacing:.1em;
|
|
text-transform:uppercase;margin:0 0 2rem}
|
|
.cat{margin:2.5rem 0 .5rem;font-family:var(--mono);font-size:.7rem;letter-spacing:.16em;
|
|
text-transform:uppercase;color:var(--faint);border-bottom:1px solid var(--line);
|
|
padding-bottom:.45rem}
|
|
.row{display:block;padding:.6rem .9rem;margin:0 -.9rem;border-bottom:1px solid var(--line);
|
|
border-radius:var(--r-soft);text-decoration:none}
|
|
.row:hover{background:#0a0a0a}
|
|
.nm{font-family:var(--mono);font-size:.84rem;color:var(--ink);display:block}
|
|
.ds{color:var(--soft);font-size:.9rem;display:block;margin-top:.12rem}
|
|
footer{margin-top:4rem;padding:2rem 0 4rem;border-top:1px solid var(--line);
|
|
color:var(--faint);font-size:.85rem}
|
|
footer a{color:var(--soft)}
|
|
.fmap{display:grid;gap:1.75rem;grid-template-columns:repeat(auto-fit,minmax(11rem,1fr));
|
|
margin:0 0 2rem;font-size:.85rem}
|
|
.fmap b{display:block;font-family:var(--mono);font-size:.68rem;letter-spacing:.16em;
|
|
text-transform:uppercase;color:var(--faint);font-weight:400;margin:0 0 .6rem;
|
|
border-bottom:1px solid var(--line);padding-bottom:.4rem}
|
|
.fmap a{display:inline-block;color:var(--ink);text-decoration:none;margin:.3rem .35rem 0 0}
|
|
.fmap a:hover{text-decoration:underline}
|
|
.fmap div{color:var(--faint);line-height:1.5}
|
|
.fend{border-top:1px solid var(--line);padding-top:1.25rem;font-size:.82rem}
|
|
|
|
@media (max-width:640px){
|
|
.hero .wrap{padding-top:3.25rem;padding-bottom:2.75rem}
|
|
.btns{gap:.5rem}
|
|
.btn{padding:.75rem 1.15rem;font-size:.68rem}
|
|
.copy,.copy .btn{width:100%;justify-content:center}
|
|
.menu{left:0;right:0;min-width:0}
|
|
.strip .in{flex-direction:column;align-items:flex-start;gap:.6rem;padding:.9rem 1.75rem}
|
|
.strip b{border-right:0;padding-right:0}
|
|
.bar{width:100%}
|
|
}
|
|
</style>
|
|
<link rel="icon" href="/favicon.svg">
|
|
<link rel="canonical" href="https://hanzoskills.com/containers-container-security">
|
|
<link rel="alternate" type="text/markdown" href="/containers-container-security.md">
|
|
<script type="module" src="/webmcp.js"></script>
|
|
</head>
|
|
<!-- Reading this with a machine? Every page here is also plain markdown.
|
|
This one: https://hanzoskills.com/containers-container-security.md
|
|
Index: https://hanzoskills.com/llms.txt
|
|
Corpus: https://hanzoskills.com/all.md
|
|
Append .md to any path for the markdown twin. -->
|
|
<body>
|
|
<nav><div class="in">
|
|
<a class="brand" href="/"><svg class="mark" viewBox="0 0 67 67" fill="currentColor" aria-hidden="true"><path d="M22.21 67V44.6369H0V67H22.21Z"/><path d="M66.7038 22.3184H22.2534L0.0878906 44.6367H44.4634L66.7038 22.3184Z"/><path d="M22.21 0H0V22.3184H22.21V0Z"/><path d="M66.7198 0H44.5098V22.3184H66.7198V0Z"/><path d="M66.7198 67V44.6369H44.5098V67H66.7198Z"/></svg> HANZO<i>SKILLS</i></a>
|
|
<a class="up" href="/">All skills</a>
|
|
</div></nav>
|
|
|
|
<div class="wrap">
|
|
<h1>containers-container-security</h1><p class="lede">Hardening container images for production</p><h1>Container Security</h1>
|
|
<p><strong>Scope</strong>: USER directive, distroless images, vulnerability scanning, secrets management <strong>Lines</strong>: ~280 <strong>Last Updated</strong>: 2025-10-18</p>
|
|
<h2>When to Use This Skill</h2>
|
|
<p>Activate this skill when:</p>
|
|
<ul><li>Hardening container images for production</li><li>Scanning images for vulnerabilities</li><li>Managing secrets securely in containers</li><li>Implementing least privilege access</li><li>Choosing secure base images</li><li>Auditing container security posture</li><li>Complying with security policies</li><li>Preventing container breakout attacks</li></ul>
|
|
<h2>Core Concepts</h2>
|
|
<h3>Container Security Principles</h3>
|
|
<p><strong>Defense in depth</strong>: Layer multiple security controls.</p>
|
|
<p><strong>Key principles</strong>:</p>
|
|
<ul><li><strong>Least privilege</strong>: Run as non-root, minimal permissions</li><li><strong>Minimal attack surface</strong>: Smallest possible image</li><li><strong>Immutability</strong>: Containers are read-only</li><li><strong>Secrets management</strong>: Never hardcode credentials</li><li><strong>Vulnerability scanning</strong>: Regular image audits</li><li><strong>Runtime security</strong>: Monitor container behavior</li></ul>
|
|
<p><strong>Threat model</strong>:</p>
|
|
<pre><code>Attack Vector → Mitigation
|
|
─────────────────────────────────────────────
|
|
Vulnerable dependencies → Scan images (Trivy)
|
|
Running as root → USER directive
|
|
Excessive permissions → Drop capabilities
|
|
Hardcoded secrets → Secrets management
|
|
Large attack surface → Distroless/minimal images
|
|
Outdated base images → Pin + update regularly</code></pre>
|
|
<hr>
|
|
<h2>Running as Non-Root</h2>
|
|
<h3>Why Non-Root?</h3>
|
|
<p><strong>Problem</strong>: Default containers run as <code>root</code> (UID 0).</p>
|
|
<p><strong>Risks</strong>:</p>
|
|
<ul><li>Container escape → root on host</li><li>File system modifications</li><li>Network configuration changes</li><li>Process manipulation</li></ul>
|
|
<p><strong>Solution</strong>: Run as unprivileged user.</p>
|
|
<h3>Creating Non-Root User</h3>
|
|
<p><strong>Method 1: Create user in Dockerfile</strong>:</p>
|
|
<pre><code class="lang-dockerfile">FROM python:3.11-slim
|
|
|
|
# Create user
|
|
RUN useradd -m -u 1000 appuser
|
|
|
|
WORKDIR /app
|
|
|
|
# Install dependencies as root
|
|
COPY requirements.txt .
|
|
RUN pip install --no-cache-dir -r requirements.txt
|
|
|
|
# Copy app and change ownership
|
|
COPY . .
|
|
RUN chown -R appuser:appuser /app
|
|
|
|
# Switch to non-root user
|
|
USER appuser
|
|
|
|
CMD ["python", "app.py"]</code></pre>
|
|
<p><strong>Method 2: Use existing user</strong>:</p>
|
|
<pre><code class="lang-dockerfile">FROM node:20-slim
|
|
|
|
WORKDIR /app
|
|
|
|
# Install dependencies
|
|
COPY package.json .
|
|
RUN npm install
|
|
|
|
COPY . .
|
|
|
|
# Use existing 'node' user (UID 1000)
|
|
USER node
|
|
|
|
CMD ["node", "index.js"]</code></pre>
|
|
<p><strong>Method 3: Numeric UID (more portable)</strong>:</p>
|
|
<pre><code class="lang-dockerfile">FROM alpine:3.19
|
|
|
|
RUN adduser -D -u 1000 appuser
|
|
|
|
USER 1000 # Numeric UID (works even if username changes)
|
|
|
|
CMD ["./app"]</code></pre>
|
|
<h3>Fixing Permission Issues</h3>
|
|
<p><strong>Problem</strong>: Non-root user can't write to directories.</p>
|
|
<p><strong>Solution</strong>: Change ownership or use mounted volumes correctly.</p>
|
|
<pre><code class="lang-dockerfile">FROM python:3.11-slim
|
|
|
|
RUN useradd -m appuser
|
|
|
|
WORKDIR /app
|
|
|
|
COPY --chown=appuser:appuser . .
|
|
|
|
RUN mkdir /app/logs && chown appuser:appuser /app/logs
|
|
|
|
USER appuser
|
|
|
|
CMD ["python", "app.py"]</code></pre>
|
|
<p><strong>For volumes</strong>:</p>
|
|
<pre><code class="lang-yaml"># docker-compose.yml
|
|
services:
|
|
app:
|
|
user: "${UID}:${GID}" # Match host user
|
|
volumes:
|
|
- ./data:/app/data</code></pre>
|
|
<hr>
|
|
<h2>Minimal Base Images</h2>
|
|
<h3>Image Security Comparison</h3>
|
|
<p>| Base Image | Size | Shell | Package Manager | CVEs (Typical) | |------------|------|-------|-----------------|----------------| | <code>ubuntu:22.04</code> | ~77MB | Yes | apt | High | | <code>python:3.11</code> | ~1GB | Yes | apt | High | | <code>python:3.11-slim</code> | ~180MB | Yes | apt | Medium | | <code>alpine:3.19</code> | ~7MB | Yes | apk | Low | | <code>distroless/python3</code> | ~50MB | No | No | Very Low | | <code>scratch</code> | 0MB | No | No | None |</p>
|
|
<h3>Distroless Images</h3>
|
|
<p><strong>Purpose</strong>: Runtime-only images (no shell, no package manager).</p>
|
|
<p><strong>Benefits</strong>:</p>
|
|
<ul><li><strong>Smaller attack surface</strong>: No shell to exploit</li><li><strong>Fewer CVEs</strong>: Minimal dependencies</li><li><strong>Smaller size</strong>: Only runtime libraries</li><li><strong>Immutable</strong>: Can't install malware at runtime</li></ul>
|
|
<p><strong>Available variants</strong>:</p>
|
|
<pre><code>gcr.io/distroless/static-debian12 # Static binaries (Go, Rust)
|
|
gcr.io/distroless/base-debian12 # Requires glibc
|
|
gcr.io/distroless/python3-debian12 # Python runtime
|
|
gcr.io/distroless/nodejs20-debian12 # Node.js runtime
|
|
gcr.io/distroless/java17-debian12 # Java runtime</code></pre>
|
|
<h3>Using Distroless (Go Example)</h3>
|
|
<pre><code class="lang-dockerfile"># Build stage
|
|
FROM golang:1.21 AS builder
|
|
WORKDIR /app
|
|
COPY . .
|
|
RUN CGO_ENABLED=0 GOOS=linux go build -o app
|
|
|
|
# Runtime stage with distroless
|
|
FROM gcr.io/distroless/static-debian12
|
|
|
|
COPY --from=builder /app/app /app
|
|
|
|
USER nonroot:nonroot # Built-in non-root user (UID 65532)
|
|
|
|
ENTRYPOINT ["/app"]</code></pre>
|
|
<p><strong>Debugging distroless</strong> (no shell):</p>
|
|
<pre><code class="lang-dockerfile"># Use debug variant for troubleshooting
|
|
FROM gcr.io/distroless/static-debian12:debug
|
|
|
|
# Includes busybox shell for debugging</code></pre>
|
|
<pre><code class="lang-bash"># Exec with shell in debug variant
|
|
docker exec -it container /busybox/sh</code></pre>
|
|
<h3>Using Distroless (Python Example)</h3>
|
|
<pre><code class="lang-dockerfile"># Build stage
|
|
FROM python:3.11-slim AS builder
|
|
WORKDIR /app
|
|
COPY requirements.txt .
|
|
RUN pip install --user --no-cache-dir -r requirements.txt
|
|
|
|
# Runtime stage
|
|
FROM gcr.io/distroless/python3-debian12
|
|
|
|
COPY --from=builder /root/.local /root/.local
|
|
COPY . /app
|
|
|
|
WORKDIR /app
|
|
ENV PATH=/root/.local/bin:$PATH
|
|
|
|
USER nonroot:nonroot
|
|
|
|
CMD ["app.py"]</code></pre>
|
|
<hr>
|
|
<h2>Vulnerability Scanning</h2>
|
|
<h3>Tool: Trivy (Recommended)</h3>
|
|
<p><strong>Install</strong>:</p>
|
|
<pre><code class="lang-bash"># Mac
|
|
brew install trivy
|
|
|
|
# Linux
|
|
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | sudo apt-key add -
|
|
echo "deb https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main" | sudo tee -a /etc/apt/sources.list.d/trivy.list
|
|
sudo apt-get update
|
|
sudo apt-get install trivy</code></pre>
|
|
<p><strong>Scan image</strong>:</p>
|
|
<pre><code class="lang-bash"># Scan for all vulnerabilities
|
|
trivy image python:3.11
|
|
|
|
# Only high/critical
|
|
trivy image --severity HIGH,CRITICAL python:3.11
|
|
|
|
# Scan local Dockerfile
|
|
trivy config Dockerfile
|
|
|
|
# Scan filesystem
|
|
trivy fs .
|
|
|
|
# Output as JSON
|
|
trivy image -f json -o results.json myapp:latest</code></pre>
|
|
<p><strong>Example output</strong>:</p>
|
|
<pre><code>myapp:latest (debian 12.0)
|
|
===========================
|
|
Total: 45 (HIGH: 12, CRITICAL: 3)
|
|
|
|
┌────────────────┬─────────────┬──────────┬───────────────────┬───────────────┐
|
|
│ Library │ Vulnerability│ Severity │ Installed Version │ Fixed Version │
|
|
├────────────────┼─────────────┼──────────┼───────────────────┼───────────────┤
|
|
│ openssl │ CVE-2023-123│ CRITICAL │ 1.1.1n │ 1.1.1w │
|
|
│ curl │ CVE-2023-456│ HIGH │ 7.68.0 │ 7.88.1 │
|
|
└────────────────┴─────────────┴──────────┴───────────────────┴───────────────┘</code></pre>
|
|
<h3>Fixing Vulnerabilities</h3>
|
|
<p><strong>Step 1: Update base image</strong>:</p>
|
|
<pre><code class="lang-dockerfile"># ❌ Old vulnerable image
|
|
FROM python:3.11
|
|
|
|
# ✅ Latest patched image
|
|
FROM python:3.11-slim-bookworm</code></pre>
|
|
<p><strong>Step 2: Update dependencies</strong>:</p>
|
|
<pre><code class="lang-dockerfile"># Update system packages
|
|
RUN apt-get update && \
|
|
apt-get upgrade -y && \
|
|
rm -rf /var/lib/apt/lists/* # Safe: cleaning package manager cache</code></pre>
|
|
<p><strong>Step 3: Pin secure versions</strong>:</p>
|
|
<pre><code class="lang-dockerfile"># Pin specific package versions
|
|
RUN apt-get update && \
|
|
apt-get install -y \
|
|
curl=7.88.1-1 \
|
|
openssl=1.1.1w-1 && \
|
|
rm -rf /var/lib/apt/lists/* # Safe: cleaning package manager cache</code></pre>
|
|
<p><strong>Step 4: Remove unnecessary packages</strong>:</p>
|
|
<pre><code class="lang-dockerfile"># Use multi-stage build to exclude build tools
|
|
FROM python:3.11 AS builder
|
|
RUN pip install -r requirements.txt
|
|
|
|
FROM python:3.11-slim
|
|
COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages</code></pre>
|
|
<h3>CI/CD Integration</h3>
|
|
<p><strong>GitHub Actions</strong>:</p>
|
|
<pre><code class="lang-yaml">name: Security Scan
|
|
on: [push]
|
|
|
|
jobs:
|
|
scan:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- uses: actions/checkout@v3
|
|
|
|
- name: Build image
|
|
run: docker build -t myapp:latest .
|
|
|
|
- name: Run Trivy
|
|
uses: aquasecurity/trivy-action@master
|
|
with:
|
|
image-ref: myapp:latest
|
|
severity: 'CRITICAL,HIGH'
|
|
exit-code: '1' # Fail if vulnerabilities found</code></pre>
|
|
<p><strong>GitLab CI</strong>:</p>
|
|
<pre><code class="lang-yaml">security_scan:
|
|
image: docker:latest
|
|
services:
|
|
- docker:dind
|
|
script:
|
|
- docker build -t myapp:latest .
|
|
- wget -qO - https://github.com/aquasecurity/trivy/releases/download/v0.45.0/trivy_0.45.0_Linux-64bit.tar.gz | tar -xzf -
|
|
- ./trivy image --exit-code 1 --severity HIGH,CRITICAL myapp:latest</code></pre>
|
|
<hr>
|
|
<h2>Secrets Management</h2>
|
|
<h3>❌ Anti-Patterns (Never Do This)</h3>
|
|
<p><strong>Hardcoded secrets</strong>:</p>
|
|
<pre><code class="lang-dockerfile"># ❌ NEVER hardcode secrets
|
|
ENV API_KEY=sk-1234567890abcdef
|
|
ENV DATABASE_PASSWORD=mysecretpass</code></pre>
|
|
<p><strong>Secrets in layers</strong>:</p>
|
|
<pre><code class="lang-dockerfile"># ❌ Secret stored in layer history
|
|
COPY .env /app/.env</code></pre>
|
|
<p><strong>Secrets in build args</strong>:</p>
|
|
<pre><code class="lang-dockerfile"># ❌ Build args visible in history
|
|
ARG SECRET_KEY=abc123</code></pre>
|
|
<h3>✅ Best Practices</h3>
|
|
<p><strong>Method 1: Runtime environment variables</strong>:</p>
|
|
<pre><code class="lang-bash"># Pass at runtime (not in image)
|
|
docker run -e API_KEY=secret123 myapp:latest</code></pre>
|
|
<p><strong>docker-compose.yml</strong>:</p>
|
|
<pre><code class="lang-yaml">services:
|
|
app:
|
|
environment:
|
|
- API_KEY=${API_KEY} # From host environment
|
|
env_file:
|
|
- .env # From .env file (not committed)</code></pre>
|
|
<p><strong>.env</strong> (git-ignored):</p>
|
|
<pre><code class="lang-bash">API_KEY=sk-1234567890abcdef
|
|
DATABASE_PASSWORD=mysecretpass</code></pre>
|
|
<p><strong>Method 2: Docker secrets (Swarm/Compose)</strong>:</p>
|
|
<pre><code class="lang-yaml"># docker-compose.yml
|
|
services:
|
|
app:
|
|
secrets:
|
|
- db_password
|
|
- api_key
|
|
|
|
secrets:
|
|
db_password:
|
|
file: ./secrets/db_password.txt
|
|
api_key:
|
|
external: true # From Docker secret store</code></pre>
|
|
<p><strong>Access in container</strong>:</p>
|
|
<pre><code class="lang-python"># Read from /run/secrets/db_password
|
|
with open('/run/secrets/db_password') as f:
|
|
password = f.read().strip()</code></pre>
|
|
<p><strong>Method 3: BuildKit secrets (build-time)</strong>:</p>
|
|
<pre><code class="lang-dockerfile"># Use secret during build without storing in layer
|
|
RUN --mount=type=secret,id=github_token \
|
|
git clone https://$(cat /run/secrets/github_token)@github.com/private/repo.git</code></pre>
|
|
<p><strong>Build command</strong>:</p>
|
|
<pre><code class="lang-bash">docker build --secret id=github_token,src=$HOME/.github_token .</code></pre>
|
|
<p><strong>Method 4: External secret managers</strong>:</p>
|
|
<pre><code class="lang-python"># Fetch from AWS Secrets Manager, Vault, etc.
|
|
import boto3
|
|
|
|
def get_secret(secret_name):
|
|
client = boto3.client('secretsmanager')
|
|
response = client.get_secret_value(SecretId=secret_name)
|
|
return response['SecretString']
|
|
|
|
api_key = get_secret('prod/api_key')</code></pre>
|
|
<hr>
|
|
<h2>Dropping Capabilities</h2>
|
|
<h3>What Are Capabilities?</h3>
|
|
<p><strong>Linux capabilities</strong>: Fine-grained permissions (instead of all-or-nothing root).</p>
|
|
<p><strong>Default capabilities</strong> (Docker):</p>
|
|
<pre><code>CHOWN, DAC_OVERRIDE, FOWNER, FSETID, KILL, SETGID, SETUID,
|
|
SETPCAP, NET_BIND_SERVICE, NET_RAW, SYS_CHROOT, MKNOD,
|
|
AUDIT_WRITE, SETFCAP</code></pre>
|
|
<h3>Dropping All Capabilities</h3>
|
|
<pre><code class="lang-bash"># Run with no capabilities
|
|
docker run --cap-drop=ALL myapp:latest
|
|
|
|
# Add back only needed capabilities
|
|
docker run --cap-drop=ALL --cap-add=NET_BIND_SERVICE myapp:latest</code></pre>
|
|
<p><strong>docker-compose.yml</strong>:</p>
|
|
<pre><code class="lang-yaml">services:
|
|
app:
|
|
cap_drop:
|
|
- ALL
|
|
cap_add:
|
|
- NET_BIND_SERVICE # Only if binding to port <1024</code></pre>
|
|
<p><strong>Common capabilities</strong>:</p>
|
|
<ul><li><code>NET_BIND_SERVICE</code> - Bind to ports <1024</li><li><code>NET_RAW</code> - Use RAW/PACKET sockets</li><li><code>SYS_TIME</code> - Set system time</li><li><code>SYS_ADMIN</code> - Admin operations (avoid!)</li></ul>
|
|
<hr>
|
|
<h2>Read-Only Root Filesystem</h2>
|
|
<h3>Why Read-Only?</h3>
|
|
<p><strong>Benefits</strong>:</p>
|
|
<ul><li>Prevents runtime modifications</li><li>Stops malware installation</li><li>Enforces immutability</li></ul>
|
|
<p><strong>Enable read-only</strong>:</p>
|
|
<pre><code class="lang-bash">docker run --read-only myapp:latest</code></pre>
|
|
<p><strong>docker-compose.yml</strong>:</p>
|
|
<pre><code class="lang-yaml">services:
|
|
app:
|
|
read_only: true
|
|
tmpfs:
|
|
- /tmp # Allow writes to /tmp
|
|
- /var/run</code></pre>
|
|
<p><strong>Dockerfile</strong> (explicit tmp directories):</p>
|
|
<pre><code class="lang-dockerfile">FROM python:3.11-slim
|
|
|
|
RUN useradd -m appuser
|
|
|
|
WORKDIR /app
|
|
COPY . .
|
|
|
|
# Create writable directories
|
|
RUN mkdir -p /app/tmp /app/logs && \
|
|
chown -R appuser:appuser /app/tmp /app/logs
|
|
|
|
USER appuser
|
|
|
|
CMD ["python", "app.py"]</code></pre>
|
|
<p><strong>Run with read-only + tmpfs</strong>:</p>
|
|
<pre><code class="lang-bash">docker run \
|
|
--read-only \
|
|
--tmpfs /tmp \
|
|
--tmpfs /app/logs \
|
|
myapp:latest</code></pre>
|
|
<hr>
|
|
<h2>Security Scanning Checklist</h2>
|
|
<pre><code>Image Hardening:
|
|
[ ] Run as non-root user (USER directive)
|
|
[ ] Use minimal base image (distroless/slim)
|
|
[ ] Pin base image versions (not :latest)
|
|
[ ] Remove unnecessary packages
|
|
[ ] Use multi-stage builds
|
|
|
|
Vulnerability Management:
|
|
[ ] Scan images with Trivy
|
|
[ ] Update base images regularly
|
|
[ ] Patch critical/high vulnerabilities
|
|
[ ] Integrate scanning in CI/CD
|
|
[ ] Monitor for new CVEs
|
|
|
|
Secrets:
|
|
[ ] Never hardcode secrets in Dockerfiles
|
|
[ ] Use runtime environment variables
|
|
[ ] Use Docker secrets or external secret managers
|
|
[ ] Git-ignore .env files
|
|
[ ] Use BuildKit secrets for build-time
|
|
|
|
Runtime Security:
|
|
[ ] Drop unnecessary capabilities (cap_drop)
|
|
[ ] Enable read-only root filesystem
|
|
[ ] Limit resources (CPU/memory)
|
|
[ ] Use security profiles (AppArmor/SELinux)
|
|
[ ] Enable Docker Content Trust (image signing)
|
|
|
|
Network Security:
|
|
[ ] Isolate containers with networks
|
|
[ ] Expose only necessary ports
|
|
[ ] Use TLS for inter-service communication
|
|
[ ] Implement network policies</code></pre>
|
|
<hr>
|
|
<h2>Security Best Practices</h2>
|
|
<h3>Practice 1: Regularly Update Images</h3>
|
|
<pre><code class="lang-bash"># Pull latest base image
|
|
docker pull python:3.11-slim
|
|
|
|
# Rebuild
|
|
docker build --no-cache -t myapp:latest .
|
|
|
|
# Rescan
|
|
trivy image myapp:latest</code></pre>
|
|
<h3>Practice 2: Use Image Signing</h3>
|
|
<pre><code class="lang-bash"># Enable Docker Content Trust
|
|
export DOCKER_CONTENT_TRUST=1
|
|
|
|
# Push signed image
|
|
docker push myapp:latest
|
|
|
|
# Pull (verifies signature)
|
|
docker pull myapp:latest</code></pre>
|
|
<h3>Practice 3: Limit Container Resources</h3>
|
|
<pre><code class="lang-yaml">services:
|
|
app:
|
|
deploy:
|
|
resources:
|
|
limits:
|
|
cpus: '1.0'
|
|
memory: 512M
|
|
reservations:
|
|
memory: 256M
|
|
pids_limit: 100 # Prevent fork bombs</code></pre>
|
|
<h3>Practice 4: Use Security Profiles</h3>
|
|
<p><strong>AppArmor</strong>:</p>
|
|
<pre><code class="lang-bash">docker run --security-opt apparmor=docker-default myapp:latest</code></pre>
|
|
<p><strong>Seccomp</strong> (restrict syscalls):</p>
|
|
<pre><code class="lang-bash">docker run --security-opt seccomp=/path/to/profile.json myapp:latest</code></pre>
|
|
<p><strong>Custom seccomp profile</strong>:</p>
|
|
<pre><code class="lang-json">{
|
|
"defaultAction": "SCMP_ACT_ERRNO",
|
|
"syscalls": [
|
|
{
|
|
"names": ["read", "write", "open", "close"],
|
|
"action": "SCMP_ACT_ALLOW"
|
|
}
|
|
]
|
|
}</code></pre>
|
|
<hr>
|
|
<h2>Common Security Anti-Patterns</h2>
|
|
<p>❌ <strong>Running as root</strong>: No <code>USER</code> directive ✅ Create and use non-root user</p>
|
|
<p>❌ <strong>Using :latest tag</strong>: <code>FROM python:latest</code> ✅ Pin versions: <code>FROM python:3.11-slim</code></p>
|
|
<p>❌ <strong>Hardcoding secrets</strong>: <code>ENV API_KEY=secret</code> ✅ Use runtime env vars or secret managers</p>
|
|
<p>❌ <strong>Large base images</strong>: <code>FROM ubuntu:22.04</code> ✅ Use minimal images: <code>FROM alpine</code> or distroless</p>
|
|
<p>❌ <strong>No vulnerability scanning</strong>: Deploying without scanning ✅ Integrate Trivy in CI/CD</p>
|
|
<p>❌ <strong>Excessive capabilities</strong>: Running with default caps ✅ Drop all, add only needed: <code>--cap-drop=ALL</code></p>
|
|
<p>❌ <strong>Writable filesystem</strong>: No read-only restriction ✅ Enable read-only: <code>--read-only</code></p>
|
|
<hr>
|
|
<h2>Quick Reference</h2>
|
|
<h3>Trivy Commands</h3>
|
|
<pre><code class="lang-bash"># Scan image
|
|
trivy image myapp:latest
|
|
|
|
# Only critical/high
|
|
trivy image --severity CRITICAL,HIGH myapp:latest
|
|
|
|
# Ignore unfixed
|
|
trivy image --ignore-unfixed myapp:latest
|
|
|
|
# Scan Dockerfile
|
|
trivy config Dockerfile
|
|
|
|
# Scan IaC
|
|
trivy config .</code></pre>
|
|
<h3>Security Hardening Template</h3>
|
|
<pre><code class="lang-dockerfile"># Multi-stage build
|
|
FROM python:3.11-slim AS builder
|
|
WORKDIR /app
|
|
COPY requirements.txt .
|
|
RUN pip install --user --no-cache-dir -r requirements.txt
|
|
|
|
# Minimal runtime
|
|
FROM gcr.io/distroless/python3-debian12
|
|
|
|
# Copy dependencies
|
|
COPY --from=builder /root/.local /root/.local
|
|
|
|
# Copy app
|
|
COPY --chown=nonroot:nonroot . /app
|
|
|
|
WORKDIR /app
|
|
ENV PATH=/root/.local/bin:$PATH
|
|
|
|
# Non-root user
|
|
USER nonroot:nonroot
|
|
|
|
# Run app
|
|
CMD ["app.py"]</code></pre>
|
|
<hr>
|
|
<h2>Related Skills</h2>
|
|
<ul><li><code>dockerfile-optimization.md</code> - Multi-stage builds, layer caching</li><li><code>docker-compose-development.md</code> - Compose security configurations</li><li><code>container-networking.md</code> - Network isolation</li><li><code>kubernetes-security.md</code> - Pod security policies</li></ul>
|
|
<hr>
|
|
<p><strong>Last Updated</strong>: 2025-10-18 <strong>Format Version</strong>: 1.0 (Atomic)</p>
|
|
<footer>
|
|
<div class="fmap">
|
|
<div><b>Build</b>
|
|
<a href="https://hanzo.app">hanzo.app</a> app builder
|
|
<a href="https://hanzo.codes">hanzo.codes</a> code with an agent
|
|
<a href="https://hanzoskills.com">hanzoskills.com</a> what an agent reads</div>
|
|
<div><b>Run</b>
|
|
<a href="https://console.hanzo.ai">console.hanzo.ai</a> cloud console
|
|
<a href="https://api.hanzo.ai">api.hanzo.ai</a> the API</div>
|
|
<div><b>Account</b>
|
|
<a href="https://hanzo.id">hanzo.id</a> sign in
|
|
<a href="https://pay.hanzo.ai">pay.hanzo.ai</a> top up
|
|
<a href="https://billing.hanzo.ai">billing.hanzo.ai</a> invoices and usage</div>
|
|
<div><b>Company</b>
|
|
<a href="https://hanzo.ai">hanzo.ai</a> canonical
|
|
<a href="https://hanzo.ventures">hanzo.ventures</a> ventures</div>
|
|
</div>
|
|
<div class="fend"><a href="/containers-container-security.md">This page as plain text</a> · <a href="/llms.txt">Index for agents</a></div>
|
|
</footer>
|
|
</div></body></html> |