Skip to main content
Docker & Container SecuritydevopsTarget: Docker BuildKit / Multi-Stage

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.

#docker cursorrules#docker cursor rules#docker claude md#containerization agent rules#cursor rules for dockerfile

Configuration Workbench

Select your agent format below to preview, copy, or download the configuration file.

Cursor configuration for Docker containerization.
10 lines•0.6 KB
# 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`.

1Save `.cursorrules` to your project root.
2Ensure Docker BuildKit is enabled (`DOCKER_BUILDKIT=1`).

Verified Agent Behavior Examples

Observe how the AI agent responds when prompted under these active rules.

Scenario: Building a Next.js production Dockerfile
User prompt: “Write a production Dockerfile for a Next.js standalone application.”
Agent Behavior: The agent generates a 3-stage Dockerfile (deps, builder, runner), uses `output: 'standalone'`, creates a `nextjs:nodejs` non-root user, and exposes the port cleanly.

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.