Docker - WORKDIR creates directories owned by root
Updated 2026-07-16: refreshed the example and linked the Dockerfile reference.
Which user do you think owns the src/ directory in this Dockerfile example?:
FROM alpine:3.21
RUN adduser -D wendy
USER wendy
WORKDIR src/
COPY --chown=wendy . src/
The answer?: root
This happens because WORKDIR will create directories that don’t exist with the root user.
What if you wanted wendy to be the owner of that src/ directory?
You would need to COPY the src/ directory into place before setting it as the WORKDIR. For example:
FROM alpine:3.21
RUN adduser -D wendy
USER wendy
COPY --chown=wendy . src/
WORKDIR src/
More info: the Dockerfile reference, a closed GitHub issue, and the Docker code.