Skip to content

Add --chown flag to Dockerfile ADD and COPY#28499

Closed
duglin wants to merge 2 commits intomoby:masterfrom
duglin:copyUser
Closed

Add --chown flag to Dockerfile ADD and COPY#28499
duglin wants to merge 2 commits intomoby:masterfrom
duglin:copyUser

Conversation

@duglin
Copy link
Copy Markdown
Contributor

@duglin duglin commented Nov 16, 2016

Carrying #27303 since Kara is off doing other exciting things.

  • What Kara did
    Add a --chown flag to Dockerfile ADD and COPY instructions, proposed by Proposal: Implement --user flag for COPY and ADD #13600

  • How Kara did it
    Added flag to add and disptachCopy of builder/dockerfile/dispatchers.go, and passed the relevant information through to the daemon which looks up the user/group using a new function on the container object.

  • How to verify it
    Create a Dockerfile that uses the --chown flag on an ADD or COPY instruction, and check that the file is listed as having the appropriate owner

  • Description for the changelog
    add --chown flag to Dockerfile ADD and COPY instructions

2nd commit is just a rebase.

Signed-off-by: Doug Davis dug@us.ibm.com

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand this solves duplication layer when changing owner, but it does not solve layer duplication needed for chmod +x (which is in my case needed on 60% of COPY/ADD in most images) ?

@lauripiisang
Copy link
Copy Markdown

/cc @thaJeztah I've seen you create similar issues and provide feedback, maybe you can help cc in the right people to get this merged ?

@thaJeztah
Copy link
Copy Markdown
Member

Let's move this to code review

@tianon
Copy link
Copy Markdown
Member

tianon commented Dec 22, 2016

If --chmod might come later, would it make sense to call this --chown instead of --user to make it more clear what it's doing?

@duglin
Copy link
Copy Markdown
Contributor Author

duglin commented Dec 22, 2016

did someone suggest a chmod option? I missed that

@tianon
Copy link
Copy Markdown
Member

tianon commented Dec 22, 2016 via email

@duglin
Copy link
Copy Markdown
Contributor Author

duglin commented Dec 22, 2016

doi - sorry I missed that. I was scanning too quickly.

@jrusiecki
Copy link
Copy Markdown

I need ADD extension to use instead of:
run wget ... | chmod +x ...
which is superior (layer-wise) to

ADD ...
run chmod +x ...

Thus without +x option ADD is useless anyway (if I already have run... I put there also non-executables so will not use COPY/ADD even for --user option).
COPY would benefit from +x too (although file copied from linux is probably already +x) as it would improve quality by clearly stating +x so to limit errors.

@ghost
Copy link
Copy Markdown

ghost commented Dec 25, 2016

+1

@duglin duglin force-pushed the copyUser branch 2 times, most recently from b8790bf to beee26d Compare December 28, 2016 15:29
@duglin
Copy link
Copy Markdown
Contributor Author

duglin commented Dec 31, 2016

@tianon which would be better:
--user && --mode
or
--chown && --chmod
?

The 2nd feels a little like we're trying to expose our implementation detail (changing things after the fact vs setting the properties on the files during creation) - when the end user should just care about the properties having the right values regardless of how they are set.

@tianon
Copy link
Copy Markdown
Member

tianon commented Jan 3, 2017

Hmm, good point, but --user is already in use for changing the runtime user, so potentially confusing double usage. For what it's worth, rsync uses --chown and --chmod:

     --chown=USER:GROUP      simple username/groupname mapping
     --chmod=CHMOD           affect file and/or directory permissions

In my mind, the argument for --chown and --chmod is that normally, ADD/COPY try as hard as possible to preserve the source ownership/permissions, so the "change" implied by --chmod and --chown as the name is referring to the change which has to happen during the copy (ie, "copy XYZ, but change the owner to ABC").

@glensc
Copy link
Copy Markdown
Contributor

glensc commented Jan 3, 2017

i'm also proposing DEFATTR so that if you use multiple COPY or ADD you don't have to repeat the options:

DEFATTR chown=USER
DEFATTR chown=USER:GROUP
DEFATTR chown=UID
DEFATTR chown=UID:GID

chmod is a bit tricky because you definetely want different permissions for files and dirs:

DEFATTR chmod=a+rX
DEFATTR chmod=a+X

these would rise the x bit only if x bit is present for any of the users: chmod(1):

execute/search only if the file is a directory or already has execute permission for some user (X)

or more clearer directives for different kinds:

DEFATTR dir_chmod=0755 file_chmod=0644

@thaJeztah
Copy link
Copy Markdown
Member

@glensc could you open a separate issue for that proposal? Also, would be worth describing how it should interact with different commands that take options (just for illustration, if LABEL would take --chown as an option).

@duglin
Copy link
Copy Markdown
Contributor Author

duglin commented Jan 3, 2017

@tianon ok - will go with --chown.

I'd prefer to leave --chmod for another PR because I think more thinking needs to go into that.

I have a high-order question for people.... if we were developing docker build from scratch would we have a --chown flag at all on ADD/COPY or would we have USER impact subsequent RUN, ADD and COPY commands? I'm asking because @glensc's latest comment got me wondering if normally people would have expected USER to have a broader impact than it does today. I'm not suggesting we break backwards compatibility, but I am wondering if instead of a flag on ADD/COPY we should consider a flag on USER instead - something like: USER --chown john:mygroup. This would mean use john:mygroup for all subsequent RUN commands (as well as the CMD) like it does today, but also use john:mygroup on all subsequent ADD/COPY commands too - which is probably what most newbies would have expected anyway.

To be clear, the --chown flag doesn't take any args, the john:mygroup is the normal USER arg. Perhaps --chown isn't the best word since it might be mentally parsed incorrectly, so perhaps --copy instead - dunno, I'm open to other words.

thoughts?

@jrusiecki
Copy link
Copy Markdown

I've copied my comments to moby/buildkit#4242.

@jazoom
Copy link
Copy Markdown

jazoom commented Jan 26, 2017

This still isn't in the docs. Is it used like this?

COPY from.txt /to.txt --chown someuser

@cpuguy83
Copy link
Copy Markdown
Member

@jazoom This is not merged.

@dmytroleonenko
Copy link
Copy Markdown

Looking for the solution for ages. Subscribed

b.runConfig.Cmd = strslice.StrSlice(append(getShell(b.runConfig), fmt.Sprintf("#(nop) %s --chown=%s %s in %s ", cmdName, usergrp, srcHash, dest)))
} else {
b.runConfig.Cmd = strslice.StrSlice(append(getShell(b.runConfig), fmt.Sprintf("#(nop) %s %s in %s ", cmdName, srcHash, dest)))
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two branches are nearly identical. Could you move most of this line out of the branch, and leave only the inner most fmt.Sprintf() in the branch?

comment = fmt.Sprintf("%s --chown=%s %s in %s", cmdName, usergrp, origPaths, dest)
} else {
comment = fmt.Sprintf("%s %s in %s", cmdName, origPaths, dest)
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks awfully similar to the branch above (that I commented on).

How about a function for this? The #(nop) prefix could be an arg.

// uid:gid, username:gid, or uid:groupname and parses the passwd file in the container
// to return the ExecUser referred to by `username`.
func (container *Container) ParseUserGrp(username string) (*user.ExecUser, error) {
passwdPath, err := user.GetPasswdPath()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this call always returns an err on windows, which means this new flag would not work for the windows builder.

Are we ok with that?

@dnephin
Copy link
Copy Markdown
Member

dnephin commented Feb 10, 2017

Needs a rebase as well

@dnephin
Copy link
Copy Markdown
Member

dnephin commented Mar 9, 2017

@duglin Thanks for working on this!

It seems this PR hasn't seen any activity in 4 weeks. I'm going to close the PR since there is no activity. If/when you have time to rebase, and respond to the comments please feel free to re-open it (or ping me and I can).

@dnephin dnephin closed this Mar 9, 2017
@duglin
Copy link
Copy Markdown
Contributor Author

duglin commented Mar 9, 2017

@dnephin this hasn't been touched in a while because people asked for the discussion to be moved over to: #30110

I can rebase now if we want but we really need #30110 to be done/discussed first.

@dnephin
Copy link
Copy Markdown
Member

dnephin commented Mar 9, 2017

Ok, let's reopen once the discussion in #30110 has produced some results

@italomaia
Copy link
Copy Markdown

I'm unsure how #32816 is blocking this issue. To some extent, they seem unrelated as the problem of 32816 is related to the source, right? This PR seems so helpful it hurts to see it goes by closed for another week.

@thaJeztah
Copy link
Copy Markdown
Member

reopening (again) following #30110 (comment), but probably needs a bit of rewriting

@thaJeztah thaJeztah reopened this Jul 20, 2017
@thaJeztah
Copy link
Copy Markdown
Member

(make that a lot of rewriting LOL)

@estesp
Copy link
Copy Markdown
Contributor

estesp commented Jul 24, 2017

I can carry this if that's helpful to @duglin?

@duglin
Copy link
Copy Markdown
Contributor Author

duglin commented Jul 24, 2017

go for it! thanks @estesp

@estesp
Copy link
Copy Markdown
Contributor

estesp commented Jul 26, 2017

Carried in #34263

@estesp estesp closed this Jul 26, 2017
@thaJeztah thaJeztah added the area/builder Build label Sep 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.