docker / docker/buildx

Docker build - COPY/ADD/RUN --mount=type=bind does not preserve original UID/GID

Open
#1,421 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
4.5k
Forks
682
Avg merge
2d 14h
Merged PRs (30d)
29

Description

I would like to copy all files from a folder in my docker build context.
The files (and folders) inside 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 the UID:GID which is also stated in the documentation.
(UID:GID 0 is standard - why?)

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.

I am using DOCKER_BUILDKIT=1

https://stackoverflow.com/questions/74546557/docker-build-copy-add-preserve-owner-group-permissions

There is clearly a bug in the COPY --chown parameter
I can use any username/groupname which is not present in /etc/passwd and/or /etc/groups - it always defaults to GID:UID=0

The COPY command should fail
https://docs.docker.com/engine/reference/builder/#:~:text=If%20the%20container%20root%20filesystem%20does%20not%20contain%20either%20/etc/passwd%20or%20/etc/group%20files%20and%20either%20user%20or%20group%20names%20are%20used%20in%20the%20--chown%20flag%2C%20the%20build%20will%20fail%20on%20the%20COPY%20operation.%20Using%20numeric%20IDs%20requires%20no%20lookup%20and%20does%20not%20depend%20on%20container%20root%20filesystem%20content.

Also:

RUN --mount=type=bind should act just as a -v parameter and keep the all the UID/GIDs?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test is identified in the issue. Start by reproducing the behavior with a Dockerfile using COPY/ADD --chown and RUN --mount=type=bind under DOCKER_BUILDKIT=1, then trace the relevant BuildKit build entry points. Done means ownership handling matches the documented behavior, including failure for unresolved names and preservation where requested, with regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.