agentleFS
mchittinenimchittineni/cloud-platform-skills/.cursor/rules/skills/docker-containerization-basics.mdc

cloud-platform-skills / skills

Cursor rule1 starsChanged 3 months agoMITView on GitHub

Container image engineering: multi-stage builds, layer-cache ordering, non-root users, distroless and minimal base images, and image-size/attack-surface reduction. Use when writing or reviewing a Dockerfile, shrinking image size, fixing slow builds, or hardening containers before they reach a registry.

The file

We split this docker-containerization-basics.mdc into the rules it gives an agent

4 rules. About 0 are common, 1 is distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Layer Caching: Place commands that change rarely (package installations, dependencies) before code copy commands.

    line 60
  2. Very specific:

    Run as Non-Root: Never run container payloads as root (UID 0) unless explicitly required by low-level system daemons.

    line 61
  3. Very specific:

    Signal Handling: Ensure PID 1 handles SIGTERM and SIGINT properly.

    line 62
  4. Very specific:

    Scan Before Push: Run vulnerability scanners prior to registry push (trivy image <image-tag>).

    line 63

≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
docker-containerization-basics.mdc
---
description: "Container image engineering: multi-stage builds, layer-cache ordering, non-root users, distroless and minimal base images, and image-size/attack-surface reduction. Use when writing or reviewing a Dockerfile, shrinking image size, fixing slow builds, or hardening containers before they reach a registry."
globs:
alwaysApply: false
---

# Docker Multi-Stage Optimization & Container Hardening

## When to Use This Skill

**Triggers — load this skill when:**

- You are authoring or reviewing a Dockerfile for a production service
- Image size, build time, or layer-cache behaviour needs to improve
- A container must be hardened (non-root, read-only rootfs, minimal base) before release

**Route elsewhere when:**

- Runtime threat detection on a running container -> `container-runtime-security-falco`
- Image CVE scanning and SBOM generation in CI -> `shift-left-security-sast-sca`
- Packaging the image for cluster deployment -> `helm-kubernetes-deployment`

## 1. Production Multi-Stage Dockerfile Pattern

```dockerfile
# syntax=docker/dockerfile:1.4
# Stage 1: Build stage with full compiler toolchain
FROM golang:1.22-alpine AS builder

WORKDIR /app
RUN apk add --no-cache git ca-certificates

# Cache dependencies layer
COPY go.mod go.sum ./
RUN go mod download

# Copy source code and compile statically linked binary
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
    -ldflags="-w -s" \
    -o /bin/server ./cmd/server

# Stage 2: Minimal non-root runtime image
FROM gcr.io/distroless/static-debian12:nonroot

WORKDIR /
COPY --from=builder /bin/server /server
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

USER nonroot:nonroot
EXPOSE 8080

ENTRYPOINT ["/server"]
```

---

## 2. Docker Best Practices

- **Layer Caching**: Place commands that change rarely (package installations, dependencies) before code copy commands.
- **Run as Non-Root**: Never run container payloads as `root` (`UID 0`) unless explicitly required by low-level system daemons.
- **Signal Handling**: Ensure PID 1 handles `SIGTERM` and `SIGINT` properly.
- **Scan Before Push**: Run vulnerability scanners prior to registry push (`trivy image <image-tag>`).

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.