Files in a Docker image keep root ownership after chown
ls -l in the container shows root root on files you chownedShort answer
Three things make chown look broken. COPY and ADD create files owned by root unless you add --chown. On the legacy builder, anything changed after a VOLUME instruction is discarded. And --chown with a name needs that user in the image's /etc/passwd; numeric IDs always work.
Fix it fast
1Find out which cause you have
Look at who really owns the files in the built image.
docker run --rm my-image ls -ln /app2Match what you see, then jump to its fix
What does the error look like?
ls -l in the container shows root root on files you chownedPermission denied writing to a directory the app user should own
Cause 1 of 3
COPY created the files as root
1Check
The Dockerfile reference says that without --chown, files are created with UID and GID 0.
grep -n "COPY\|ADD\|USER" Dockerfile2Fix
Set the owner on the COPY itself instead of a separate chown layer.
COPY --chown=1000:1000 . /app3Verify
Rebuild and list the owner again.
docker build -t my-image . && docker run --rm my-image ls -ln /appConfirmed by docs.docker.com
Cause 2 of 3
The change comes after a VOLUME instruction
1Check
The reference says changes made to a volume after it is declared are discarded with the legacy builder, and kept with BuildKit.
docker buildx version; grep -n VOLUME Dockerfile2Fix
Create the directory and set its owner before the VOLUME line, and use BuildKit.
RUN mkdir -p /data && chown 1000:1000 /data
VOLUME /data3Verify
Run the image and list the directory owner.
docker run --rm my-image ls -ldn /dataConfirmed by docs.docker.com
Cause 3 of 3
The user name is not in the image's /etc/passwd
1Check
Names are resolved from /etc/passwd and /etc/group in the container root filesystem; numeric IDs need no lookup. The reference also says --chown with --link needs numeric IDs.
docker run --rm my-image getent passwd appuser2Fix
Create the user before the COPY, or use numeric IDs in --chown.
RUN useradd -u 1000 appuser
COPY --chown=appuser:appuser . /app3Verify
Rebuild and check the owner column.
docker run --rm my-image ls -l /appConfirmed by docs.docker.com
How do I stop it coming back?
- Prefer COPY --chown over a separate RUN chown so ownership is set where the files are created.
- Assign an explicit UID and GID to the app user, as the Docker best-practices page advises when IDs matter.
- Declare VOLUME last, after directories exist with the right owner.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Servers
apk cannot find py-pip in an Alpine Docker image
py-pip (no such package):
On current Alpine releases there is no package called py-pip.
2 causes, each with a fixServers
ng serve runs in the container but localhost:4200 does not open
The browser cannot open localhost:4200 although ng serve is running in the container
ng serve listens on localhost by default, which inside a container is the container itself.
3 causes, each with a fixServers
Angular routes return 404 from nginx when you refresh or open a link
The home page loads, but refreshing /some/route shows an nginx 404
Angular routes like /users/42 are not files, so nginx looks for them on disk and returns 404.
3 causes, each with a fixServers
AH00558: Apache in Docker cannot reliably determine its fully qualified domain name
Set the 'ServerName' directive globally to suppress this message
AH00558 is a warning, not a crash.
2 causes, each with a fixServers
Apache in a Docker container logs AH01114 when it proxies to a backend
AH01114: HTTP: failed to make connection to backend: api
Apache's proxy worker could not open a connection to the address in ProxyPass.
5 causes, each with a fixServers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fix