Debian Codegraph @

buildkit/frontend/dockerfile/linter/docs/UndefinedVar.md

raw
1 ## Output
2
3 ```text
4 Usage of undefined variable '$foo'
5 ```
6
7 ## Description
8
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.
13
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.
18
19 It also detects common mistakes like typos in variable names. For example, in
20 the following Dockerfile:
21
22 ```dockerfile
23 FROM alpine
24 ENV PATH=$PAHT:/app/bin
25 ```
26
27 The check identifies that `$PAHT` is undefined and likely a typo for `$PATH`:
28
29 ```text
30 Usage of undefined variable '$PAHT' (did you mean $PATH?)
31 ```
32
33 ## Examples
34
35 ❌ Bad: `$foo` is an undefined build argument.
36
37 ```dockerfile
38 FROM alpine AS base
39 COPY $foo .
40 ```
41
42 ✅ Good: declaring `foo` as a build argument before attempting to access it.
43
44 ```dockerfile
45 FROM alpine AS base
46 ARG foo
47 COPY $foo .
48 ```
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 ```