From 9f2cc2af179569a3c4a0bac4f74808cb0067d82f Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Tue, 10 Nov 2026 17:31:58 -0500 Subject: [PATCH 1/4] feat: add no-duplicate-instructions rule Co-Authored-By: Claude Fable 5.2 --- README.md | 11 +-- docs/rules/no-duplicate-instructions.md | 63 +++++++++++++++++ src/index.spec.ts | 1 + src/index.ts | 3 + src/rules/no-duplicate-instructions.spec.ts | 57 +++++++++++++++ src/rules/no-duplicate-instructions.ts | 77 +++++++++++++++++++++ 6 files changed, 207 insertions(+), 5 deletions(-) create mode 100644 docs/rules/no-duplicate-instructions.md create mode 100644 src/rules/no-duplicate-instructions.spec.ts create mode 100644 src/rules/no-duplicate-instructions.ts 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..a68ba98 --- /dev/null +++ b/docs/rules/no-duplicate-instructions.md @@ -0,0 +1,63 @@ +# 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"] +CMD ["node", "worker.js"] # only this one takes effect +``` + +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..2d8dd79 --- /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 { DockerLanguage } from "../languages/docker-language.js"; +import rule from "./no-duplicate-instructions.js"; + +const ruleTester = new RuleTester({ + plugins: { + docker: { + languages: { dockerfile: new DockerLanguage() }, + }, + // 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..cf3bec1 --- /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; From ac26f364067eb00ae50636b99f902da539efec6a Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Wed, 11 Nov 2026 09:20:08 -0500 Subject: [PATCH 2/4] docs: move explanatory comment off the CMD line Dockerfile instructions do not support trailing comments, so the comment became part of the CMD arguments. Co-Authored-By: Claude Opus 5.5 --- docs/rules/no-duplicate-instructions.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/rules/no-duplicate-instructions.md b/docs/rules/no-duplicate-instructions.md index a68ba98..018746d 100644 --- a/docs/rules/no-duplicate-instructions.md +++ b/docs/rules/no-duplicate-instructions.md @@ -10,7 +10,8 @@ in each build stage. Earlier occurrences are silently ignored: ```dockerfile FROM node:20 CMD ["node", "server.js"] -CMD ["node", "worker.js"] # only this one takes effect +# only this one takes effect +CMD ["node", "worker.js"] ``` A duplicate is almost always a mistake — usually a leftover from an edit From 5a210f5be08b0252aa5b9fdb890ed491443e1772 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Wed, 11 Nov 2026 11:12:38 -0500 Subject: [PATCH 3/4] docs: format Dockerfile instructions as code Co-Authored-By: Claude Opus 5.5 --- docs/rules/no-duplicate-instructions.md | 10 +++++----- src/rules/no-duplicate-instructions.ts | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/rules/no-duplicate-instructions.md b/docs/rules/no-duplicate-instructions.md index 018746d..fbba710 100644 --- a/docs/rules/no-duplicate-instructions.md +++ b/docs/rules/no-duplicate-instructions.md @@ -1,10 +1,10 @@ # no-duplicate-instructions -Disallow multiple CMD, ENTRYPOINT, or HEALTHCHECK instructions in the same stage. +Disallow multiple `CMD`, `ENTRYPOINT`, or `HEALTHCHECK` instructions in the same stage. ## Background -Docker only honors the last CMD, ENTRYPOINT, and HEALTHCHECK instruction +Docker only honors the last `CMD`, `ENTRYPOINT`, and `HEALTHCHECK` instruction in each build stage. Earlier occurrences are silently ignored: ```dockerfile @@ -19,7 +19,7 @@ or a misunderstanding of how these instructions combine. ## Rule Details -This rule warns on every CMD, ENTRYPOINT, and HEALTHCHECK instruction +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. @@ -54,8 +54,8 @@ 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 +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 diff --git a/src/rules/no-duplicate-instructions.ts b/src/rules/no-duplicate-instructions.ts index cf3bec1..9a16aa8 100644 --- a/src/rules/no-duplicate-instructions.ts +++ b/src/rules/no-duplicate-instructions.ts @@ -24,7 +24,7 @@ const rule: DockerRuleDefinition<{ MessageIds: "duplicateInstruction" }> = { type: "problem", docs: { description: - "Disallow multiple CMD, ENTRYPOINT, or HEALTHCHECK instructions in the same stage", + "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", }, From 40c222700b7997b7dc9bbdf0683e932e01b15e43 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Tue, 17 Nov 2026 10:31:40 -0500 Subject: [PATCH 4/4] refactor: use DockerFileLanguage in rule tests Co-Authored-By: Claude Opus 5.5 --- src/rules/no-duplicate-instructions.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rules/no-duplicate-instructions.spec.ts b/src/rules/no-duplicate-instructions.spec.ts index 2d8dd79..805c54f 100644 --- a/src/rules/no-duplicate-instructions.spec.ts +++ b/src/rules/no-duplicate-instructions.spec.ts @@ -3,13 +3,13 @@ */ import { RuleTester } from "eslint"; -import { DockerLanguage } from "../languages/docker-language.js"; +import { DockerFileLanguage } from "../languages/docker-language.js"; import rule from "./no-duplicate-instructions.js"; const ruleTester = new RuleTester({ plugins: { docker: { - languages: { dockerfile: new DockerLanguage() }, + languages: { dockerfile: new DockerFileLanguage() }, }, // eslint-disable-next-line @typescript-eslint/no-explicit-any -- plugin shape is validated by ESLint at runtime. } as any,