Home > Software engineering >  Docker Build COPY/ADD preserve Owner/Group
Docker Build COPY/ADD preserve Owner/Group

Time:11-23

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

CodePudding user response:

Try this

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

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

CodePudding user response:

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.

  • Related