diff --git a/README.md b/README.md index 1f9e3d6..fb705bf 100644 --- a/README.md +++ b/README.md @@ -63,11 +63,12 @@ export default defineConfig([ Each rule mirrors a well-known hadolint check or Docker best practice. -| Rule | hadolint | Description | Fixable | Recommended | -| -------------------------------------------------------------------------------- | -------- | ------------------------------------------------------ | ------- | ----------- | -| [`consistent-instruction-casing`](./docs/rules/consistent-instruction-casing.md) | — | Instruction keywords should be uppercase | ✅ | error | -| [`no-apt-get-upgrade`](./docs/rules/no-apt-get-upgrade.md) | DL3005 | Don't `apt-get upgrade`; update the base image instead | | error | -| [`no-cd-in-run`](./docs/rules/no-cd-in-run.md) | DL3003 | Use `WORKDIR` instead of `cd` in `RUN` | 💡 | error | +| Rule | hadolint | Description | Fixable | Recommended | +| -------------------------------------------------------------------------------- | ------------- | --------------------------------------------------------------------- | ------- | ----------- | +| [`consistent-instruction-casing`](./docs/rules/consistent-instruction-casing.md) | — | Instruction keywords should be uppercase | ✅ | error | +| [`no-apt-get-upgrade`](./docs/rules/no-apt-get-upgrade.md) | DL3005 | Don't `apt-get upgrade`; update the base image instead | | error | +| [`no-cd-in-run`](./docs/rules/no-cd-in-run.md) | DL3003 | Use `WORKDIR` instead of `cd` in `RUN` | 💡 | error | +| [`no-duplicate-instructions`](./docs/rules/no-duplicate-instructions.md) | DL4003/DL4004 | Only one `CMD`, `ENTRYPOINT`, or `HEALTHCHECK` per stage takes effect | | error | ## Configuration comments diff --git a/docs/rules/no-duplicate-instructions.md b/docs/rules/no-duplicate-instructions.md new file mode 100644 index 0000000..fbba710 --- /dev/null +++ b/docs/rules/no-duplicate-instructions.md @@ -0,0 +1,64 @@ +# no-duplicate-instructions + +Disallow multiple `CMD`, `ENTRYPOINT`, or `HEALTHCHECK` instructions in the same stage. + +## Background + +Docker only honors the last `CMD`, `ENTRYPOINT`, and `HEALTHCHECK` instruction +in each build stage. Earlier occurrences are silently ignored: + +```dockerfile +FROM node:20 +CMD ["node", "server.js"] +# only this one takes effect +CMD ["node", "worker.js"] +``` + +A duplicate is almost always a mistake — usually a leftover from an edit +or a misunderstanding of how these instructions combine. + +## Rule Details + +This rule warns on every `CMD`, `ENTRYPOINT`, and `HEALTHCHECK` instruction +that is followed by another instruction with the same keyword in the same +build stage. The last occurrence — the one Docker actually uses — is not +reported. Stages in a multi-stage build are checked independently. + +Examples of **incorrect** code for this rule: + +```dockerfile +# eslint docker/no-duplicate-instructions: "error" + +FROM node:20 +ENTRYPOINT ["node"] +CMD ["server.js"] +CMD ["worker.js"] +``` + +Examples of **correct** code for this rule: + +```dockerfile +# eslint docker/no-duplicate-instructions: "error" + +FROM node:20 AS build +CMD ["node", "build.js"] + +FROM node:20 +ENTRYPOINT ["node"] +CMD ["server.js"] +``` + +## Options + +This rule has no options. + +## When Not to Use It + +There is no valid reason to have duplicate `CMD`, `ENTRYPOINT`, or +`HEALTHCHECK` instructions in one stage, so this rule should always be +enabled. + +## Prior Art + +- [hadolint DL4003](https://github.com/hadolint/hadolint/wiki/DL4003) +- [hadolint DL4004](https://github.com/hadolint/hadolint/wiki/DL4004) diff --git a/src/index.spec.ts b/src/index.spec.ts index 79f893d..b525b5a 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -33,6 +33,7 @@ describe("plugin", () => { "consistent-instruction-casing", "no-apt-get-upgrade", "no-cd-in-run", + "no-duplicate-instructions", ]); }); diff --git a/src/index.ts b/src/index.ts index 0f6fb94..31ab30d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,11 +12,13 @@ import { DockerFileLanguage } from "./languages/docker-language.js"; import consistentInstructionCasing from "./rules/consistent-instruction-casing.js"; import noAptGetUpgrade from "./rules/no-apt-get-upgrade.js"; import noCdInRun from "./rules/no-cd-in-run.js"; +import noDuplicateInstructions from "./rules/no-duplicate-instructions.js"; const rules = { "consistent-instruction-casing": consistentInstructionCasing, "no-apt-get-upgrade": noAptGetUpgrade, "no-cd-in-run": noCdInRun, + "no-duplicate-instructions": noDuplicateInstructions, }; const plugin = { @@ -44,6 +46,7 @@ const plugin = { "docker/consistent-instruction-casing": "error", "docker/no-apt-get-upgrade": "error", "docker/no-cd-in-run": "error", + "docker/no-duplicate-instructions": "error", } as const, }, }, diff --git a/src/rules/no-duplicate-instructions.spec.ts b/src/rules/no-duplicate-instructions.spec.ts new file mode 100644 index 0000000..805c54f --- /dev/null +++ b/src/rules/no-duplicate-instructions.spec.ts @@ -0,0 +1,57 @@ +/** + * @fileoverview Tests for the no-duplicate-instructions rule. + */ + +import { RuleTester } from "eslint"; +import { DockerFileLanguage } from "../languages/docker-language.js"; +import rule from "./no-duplicate-instructions.js"; + +const ruleTester = new RuleTester({ + plugins: { + docker: { + languages: { dockerfile: new DockerFileLanguage() }, + }, + // eslint-disable-next-line @typescript-eslint/no-explicit-any -- plugin shape is validated by ESLint at runtime. + } as any, + language: "docker/dockerfile", +}); + +ruleTester.run("no-duplicate-instructions", rule as never, { + valid: [ + 'FROM node:20\nCMD ["node", "app.js"]', + 'FROM node:20\nENTRYPOINT ["node"]\nCMD ["app.js"]', + // One CMD per stage is fine. + 'FROM node:20 AS build\nCMD ["a"]\nFROM node:20\nCMD ["b"]', + "FROM node:20\nRUN echo one\nRUN echo two", + "FROM node:20\nHEALTHCHECK CMD curl -f http://localhost/", + ], + invalid: [ + { + code: 'FROM node:20\nCMD ["a"]\nCMD ["b"]', + errors: [ + { + messageId: "duplicateInstruction", + data: { keyword: "CMD" }, + line: 2, + column: 1, + }, + ], + }, + { + code: 'FROM node:20\nENTRYPOINT ["a"]\nENTRYPOINT ["b"]\nENTRYPOINT ["c"]', + errors: [ + { messageId: "duplicateInstruction", line: 2 }, + { messageId: "duplicateInstruction", line: 3 }, + ], + }, + { + code: "FROM node:20\nHEALTHCHECK CMD a\nHEALTHCHECK CMD b", + errors: [{ messageId: "duplicateInstruction", line: 2 }], + }, + { + // Duplicates inside a single stage of a multi-stage build. + code: 'FROM node:20 AS build\nCMD ["a"]\nFROM node:20\nCMD ["b"]\nCMD ["c"]', + errors: [{ messageId: "duplicateInstruction", line: 4 }], + }, + ], +}); diff --git a/src/rules/no-duplicate-instructions.ts b/src/rules/no-duplicate-instructions.ts new file mode 100644 index 0000000..9a16aa8 --- /dev/null +++ b/src/rules/no-duplicate-instructions.ts @@ -0,0 +1,77 @@ +/** + * @fileoverview Rule to disallow multiple CMD, ENTRYPOINT, or HEALTHCHECK + * instructions in the same build stage: only the last one takes effect. + */ + +//------------------------------------------------------------------------------ +// Imports +//------------------------------------------------------------------------------ + +import type { DockerRuleDefinition, InstructionNode } from "../types.js"; + +//------------------------------------------------------------------------------ +// Helpers +//------------------------------------------------------------------------------ + +const SINGLETON_KEYWORDS = new Set(["CMD", "ENTRYPOINT", "HEALTHCHECK"]); + +//------------------------------------------------------------------------------ +// Rule Definition +//------------------------------------------------------------------------------ + +const rule: DockerRuleDefinition<{ MessageIds: "duplicateInstruction" }> = { + meta: { + type: "problem", + docs: { + description: + "Disallow multiple `CMD`, `ENTRYPOINT`, or `HEALTHCHECK` instructions in the same stage", + recommended: true, + url: "https://github.com/eslint/docker/blob/main/docs/rules/no-duplicate-instructions.md", + }, + schema: [], + messages: { + duplicateInstruction: + "Multiple {{keyword}} instructions in the same stage; only the last one takes effect.", + }, + }, + + create(context) { + return { + Dockerfile(node) { + let seen = new Map(); + + const flush = (): void => { + for (const [keyword, instructions] of seen) { + for (const instruction of instructions.slice(0, -1)) { + context.report({ + node: instruction, + messageId: "duplicateInstruction", + data: { keyword }, + }); + } + } + + seen = new Map(); + }; + + for (const instruction of node.body) { + if (instruction.type === "FromInstruction") { + flush(); + continue; + } + + if (SINGLETON_KEYWORDS.has(instruction.keyword)) { + const group = seen.get(instruction.keyword) ?? []; + + group.push(instruction); + seen.set(instruction.keyword, group); + } + } + + flush(); + }, + }; + }, +}; + +export default rule;