# Rules

What LayerLint checks for and why it matters. This file is generated from
`internal/rules/meta.go`; run `make gen-rules` after changing rule metadata.

Rule IDs use the form `dockerfile/<kebab-name>`. Rule pages live at
`https://layerlint.in/rules/<kebab-name>`.

## broad-copy-before-deps

**High severity** · Layer Caching

`dockerfile/broad-copy-before-deps`

Copying the full source tree before installing dependencies invalidates the dependency cache on every code change.

**Why it matters:** Dependency layers rebuild whenever any source file changes, even if dependency manifests did not.

**Impact:** Every code change re-runs dependency installation, so CI feedback takes longer.

Bad:
```dockerfile
FROM node:18
COPY . .
RUN npm ci
```

Good:
```dockerfile
FROM node:18
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
```

**Fix:** Copy only dependency manifests first, install dependencies, then copy the rest of the source.

## manifest-without-lockfile

**High severity** · Reproducibility

`dockerfile/manifest-without-lockfile`

Installing dependencies without copying the lockfile defeats reproducible builds.

**Why it matters:** Without a lockfile, installs can resolve different versions between builds.

**Impact:** Non-reproducible builds and version drift between environments.

Bad:
```dockerfile
COPY package.json ./
RUN npm ci
```

Good:
```dockerfile
COPY package.json package-lock.json ./
RUN npm ci
```

**Fix:** Copy the lockfile alongside the manifest and install with npm ci or --frozen-lockfile.

## unpinned-base-image-tag

**Medium severity** · Security & Stability

`dockerfile/unpinned-base-image-tag`

Using :latest or an implicit latest tag makes builds non-reproducible.

**Why it matters:** The image behind a moving tag can change without notice.

**Impact:** Builds that worked yesterday can change or break today.

Bad:
```dockerfile
FROM node:latest
```

Good:
```dockerfile
FROM node:18.20.0
```

**Fix:** Pin a specific version tag or a digest.

## copying-secrets-into-image

**High severity** · Security

`dockerfile/copying-secrets-into-image`

Secrets copied into layers remain in image history even if deleted later.

**Why it matters:** Copied files stay in earlier image layers even if a later instruction deletes them.

**Impact:** Anyone with the image can extract credentials from layer history.

Bad:
```dockerfile
COPY .env /app/
COPY id_rsa /root/.ssh/
```

Good:
```dockerfile
RUN --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci
```

**Fix:** Use BuildKit secrets (--mount=type=secret) or runtime environment variables.

## run-as-root

**High severity** · Security

`dockerfile/run-as-root`

Containers default to root. Prefer a non-root USER before CMD/ENTRYPOINT.

**Why it matters:** A compromised process running as root has full container privileges.

**Impact:** Container compromise becomes a host-level risk and can violate admission policies.

Bad:
```dockerfile
FROM node:18
CMD ["node", "server.js"]
```

Good:
```dockerfile
FROM node:18
RUN adduser -S appuser
USER appuser
CMD ["node", "server.js"]
```

**Fix:** Create a non-root user and set USER before CMD/ENTRYPOINT.

## missing-dockerignore

**Medium severity** · Build Optimization

`dockerfile/missing-dockerignore`

Without .dockerignore, the build context may include .git, node_modules, secrets, and other unnecessary files.

**Why it matters:** Larger contexts slow builds and can leak sensitive files.

**Impact:** Slower builds and a higher chance of copying secrets or build artifacts.

Bad:
```dockerfile
# no .dockerignore next to the Dockerfile
```

Good:
```dockerfile
node_modules/
.git/
dist/
*.log
.env*
```

**Fix:** Add a .dockerignore that excludes .git, node_modules, build output, and secrets.

## build-without-cache-mount

**Low severity** · Build Optimization

`dockerfile/build-without-cache-mount`

Package installs and compiles are faster when BuildKit cache mounts are used.

**Why it matters:** Without cache mounts, package managers re-download artifacts more often than needed.

**Impact:** Repeated downloads add time and network use on each build.

Bad:
```dockerfile
RUN npm ci
```

Good:
```dockerfile
RUN --mount=type=cache,target=/root/.npm npm ci
```

**Fix:** Add --mount=type=cache for the package-manager cache directory.

## apt-update-without-install

**Medium severity** · Layer Caching

`dockerfile/apt-update-without-install`

Running apt-get update in a separate layer from install leaves a stale package index.

**Why it matters:** A cached update layer goes stale, and unneeded recommended packages inflate the image.

**Impact:** Stale package lists and larger images with a wider attack surface.

Bad:
```dockerfile
RUN apt-get update
RUN apt-get install -y curl
```

Good:
```dockerfile
RUN apt-get update && apt-get install -y --no-install-recommends curl && rm -rf /var/lib/apt/lists/*
```

**Fix:** Combine update and install in one RUN, add --no-install-recommends, and clean /var/lib/apt/lists.

## multiple-broad-copies

**Medium severity** · Layer Caching

`dockerfile/multiple-broad-copies`

Copying the full tree more than once wastes layers and hurts caching.

**Why it matters:** Repeated broad COPY instructions invalidate cache more often than necessary.

**Impact:** Extra layers and unnecessary rebuilds.

Bad:
```dockerfile
COPY . .
RUN npm run build
COPY . .
```

Good:
```dockerfile
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
```

**Fix:** Use one broad copy at the end, after dependency installation.

## redundant-dependency-install

**Medium severity** · Build Optimization

`dockerfile/redundant-dependency-install`

Running the same dependency install more than once usually means layer ordering is wrong.

**Why it matters:** Duplicate installs add time without improving correctness.

**Impact:** Longer builds and larger intermediate images.

Bad:
```dockerfile
RUN npm install
RUN npm run build
RUN npm install
```

Good:
```dockerfile
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
```

**Fix:** Install dependencies once per stage and fix the layer ordering.

## add-instead-of-copy

**Low severity** · Best Practices

`dockerfile/add-instead-of-copy`

ADD has magic behavior (auto-extract, remote URLs). Prefer COPY unless you need that behavior.

**Why it matters:** Unexpected ADD behavior makes Dockerfiles harder to reason about.

**Impact:** Surprising auto-extraction or remote fetches during builds.

Bad:
```dockerfile
ADD . /app/
```

Good:
```dockerfile
COPY . /app/
```

**Fix:** Use COPY unless you specifically need ADD's archive extraction or URL support.

## wget-curl-without-checksum

**High severity** · Security

`dockerfile/wget-curl-without-checksum`

Downloading binaries without verifying checksums is a supply-chain risk.

**Why it matters:** Tampered remote artifacts can be installed silently.

**Impact:** Supply-chain compromise of the resulting image.

Bad:
```dockerfile
RUN curl -L https://example.com/tool -o /usr/local/bin/tool
```

Good:
```dockerfile
RUN curl -L https://example.com/tool -o /usr/local/bin/tool && echo "<sha256>  /usr/local/bin/tool" | sha256sum -c -
```

**Fix:** Verify downloads with a checksum obtained from the official source.

## missing-healthcheck

**Medium severity** · Production Readiness

`dockerfile/missing-healthcheck`

Services that EXPOSE a port should declare a HEALTHCHECK so orchestrators can detect unhealthy processes. CLI images without EXPOSE are skipped.

**Why it matters:** Orchestrators cannot distinguish a healthy service from a stuck process.

**Impact:** Risky rollouts and no automatic recovery from stuck processes.

Bad:
```dockerfile
FROM node:18
EXPOSE 8080
CMD ["node", "server.js"]
```

Good:
```dockerfile
FROM node:18
EXPOSE 8080
HEALTHCHECK CMD curl -f http://localhost:8080/health || exit 1
CMD ["node", "server.js"]
```

**Fix:** Add a HEALTHCHECK that probes a real health endpoint.
