moby / moby/moby

How to make chown of dir not been root when use api CopyToContainer

Open
#43,310 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
72.1k
Forks
19.2k
Avg merge
1d 18h
Merged PRs (30d)
164

Description

Description
How to make chown of dir not been root when use api CopyToContainer to copy files to container. I set user as common in dockerdile and use api start container. File's own is correct, but dir's own is root. It make me confuse. How can it create a root dir in commom user.

Here is my code


// CopyTo copy files to container.
func (m *Manager) CopyTo(files map[string][]byte, fileMode int64, options types.CopyToContainerOptions) error {
	buffer := bytes.NewBuffer(nil)
	gWriter := gzip.NewWriter(buffer)
	tWriter := tar.NewWriter(gWriter)
	for path, file := range files {
		var zeroTime time.Time
		if err := tWriter.WriteHeader(&tar.Header{
			Name:       path,
			Size:       int64(len(file)),
			ModTime:    zeroTime,
			AccessTime: zeroTime,
			ChangeTime: zeroTime,
			Mode:       fileMode,
			Uid:        usrUID,
			Gid:        usrGID,
		}); err != nil {
			return err
		}
		if _, err := tWriter.Write(file); err != nil {
			return err
		}
	}
	if err := gWriter.Close(); err != nil {
		return err
	}
	if err := gWriter.Close(); err != nil {
		return err
	}
	return m.Cli.CopyToContainer(*m.Ctx, m.ID, "/", bytes.NewBuffer(buffer.Bytes()), options)
}

Describe the results you received:
File's own is pass and dir' own is root.
docker-error

Describe the results you expected:
Both own of file and dir is pass.

Additional information you deem important (e.g. issue happens only occasionally):

Output of docker version:
here is my go mod

replace (
	github.com/docker/docker => github.com/moby/moby v0.0.0-20181106193140-f5749085e9cb // v18.09.0
)
require (
	github.com/docker/docker v1.13.1
)

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

Start by reproducing the behavior through the docker client CopyToContainer API using the supplied Go example, then inspect how its tar archive handles file and directory entries, including Uid and Gid. Compare ownership for both entries when the container runs as the non-root user; done means determining whether the reported directory ownership matches the API's documented behavior and identifying the relevant fix or documentation gap.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, go
Domain
infrastructure
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.