Search code examples
linuxdockerfile-permissionschmoddocker-build

Why Docker COPY doesn't change file permissions? (--chmod)


Given this Dockerfile:

FROM docker.io/alpine

RUN mkdir test

# RUN umask 0022
COPY README /test/README
COPY --chmod=777 README /test/README-777
COPY --chmod=755 README /test/README-755

COPY FORALL /test/FORALL
COPY --chmod=777 FORALL /test/FORALL-777
COPY --chmod=755 FORALL /test/FORALL-755

RUN ls -la /test

I'd expect to have the read, write, execute permissions be set accordingly by Docker during the build process (docker build ./).

But the last command returns

total 8
drwxr-xr-x    1 root     root          4096 Jun  9 19:20 .
drwxr-xr-x    1 root     root          4096 Jun  9 19:20 ..
-rwxrwxrwx    1 root     root             0 Jun  9 19:19 FORALL
-rwxrwxrwx    1 root     root             0 Jun  9 19:19 FORALL-755
-rwxrwxrwx    1 root     root             0 Jun  9 19:19 FORALL-777
-rw-rw-r--    1 root     root             0 Jun  9 19:19 README
-rw-rw-r--    1 root     root             0 Jun  9 19:19 README-755
-rw-rw-r--    1 root     root             0 Jun  9 19:19 README-777

No file permission was changed, and no error was raised.

Why doesn't it work?
How to fix this?


Solution

  • I figured out:
    the flag --chmod is a new feature from Docker Buildkit, so it is necessary to run the build enabling it via:

    DOCKER_BUILDKIT=1 docker build ./
    

    However, it is really not clear why Docker swallows the --chmod option without any error or warn about the non-existing option 😕.