WorkdirRelativePath: Relative workdir "app" can have unexpected results if the base image changes
is a build check warning, not an error: the build still finished with exit code 0. It flags
a WORKDIR without a leading slash. Write the absolute path,
WORKDIR /app, and the warning goes away.
A relative WORKDIR is joined to whatever working directory the base image left
behind. On alpine:latest that gave /app; on a base image that sets
its own WORKDIR /app, the same line gave /app/app.
The error
#5 [2/3] WORKDIR app
#5 CACHED
#6 [3/3] RUN pwd
#6 0.192 /app
#6 DONE 0.2s
...
1 warning found (use docker --debug to expand):
- WorkdirRelativePath: Relative workdir "app" can have unexpected results if the base image changes (line 2)
exit code: 0
The terminal colour codes around "1 warning found" were removed from the capture above.
Why it happens
WORKDIR app means "the folder app inside the current working
directory". In alpine:latest the current directory at the start of the stage is
/, so the step printed /app and the image looked right. The same
three lines on another base image, one built for this test from alpine:latest
with WORKDIR /app, printed something else:
#5 [2/3] WORKDIR app
...
#6 [3/3] RUN pwd
#6 0.224 /app/app
Only the FROM line changed, and the step after WORKDIR ran one
folder deeper, in /app/app. That is the kind of "unexpected results" the
warning names: the path depends on an image you may not control.
The fix
FROM alpine:latest
WORKDIR /app
RUN pwd
CMD ["sh"]
Rebuilt with docker build --progress=plain --no-cache -t fix9-bld-c8 ., the
RUN pwd step printed /app, the build ended with
exit code: 0 and no warning was printed. An absolute path gives the same folder
whatever the base image's own WORKDIR is. If a relative path is what you want,
for example to work below a folder the base image chose, the warning can be left in place;
it does not fail the build.
How it was reproduced
A four-line Dockerfile on alpine:latest (image id 294b683cb724) with
WORKDIR app, built with docker build --progress=plain -t fix9-bld-c8 .,
then the same lines on a test image whose working directory is /app, built with
docker build --progress=plain --no-cache -t fix9-bld-c8b .. Docker Desktop for
Windows, client and engine 29.9.3, buildx v0.38.1. The image names are the test's own.
Frequently asked
- Is WorkdirRelativePath an error in docker build?
- No. It is a build check warning; in the test the build ended with exit code 0 and the image was created.
- Why is a relative WORKDIR a problem in a Dockerfile?
- It is joined to the base image's working directory. WORKDIR app gave /app on alpine and /app/app on a base image that had WORKDIR /app.
- How do I fix the WorkdirRelativePath warning?
- Write the path with a leading slash, such as WORKDIR /app. The rebuilt image printed /app and the warning was gone.
More decoded errors in the Fixes category. Another build check warning with a one-line fix is FromAsCasing.