When executing a file whose permissions were changed earlier, a Text file is busy error could appear.
RUN cd scripts/test_env \
    && chmod +x test.prepareapp.sh
    && ./test.prepareapp.sh
It helps to add sync after changing permissions on the file:
RUN cd scripts/test_env \
    && chmod +x test.prepareapp.sh \
    && sync \
    && ./test.prepareapp.sh
This way, two docker image layers are created.
COPY . /code
RUN chown -R user_name:user_name /code
In the above case, the parent folder code and everything inside will be owned by user_name.
The line that executes chown adds the same amount of data to the second layer - creating a bigger docker image.
This can be resolved by doing the following (only one docker image layer):
COPY --chown=user_name:user_name . /code
The above case has one problem - The parent folder code is still owned by root.
This can be resolved by changing the ownership of only the parent folder code afterwards (no -R) (two docker image layers again, see table below for comparison).
COPY --chown=user_name:user_name . /code
RUN chown user_name:user_name /code
The following example assumes a directory size of 440MB.
| in Dockerfile | docker image size [MB] | 
|---|---|
COPY . /code | 
414MB | 
COPY . /codeRUN chown -R user_name:user_name /code | 
823MB | 
COPY --chown=user_name:user_name. /code | 
414MB | 
COPY --chown=user_name:user_name . /codeRUN chown user_name:user_name /code | 
414MB |