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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ rule offers an editor suggestion instead.
| [`no-maintainer`](./docs/rules/no-maintainer.md) | DL4000 | `MAINTAINER` is deprecated; use a `maintainer` `LABEL` | ✅ | error |
| [`no-sudo`](./docs/rules/no-sudo.md) | DL3004 | Don't use `sudo` in `RUN`; switch users with `USER` | | error |
| [`prefer-copy-over-add`](./docs/rules/prefer-copy-over-add.md) | DL3020 | Use `COPY` unless you need `ADD`'s URL or archive handling | 💡 | error |
| [`prefer-exec-form`](./docs/rules/prefer-exec-form.md) | DL3025 | Use the exec (JSON array) form for `CMD` and `ENTRYPOINT` | ✅ | warn |

Fixable rules only autofix when the rewrite is unambiguous — for example,
`prefer-exec-form` leaves commands that rely on shell features alone.

## Configuration comments

Expand Down
96 changes: 96 additions & 0 deletions docs/rules/prefer-exec-form.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# prefer-exec-form

Require the exec (JSON array) form for `CMD` and `ENTRYPOINT`.

## Background

`CMD` and `ENTRYPOINT` accept two forms:

```dockerfile
# shell form
CMD npm start

# exec form
CMD ["npm", "start"]
```

The shell form wraps the command in `/bin/sh -c`, so the shell — not your
program — becomes PID 1. Signals such as the SIGTERM sent by
`docker stop` go to the shell and are typically not forwarded, which
means containers take the full stop timeout and are then killed. The
exec form runs the program directly, so it receives signals and shuts
down cleanly. The two forms also compose differently: with an exec-form
`ENTRYPOINT`, a shell-form `CMD` is appended as the shell invocation
followed by the command (`/bin/sh -c "npm start"`) rather than as the
command's own words, and a shell-form `ENTRYPOINT` ignores `CMD`
entirely.

## Rule Details

This rule warns when a `CMD` or `ENTRYPOINT` instruction uses the shell
form. When the command is a simple sequence of words with no shell
features (variables, quoting, pipes, redirection, leading environment
assignments, escaped whitespace, or shell builtins such as `exec`), the
rule fixes it by rewriting the command as a JSON array. Commands that rely
on shell features are reported without a fix, since converting them
requires deciding how to handle the shell. Commands that start with a
builtin whose external program behaves differently, such as `echo` or
`printf`, are also reported without a fix; use a full path such as
`/usr/bin/echo` to choose the program explicitly.

The rule also reports without a fix when:

- a `SHELL` instruction appears earlier in the file, because the command
may be written for that shell (for example, PowerShell cmdlets) rather
than being an executable,
- the stage targets Windows (a `--platform=windows/...` flag or a Windows
base image), because the default shell there is `cmd.exe`,
- the instruction is an `ENTRYPOINT` in a stage that sets `CMD`, or that
a later stage builds on and sets `CMD` without replacing the
`ENTRYPOINT`, because an exec-form `ENTRYPOINT` receives `CMD` as
arguments while the shell form ignores it, or
- the instruction is an `ONBUILD` trigger, because the downstream build
determines whether the conversion is safe.

The rule cannot see `SHELL` instructions in external base images. If a
base image changes the default shell, disable the fix for that file.

`RUN` instructions are not checked; the shell form is the normal way to
write them.

Examples of **incorrect** code for this rule:

```dockerfile
# eslint docker/prefer-exec-form: "error"

FROM node:20
ENTRYPOINT /docker-entrypoint.sh
CMD npm start
```

Examples of **correct** code for this rule:

```dockerfile
# eslint docker/prefer-exec-form: "error"

FROM node:20
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["npm", "start"]
```

## Options

This rule has no options.

## When Not to Use It

If a `CMD` or `ENTRYPOINT` genuinely needs shell processing — environment
variable expansion, for example — the shell form is legitimate. Consider
making the shell explicit (`CMD ["sh", "-c", "..."]`), or disable this
rule for that line with a configuration comment.

## Prior Art

- [hadolint DL3025](https://github.com/hadolint/hadolint/wiki/DL3025)
- [`JSONArgsRecommended`](https://docs.docker.com/reference/build-checks/json-args-recommended/)
build check
1 change: 1 addition & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe("plugin", () => {
"no-maintainer",
"no-sudo",
"prefer-copy-over-add",
"prefer-exec-form",
]);
});

Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import noDuplicateInstructions from "./rules/no-duplicate-instructions.js";
import noMaintainer from "./rules/no-maintainer.js";
import noSudo from "./rules/no-sudo.js";
import preferCopyOverAdd from "./rules/prefer-copy-over-add.js";
import preferExecForm from "./rules/prefer-exec-form.js";

const rules = {
"consistent-instruction-casing": consistentInstructionCasing,
Expand All @@ -25,6 +26,7 @@ const rules = {
"no-maintainer": noMaintainer,
"no-sudo": noSudo,
"prefer-copy-over-add": preferCopyOverAdd,
"prefer-exec-form": preferExecForm,
};

const plugin = {
Expand Down Expand Up @@ -56,6 +58,7 @@ const plugin = {
"docker/no-maintainer": "error",
"docker/no-sudo": "error",
"docker/prefer-copy-over-add": "error",
"docker/prefer-exec-form": "warn",
} as const,
},
},
Expand Down
183 changes: 183 additions & 0 deletions src/rules/prefer-exec-form.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/**
* @fileoverview Tests for the prefer-exec-form rule.
*/

import { RuleTester } from "eslint";
import { DockerFileLanguage } from "../languages/docker-language.js";
import rule from "./prefer-exec-form.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("prefer-exec-form", rule as never, {
valid: [
'CMD ["node", "app.js"]',
'ENTRYPOINT ["/docker-entrypoint.sh"]',
// RUN legitimately uses the shell form.
"RUN apt-get update",
"CMD",
],
invalid: [
{
code: "CMD npm start",
output: 'CMD ["npm", "start"]',
errors: [
{
messageId: "useExecForm",
data: { keyword: "CMD" },
line: 1,
column: 5,
endColumn: 14,
},
],
},
{
code: "ENTRYPOINT /docker-entrypoint.sh",
output: 'ENTRYPOINT ["/docker-entrypoint.sh"]',
errors: [
{
messageId: "useExecForm",
data: { keyword: "ENTRYPOINT" },
},
],
},
{
// Shell features prevent a safe autofix.
code: "CMD node app.js > /var/log/app.log",
output: null,
errors: [{ messageId: "useExecForm" }],
},
{
code: "CMD echo $HOME",
output: null,
errors: [{ messageId: "useExecForm" }],
},
{
code: 'CMD echo "hello world"',
output: null,
errors: [{ messageId: "useExecForm" }],
},
// cmd.exe expands %VAR% in the shell form only.
{
code: "CMD ping %COMPUTERNAME%",
output: null,
errors: [{ messageId: "useExecForm" }],
},
// A leading assignment is an environment variable, not a command.
{
code: "CMD NODE_ENV=production node app.js",
output: null,
errors: [{ messageId: "useExecForm" }],
},
// Shell builtins cannot be executed from the exec form.
{
code: "ENTRYPOINT exec /app/server",
output: null,
errors: [{ messageId: "useExecForm" }],
},
{
code: "CMD . /env.sh",
output: null,
errors: [{ messageId: "useExecForm" }],
},
// After SHELL, the command may need that shell.
{
code: 'SHELL ["powershell", "-command"]\nCMD Write-Host hello',
output: null,
errors: [{ messageId: "useExecForm" }],
},
{
code: 'FROM base AS a\nSHELL ["pwsh", "-c"]\nFROM a\nCMD Write-Host hello',
output: null,
errors: [{ messageId: "useExecForm" }],
},
// SHELL after the instruction does not apply to it.
{
code: 'CMD npm start\nSHELL ["/bin/bash", "-c"]',
output: 'CMD ["npm", "start"]\nSHELL ["/bin/bash", "-c"]',
errors: [{ messageId: "useExecForm" }],
},
// An exec-form ENTRYPOINT would start receiving CMD as arguments.
{
code: 'ENTRYPOINT /bin/sleep 60\nCMD ["--invalid"]',
output: null,
errors: [{ messageId: "useExecForm" }],
},
{
code: 'CMD ["--invalid"]\nENTRYPOINT /bin/sleep 60',
output: null,
errors: [{ messageId: "useExecForm" }],
},
// CMD in another stage does not apply.
{
code: 'FROM a\nCMD ["--invalid"]\nFROM b\nENTRYPOINT /bin/sleep 60',
output: 'FROM a\nCMD ["--invalid"]\nFROM b\nENTRYPOINT ["/bin/sleep", "60"]',
errors: [{ messageId: "useExecForm" }],
},
// A later stage built on this one inherits the ENTRYPOINT.
{
code: 'FROM alpine AS base\nENTRYPOINT /bin/sleep 60\nFROM base\nCMD ["--invalid"]',
output: null,
errors: [{ messageId: "useExecForm" }],
},
// A child stage that replaces the ENTRYPOINT does not block the fix.
{
code: 'FROM alpine AS base\nENTRYPOINT /bin/sleep 60\nFROM base\nENTRYPOINT ["/bin/true"]\nCMD ["x"]',
output: 'FROM alpine AS base\nENTRYPOINT ["/bin/sleep", "60"]\nFROM base\nENTRYPOINT ["/bin/true"]\nCMD ["x"]',
errors: [{ messageId: "useExecForm" }],
},
// A bracket that does not start a JSON array is shell form.
{
code: "CMD [ -f /ready ]",
output: null,
errors: [{ messageId: "useExecForm" }],
},
// The external echo may behave differently from the builtin.
{
code: "CMD echo -e ready",
output: null,
errors: [{ messageId: "useExecForm" }],
},
{
code: "CMD /usr/bin/echo ready",
output: 'CMD ["/usr/bin/echo", "ready"]',
errors: [{ messageId: "useExecForm" }],
},
// An escaped blank keeps two words together.
{
code: "CMD /bin/cat report\\ final.txt",
output: null,
errors: [{ messageId: "useExecForm" }],
},
// Windows images use cmd.exe as the default shell.
{
code: "FROM mcr.microsoft.com/windows/servercore:ltsc2022\nCMD dir",
output: null,
errors: [{ messageId: "useExecForm" }],
},
{
code: "FROM --platform=windows/amd64 example/app\nCMD dir",
output: null,
errors: [{ messageId: "useExecForm" }],
},
// ONBUILD triggers are reported without a fix.
{
code: "ONBUILD CMD /app/start",
output: null,
errors: [{ messageId: "useExecForm", column: 13 }],
},
// Arguments that are not assignments still convert.
{
code: "CMD node app.js --port=8080",
output: 'CMD ["node", "app.js", "--port=8080"]',
errors: [{ messageId: "useExecForm" }],
},
],
});
Loading
Loading