Docker & Container Security AI Agent Rules & Cursorrules
Force AI agents to write minimal, non-root, production-hardened Dockerfiles.
Stop AI agents from generating insecure, bloated 1GB Docker images that run as root. Enforce multi-stage builds, non-root user execution, and BuildKit cache mounts.
Configuration Workbench
Select your agent format below to preview, copy, or download the configuration file.
# Docker & Containerization Cursor Rules
You are a Principal DevOps and Container Security Architect.
## Core Rules
- Always write multi-stage Dockerfiles separating build dependencies from runtime environments.
- Non-Root: Never run as 'root'. Explicitly create a non-root group and user (e.g. 'USER node' or 'USER nonroot').
- Base Images: Use slim or Alpine base images with explicit version tags (never use ':latest').
- Caching: Copy dependency manifests first ('package.json', 'pnpm-lock.yaml') before copying application source code.
- Ignore Secrets: Always create a comprehensive '.dockerignore' excluding '.env*', '.git', and build artifacts.Pro-tip: Place this file directly in the root directory of your Docker & Container Security project. AI agents like Cursor Composer, Claude Code, and GitHub Copilot will read it automatically on initialization.
Architectural Principles Enforced
When this configuration file is loaded into Cursor or Claude Code, the agent strictly enforces the following design invariants:
Always use multi-stage builds (`AS builder` and `AS runner`) to keep production images tiny.
NEVER run containers as the `root` user; create and switch to a dedicated non-root user (`USER nonroot`).
Use minimal Alpine or distroless base images with fixed sha256 digests.
Leverage BuildKit cache mounts (`--mount=type=cache`) to accelerate CI/CD build speeds.
Include a lightweight `.dockerignore` file excluding `node_modules`, `.git`, and environment secrets.
Installation & Repository Setup
Place in project root as `.cursorrules` or `.cursor/rules/docker.mdc`.
Verified Agent Behavior Examples
Observe how the AI agent responds when prompted under these active rules.
Frequently Asked Questions
Why is running as non-root critical in Docker?
If a container process running as root is compromised via an application vulnerability, an attacker can potentially break out of the container boundary and gain root access to the host machine kernel.