skip to Main Content

I would like to copy all files from a folder in my docker build context. The files in the folder are mixed with different owners and groups (e.g. UID=400 GUID=800 etc.), which I need to preserve (I also need to preserve timestamps, etc.)

So basically I need a 1:1 copy of the files to my docker image.

When I use ADD/COPY, it doesn’t preserve any the owner/group IDs which is also stated in the documentation. (defaults to 0)

I have made a workaround which uses rsync (-a) with localhost, but it’s not an ideal solution.
I could also use the docker cp command and commit the image but I would like to use this in my dockerfile.

Is there any way to do this?
(Docker Version 20.10.16-r2)

Edit:
I have also tried

RUN --mount=type=bind,source=myfiles,target=/myfiles cp -ar /myfiles/* /container_target/

but this doesn’t preserve owner, etc. neither

EDIT:
I am using DOCKER_BUILDKIT=1

2

Answers


  1. Try this

    COPY --chown=docker:docker source /path/to/destination
    

    Adjust to your problem,let us know if it does not work as expected.

    Login or Signup to reply.
  2. You can tar all the files up in a tar-ball and then use

    ADD tarball.tar.gz .
    

    in your Dockerfile. Docker will automatically extract the files from the tar-ball and it’ll preserve the original file owner ids.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search