Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
64 changes: 64 additions & 0 deletions docs/rules/no-duplicate-instructions.md
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe("plugin", () => {
"consistent-instruction-casing",
"no-apt-get-upgrade",
"no-cd-in-run",
"no-duplicate-instructions",
]);
});

Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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,
},
},
Expand Down
57 changes: 57 additions & 0 deletions src/rules/no-duplicate-instructions.spec.ts
Original file line number Diff line number Diff line change
@@ -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 }],
},
],
});
77 changes: 77 additions & 0 deletions src/rules/no-duplicate-instructions.ts
Original file line number Diff line number Diff line change
@@ -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<string, InstructionNode[]>();

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;
Loading