diff --git a/libpod/runtime_ctr.go b/libpod/runtime_ctr.go index c670822a06..7abed0eef6 100644 --- a/libpod/runtime_ctr.go +++ b/libpod/runtime_ctr.go @@ -8,6 +8,7 @@ import ( "strings" "time" + "github.com/containers/buildah/pkg/chrootuser" "github.com/containers/common/pkg/config" "github.com/containers/libpod/libpod/define" "github.com/containers/libpod/libpod/events" @@ -305,11 +306,19 @@ func (r *Runtime) setupContainer(ctx context.Context, ctr *Container) (_ *Contai return nil, errors.Wrapf(err, "error retrieving named volume %s for new container", vol.Name) } } - + var volOptions []VolumeCreateOption logrus.Debugf("Creating new volume %s for container", vol.Name) - // The volume does not exist, so we need to create it. - volOptions := []VolumeCreateOption{WithVolumeName(vol.Name), WithVolumeUID(ctr.RootUID()), WithVolumeGID(ctr.RootGID())} + // Get appropriate user details. + if ctr.config.User == "" { + volOptions = []VolumeCreateOption{WithVolumeName(vol.Name), WithVolumeUID(ctr.RootUID()), WithVolumeGID(ctr.RootGID())} + } else { + uid, gid, _, err := chrootuser.GetUser(ctr.state.Mountpoint, ctr.config.User) + if err != nil { + return nil, errors.Wrapf(err, "Error retrieving container user details") + } + volOptions = []VolumeCreateOption{WithVolumeName(vol.Name), WithVolumeUID(int(uid)), WithVolumeGID(int(gid))} + } if isAnonymous { volOptions = append(volOptions, withSetAnon()) } diff --git a/test/e2e/run_userns_test.go b/test/e2e/run_userns_test.go index 25f12ec2ea..3038454b9f 100644 --- a/test/e2e/run_userns_test.go +++ b/test/e2e/run_userns_test.go @@ -89,6 +89,15 @@ var _ = Describe("Podman UserNS support", func() { Expect(ok).To(BeTrue()) }) + It("podman --userns=keep-id with volume", func() { + session := podmanTest.Podman([]string{"run", "--rm", "-t", "-i", "--userns=keep-id", "-v", "test:/test", "alpine", "ls", "-ld", "test"}) + session.WaitWithDefaultTimeout() + Expect(session.ExitCode()).To(Equal(0)) + uid := fmt.Sprintf("%d", os.Geteuid()) + ok, _ := session.GrepString(uid) + Expect(ok).To(BeTrue()) + }) + It("podman --userns=auto", func() { u, err := user.Current() Expect(err).To(BeNil())