| 1 |
## Output |
| 2 |
|
| 3 |
```text |
| 4 |
Relative workdir 'app/src' can have unexpected results if the base image changes |
| 5 |
``` |
| 6 |
|
| 7 |
## Description |
| 8 |
|
| 9 |
When specifying `WORKDIR` in a build stage, you can use an absolute path, like |
| 10 |
`/build`, or a relative path, like `./build`. Using a relative path means that |
| 11 |
the working directory is relative to whatever the previous working directory |
| 12 |
was. So if your base image uses `/usr/local/foo` as a working directory, and |
| 13 |
you specify a relative directory like `WORKDIR build`, the effective working |
| 14 |
directory becomes `/usr/local/foo/build`. |
| 15 |
|
| 16 |
The `WorkdirRelativePath` build rule warns you if you use a `WORKDIR` with a |
| 17 |
relative path without first specifying an absolute path in the same Dockerfile. |
| 18 |
The rationale for this rule is that using a relative working directory for base |
| 19 |
image built externally is prone to breaking, since working directory may change |
| 20 |
upstream without warning, resulting in a completely different directory |
| 21 |
hierarchy for your build. |
| 22 |
|
| 23 |
## Examples |
| 24 |
|
| 25 |
❌ Bad: this assumes that `WORKDIR` in the base image is `/` |
| 26 |
(if that changes upstream, the `web` stage is broken). |
| 27 |
|
| 28 |
```dockerfile |
| 29 |
FROM nginx AS web |
| 30 |
WORKDIR usr/share/nginx/html |
| 31 |
COPY public . |
| 32 |
``` |
| 33 |
|
| 34 |
✅ Good: a leading slash ensures that `WORKDIR` always ends up at the desired path. |
| 35 |
|
| 36 |
```dockerfile |
| 37 |
FROM nginx AS web |
| 38 |
WORKDIR /usr/share/nginx/html |
| 39 |
COPY public . |
| 40 |
``` |