Using COPY or ADD command to add a file and then changing ownership for the files introduces an extra layer and can take a long time when changing ownership for a lot of files.
FROM xxxx
COPY src/ /opt/src/
RUN chown -R www-data:www-data /opt/src/
Instead Docker ADD and COPY commands support --chown arguments which can be used to add files to the image and assign correct ownerships in a single step and this reduces an extra layer and produces a smaller image.
FROM xxxx
COPY --chown=www-data:www-data src/ /opt/src/
Note: This feature is only available in
17.09or later versions and is only supported in Linux container images
References: