Skip to content

Commit cda279e

Browse files
authored
Merge pull request #5449 from dvdksn/check-undefined-var
docs: update undefined var check reference
2 parents 03afef5 + b89d6a0 commit cda279e

File tree

2 files changed

+50
-20
lines changed

2 files changed

+50
-20
lines changed

frontend/dockerfile/docs/rules/undefined-var.md

+25-10
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,25 @@ Usage of undefined variable '$foo'
1313

1414
## Description
1515

16-
Before you reference an environment variable or a build argument in a `RUN`
17-
instruction, you should ensure that the variable is declared in the Dockerfile,
18-
using the `ARG` or `ENV` instructions.
16+
This check ensures that environment variables and build arguments are correctly
17+
declared before being used. While undeclared variables might not cause an
18+
immediate build failure, they can lead to unexpected behavior or errors later
19+
in the build process.
1920

20-
Attempting to access an environment variable without explicitly declaring it
21-
doesn't necessarily result in a build error, but it may yield an unexpected
22-
result or an error later on in the build process.
21+
This check does not evaluate undefined variables for `RUN`, `CMD`, and
22+
`ENTRYPOINT` instructions where you use the [shell form](https://docs.docker.com/reference/dockerfile/#shell-form).
23+
That's because when you use shell form, variables are resolved by the command
24+
shell.
2325

24-
This check also attempts to detect if you're accessing a variable with a typo.
25-
For example, given the following Dockerfile:
26+
It also detects common mistakes like typos in variable names. For example, in
27+
the following Dockerfile:
2628

2729
```dockerfile
2830
FROM alpine
2931
ENV PATH=$PAHT:/app/bin
3032
```
3133

32-
The check detects that `$PAHT` is undefined, and that it's probably a
33-
misspelling of `PATH`.
34+
The check identifies that `$PAHT` is undefined and likely a typo for `$PATH`:
3435

3536
```text
3637
Usage of undefined variable '$PAHT' (did you mean $PATH?)
@@ -53,3 +54,17 @@ ARG foo
5354
COPY $foo .
5455
```
5556

57+
❌ Bad: `$foo` is undefined.
58+
59+
```dockerfile
60+
FROM alpine AS base
61+
ARG VERSION=$foo
62+
```
63+
64+
✅ Good: the base image defines `$PYTHON_VERSION`
65+
66+
```dockerfile
67+
FROM python AS base
68+
ARG VERSION=$PYTHON_VERSION
69+
```
70+

frontend/dockerfile/linter/docs/UndefinedVar.md

+25-10
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,25 @@ Usage of undefined variable '$foo'
66

77
## Description
88

9-
Before you reference an environment variable or a build argument in a `RUN`
10-
instruction, you should ensure that the variable is declared in the Dockerfile,
11-
using the `ARG` or `ENV` instructions.
9+
This check ensures that environment variables and build arguments are correctly
10+
declared before being used. While undeclared variables might not cause an
11+
immediate build failure, they can lead to unexpected behavior or errors later
12+
in the build process.
1213

13-
Attempting to access an environment variable without explicitly declaring it
14-
doesn't necessarily result in a build error, but it may yield an unexpected
15-
result or an error later on in the build process.
14+
This check does not evaluate undefined variables for `RUN`, `CMD`, and
15+
`ENTRYPOINT` instructions where you use the [shell form](https://docs.docker.com/reference/dockerfile/#shell-form).
16+
That's because when you use shell form, variables are resolved by the command
17+
shell.
1618

17-
This check also attempts to detect if you're accessing a variable with a typo.
18-
For example, given the following Dockerfile:
19+
It also detects common mistakes like typos in variable names. For example, in
20+
the following Dockerfile:
1921

2022
```dockerfile
2123
FROM alpine
2224
ENV PATH=$PAHT:/app/bin
2325
```
2426

25-
The check detects that `$PAHT` is undefined, and that it's probably a
26-
misspelling of `PATH`.
27+
The check identifies that `$PAHT` is undefined and likely a typo for `$PATH`:
2728

2829
```text
2930
Usage of undefined variable '$PAHT' (did you mean $PATH?)
@@ -45,3 +46,17 @@ FROM alpine AS base
4546
ARG foo
4647
COPY $foo .
4748
```
49+
50+
❌ Bad: `$foo` is undefined.
51+
52+
```dockerfile
53+
FROM alpine AS base
54+
ARG VERSION=$foo
55+
```
56+
57+
✅ Good: the base image defines `$PYTHON_VERSION`
58+
59+
```dockerfile
60+
FROM python AS base
61+
ARG VERSION=$PYTHON_VERSION
62+
```

0 commit comments

Comments
 (0)