Skip to content

Commit 3f7a7a8

Browse files
authored
Merge pull request #4007 from Shubhranshu153/feat-add-env-to-inspect
add env and user to inspect in dockercompat
2 parents f57f872 + bbf2ab7 commit 3f7a7a8

File tree

5 files changed

+72
-1
lines changed

5 files changed

+72
-1
lines changed

cmd/nerdctl/container/container_inspect_linux_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,22 @@ import (
2020
"fmt"
2121
"os"
2222
"os/exec"
23+
"path/filepath"
2324
"slices"
2425
"strings"
2526
"testing"
2627

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

31+
"github.com/containerd/nerdctl/mod/tigron/expect"
32+
"github.com/containerd/nerdctl/mod/tigron/test"
3033
"github.com/containerd/nerdctl/v2/pkg/infoutil"
3134
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
3235
"github.com/containerd/nerdctl/v2/pkg/labels"
3336
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
3437
"github.com/containerd/nerdctl/v2/pkg/testutil"
38+
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
3539
)
3640

3741
func TestContainerInspectContainsPortConfig(t *testing.T) {
@@ -514,6 +518,36 @@ func TestContainerInspectBlkioSettings(t *testing.T) {
514518
assert.Equal(t, uint64(2000), inspect.HostConfig.BlkioDeviceWriteIOps[0].Rate)
515519
}
516520

521+
func TestContainerInspectUser(t *testing.T) {
522+
nerdtest.Setup()
523+
testCase := &test.Case{
524+
Description: "Container inspect contains User",
525+
Require: nerdtest.Build,
526+
Setup: func(data test.Data, helpers test.Helpers) {
527+
dockerfile := fmt.Sprintf(`
528+
FROM %s
529+
RUN groupadd -r test && useradd -r -g test test
530+
USER test
531+
`, testutil.UbuntuImage)
532+
533+
err := os.WriteFile(filepath.Join(data.TempDir(), "Dockerfile"), []byte(dockerfile), 0o600)
534+
assert.NilError(helpers.T(), err)
535+
536+
helpers.Ensure("build", "-t", data.Identifier(), data.TempDir())
537+
helpers.Ensure("create", "--name", data.Identifier(), "--user", "test", data.Identifier())
538+
},
539+
Cleanup: func(data test.Data, helpers test.Helpers) {
540+
helpers.Anyhow("rm", "-f", data.Identifier())
541+
},
542+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
543+
return helpers.Command("inspect", "--format", "{{.Config.User}}", data.Identifier())
544+
},
545+
Expected: test.Expects(0, nil, expect.Equals("test\n")),
546+
}
547+
548+
testCase.Run(t)
549+
}
550+
517551
type hostConfigValues struct {
518552
Driver string
519553
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
@@ -662,6 +672,8 @@ type internalLabels struct {
662672

663673
// label for device mapping set by the --device flag
664674
deviceMapping []dockercompat.DeviceMapping
675+
676+
user string
665677
}
666678

667679
// WithInternalLabels sets the internal labels for a container.
@@ -783,6 +795,10 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO
783795
}
784796
m[labels.DNSSetting] = string(dnsSettingsJSON)
785797

798+
if internalLabels.user != "" {
799+
m[labels.User] = internalLabels.user
800+
}
801+
786802
return containerd.WithAdditionalContainerLabels(m), nil
787803
}
788804

pkg/inspecttypes/dockercompat/dockercompat.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,16 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
561561
return nil, fmt.Errorf("failed to get blkio settings: %w", err)
562562
}
563563

564+
if n.Spec != nil {
565+
if spec, ok := n.Spec.(*specs.Spec); ok && spec.Process != nil {
566+
c.Config.Env = spec.Process.Env
567+
}
568+
}
569+
570+
if n.Labels[labels.User] != "" {
571+
c.Config.User = n.Labels[labels.User]
572+
}
573+
564574
return c, nil
565575
}
566576

pkg/inspecttypes/dockercompat/dockercompat_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,14 @@ 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",
59+
},
60+
},
61+
Spec: &specs.Spec{
62+
Process: &specs.Process{
63+
Env: []string{"/some/path"},
5864
},
5965
},
60-
Spec: &specs.Spec{},
6166
Process: &native.Process{
6267
Pid: 10000,
6368
Status: containerd.Status{
@@ -101,8 +106,11 @@ func TestContainerFromNative(t *testing.T) {
101106
"nerdctl/mounts": "[{\"Type\":\"bind\",\"Source\":\"/mnt/foo\",\"Destination\":\"/mnt/foo\",\"Mode\":\"rshared,rw\",\"RW\":true,\"Propagation\":\"rshared\"}]",
102107
"nerdctl/state-dir": tempStateDir,
103108
"nerdctl/hostname": "host1",
109+
"nerdctl/user": "test-user",
104110
},
105111
Hostname: "host1",
112+
Env: []string{"/some/path"},
113+
User: "test-user",
106114
},
107115
NetworkSettings: &NetworkSettings{
108116
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)