Workaround for `Unexpected file permission error in container`

Posted in category tips on 2016-07-20
Tags: tips, docker

Due to issue registered on docker repository on github “unexpected file permission error in container”, #783 it is impossible to run rm -rf /somefolder for a folder that was created with ADD or COPY instructions of Dockerfile.

As a workaround to this it is possible to remove individual files one-by-one in case when this operation runs on the same layer: find /src -type f | xargs -L1 rm -f

Here is an example of Dockerfile (commented out lines is how you would normally do it):

FROM mono
ADD . /src
# RUN cd /src &&
#     chmod +x ./build.sh &&
#     ./build.sh &&
#     mkdir /app &&
#     cp /src/build/app/* /app &&
#     rm -rf /src
RUN cd /src &&
    chmod +x ./build.sh &&
    ./build.sh &&
    mkdir /app &&
    cp /src/build/app/* /app &&
    find /src -type f | xargs -L1 rm -f
WORKDIR /app
CMD [ "mono", "Service.exe" ]

This will leave the folder in place but there will be an option to flatten your docker image.

My docker version looks like this (hoping it will be fixed in the coming versions of docker):

Client:
 Version:      1.11.2
 API version:  1.23
 Go version:   go1.5.4
 Git commit:   b9f10c9
 Built:        Wed Jun  1 21:20:08 2016
 OS/Arch:      windows/amd64

Server:
 Version:      1.11.2
 API version:  1.23
 Go version:   go1.5.4
 Git commit:   b9f10c9
 Built:        Wed Jun  1 21:23:11 2016
 OS/Arch:      linux/amd64