Skip to content

Commit 0f183b1

Browse files
add user to inspect
Signed-off-by: Shubharanshu Mahapatra <[email protected]>
1 parent b0448ee commit 0f183b1

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

cmd/nerdctl/container/container_inspect_linux_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,22 @@ package container
1919
import (
2020
"fmt"
2121
"os"
22+
"path/filepath"
2223
"slices"
2324
"strings"
2425
"testing"
2526

2627
"github.com/docker/go-connections/nat"
2728
"gotest.tools/v3/assert"
2829

30+
"github.com/containerd/nerdctl/mod/tigron/expect"
31+
"github.com/containerd/nerdctl/mod/tigron/test"
2932
"github.com/containerd/nerdctl/v2/pkg/infoutil"
3033
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
3134
"github.com/containerd/nerdctl/v2/pkg/labels"
3235
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
3336
"github.com/containerd/nerdctl/v2/pkg/testutil"
37+
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
3438
)
3539

3640
func TestContainerInspectContainsPortConfig(t *testing.T) {
@@ -456,6 +460,38 @@ func TestContainerInspectDevices(t *testing.T) {
456460
assert.DeepEqual(t, expectedDevices, inspect.HostConfig.Devices)
457461
}
458462

463+
func TestContainerInspectUser(t *testing.T) {
464+
nerdtest.Setup()
465+
testCase := &test.Case{
466+
Description: "Container inspect contains User",
467+
Require: nerdtest.Build,
468+
Setup: func(data test.Data, helpers test.Helpers) {
469+
dockerfile := fmt.Sprintf(`
470+
FROM %s
471+
RUN groupadd -r test && useradd -r -g test test
472+
USER test
473+
`, testutil.UbuntuImage)
474+
475+
err := os.WriteFile(filepath.Join(data.TempDir(), "Dockerfile"), []byte(dockerfile), 0o600)
476+
assert.NilError(helpers.T(), err)
477+
478+
data.Set("buildCtx", data.TempDir())
479+
helpers.Ensure("build", "-t", data.Identifier(), data.Get("buildCtx"))
480+
helpers.Ensure("create", "--name", data.Identifier(), "--user", "test", data.Identifier())
481+
},
482+
Cleanup: func(data test.Data, helpers test.Helpers) {
483+
os.Remove(filepath.Join(data.TempDir(), "Dockerfile"))
484+
helpers.Anyhow("rm", "-f", data.Identifier())
485+
},
486+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
487+
return helpers.Command("inspect", "--format", "{{.Config.User}}", data.Identifier())
488+
},
489+
Expected: test.Expects(0, nil, expect.Equals("test\n")),
490+
}
491+
492+
testCase.Run(t)
493+
}
494+
459495
type hostConfigValues struct {
460496
Driver string
461497
ShmSize int64

pkg/cmd/container/create.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,15 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
179179
}
180180
}
181181

182+
if ensuredImage != nil && ensuredImage.ImageConfig.User != "" {
183+
internalLabels.user = ensuredImage.ImageConfig.User
184+
}
185+
186+
// Override it if User is passed
187+
if options.User != "" {
188+
internalLabels.user = options.User
189+
}
190+
182191
rootfsOpts, rootfsCOpts, err := generateRootfsOpts(args, id, ensuredImage, options)
183192
if err != nil {
184193
return nil, generateRemoveStateDirFunc(ctx, id, internalLabels), err
@@ -271,6 +280,7 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
271280
if err != nil {
272281
return nil, generateRemoveOrphanedDirsFunc(ctx, id, dataStore, internalLabels), err
273282
}
283+
274284
opts = append(opts, uOpts...)
275285
gOpts, err := generateGroupsOpts(options.GroupAdd)
276286
internalLabels.groupAdd = options.GroupAdd
@@ -665,6 +675,8 @@ type internalLabels struct {
665675

666676
// label for device mapping set by the --device flag
667677
deviceMapping []dockercompat.DeviceMapping
678+
679+
user string
668680
}
669681

670682
// WithInternalLabels sets the internal labels for a container.
@@ -790,6 +802,10 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO
790802
}
791803
m[labels.DNSSetting] = string(dnsSettingsJSON)
792804

805+
if internalLabels.user != "" {
806+
m[labels.User] = internalLabels.user
807+
}
808+
793809
return containerd.WithAdditionalContainerLabels(m), nil
794810
}
795811

pkg/inspecttypes/dockercompat/dockercompat.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,11 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
546546
c.Config.Env = spec.Process.Env
547547
}
548548
}
549+
550+
if n.Labels[labels.User] != "" {
551+
c.Config.User = n.Labels[labels.User]
552+
}
553+
549554
return c, nil
550555
}
551556

pkg/inspecttypes/dockercompat/dockercompat_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func TestContainerFromNative(t *testing.T) {
5555
"nerdctl/mounts": "[{\"Type\":\"bind\",\"Source\":\"/mnt/foo\",\"Destination\":\"/mnt/foo\",\"Mode\":\"rshared,rw\",\"RW\":true,\"Propagation\":\"rshared\"}]",
5656
"nerdctl/state-dir": tempStateDir,
5757
"nerdctl/hostname": "host1",
58+
"nerdctl/user": "test-user",
5859
},
5960
},
6061
Spec: &specs.Spec{
@@ -104,9 +105,11 @@ func TestContainerFromNative(t *testing.T) {
104105
"nerdctl/mounts": "[{\"Type\":\"bind\",\"Source\":\"/mnt/foo\",\"Destination\":\"/mnt/foo\",\"Mode\":\"rshared,rw\",\"RW\":true,\"Propagation\":\"rshared\"}]",
105106
"nerdctl/state-dir": tempStateDir,
106107
"nerdctl/hostname": "host1",
108+
"nerdctl/user": "test-user",
107109
},
108110
Hostname: "host1",
109111
Env: []string{"/some/path"},
112+
User: "test-user",
110113
},
111114
NetworkSettings: &NetworkSettings{
112115
Ports: &nat.PortMap{},

pkg/labels/labels.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,7 @@ const (
115115

116116
// DNSSettings sets the dockercompat DNS config values
117117
DNSSetting = Prefix + "dns"
118+
119+
// User is the username of the container
120+
User = Prefix + "user"
118121
)

0 commit comments

Comments
 (0)