Skip to content

add env and user to inspect in dockercompat #4007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions cmd/nerdctl/container/container_inspect_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"testing"

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

"github.com/containerd/nerdctl/mod/tigron/expect"
"github.com/containerd/nerdctl/mod/tigron/test"
"github.com/containerd/nerdctl/v2/pkg/infoutil"
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
"github.com/containerd/nerdctl/v2/pkg/labels"
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
"github.com/containerd/nerdctl/v2/pkg/testutil"
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
)

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

func TestContainerInspectUser(t *testing.T) {
nerdtest.Setup()
testCase := &test.Case{
Description: "Container inspect contains User",
Require: nerdtest.Build,
Setup: func(data test.Data, helpers test.Helpers) {
dockerfile := fmt.Sprintf(`
FROM %s
RUN groupadd -r test && useradd -r -g test test
USER test
`, testutil.UbuntuImage)

err := os.WriteFile(filepath.Join(data.TempDir(), "Dockerfile"), []byte(dockerfile), 0o600)
assert.NilError(helpers.T(), err)

helpers.Ensure("build", "-t", data.Identifier(), data.TempDir())
helpers.Ensure("create", "--name", data.Identifier(), "--user", "test", data.Identifier())
},
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "-f", data.Identifier())
},
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("inspect", "--format", "{{.Config.User}}", data.Identifier())
},
Expected: test.Expects(0, nil, expect.Equals("test\n")),
}

testCase.Run(t)
}

type hostConfigValues struct {
Driver string
ShmSize int64
Expand Down
16 changes: 16 additions & 0 deletions pkg/cmd/container/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
}
}

if ensuredImage != nil && ensuredImage.ImageConfig.User != "" {
internalLabels.user = ensuredImage.ImageConfig.User
}

// Override it if User is passed
if options.User != "" {
internalLabels.user = options.User
}

rootfsOpts, rootfsCOpts, err := generateRootfsOpts(args, id, ensuredImage, options)
if err != nil {
return nil, generateRemoveStateDirFunc(ctx, id, internalLabels), err
Expand Down Expand Up @@ -271,6 +280,7 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
if err != nil {
return nil, generateRemoveOrphanedDirsFunc(ctx, id, dataStore, internalLabels), err
}

opts = append(opts, uOpts...)
gOpts, err := generateGroupsOpts(options.GroupAdd)
internalLabels.groupAdd = options.GroupAdd
Expand Down Expand Up @@ -662,6 +672,8 @@ type internalLabels struct {

// label for device mapping set by the --device flag
deviceMapping []dockercompat.DeviceMapping

user string
}

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

if internalLabels.user != "" {
m[labels.User] = internalLabels.user
}

return containerd.WithAdditionalContainerLabels(m), nil
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/inspecttypes/dockercompat/dockercompat.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,16 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
return nil, fmt.Errorf("failed to get blkio settings: %w", err)
}

if n.Spec != nil {
if spec, ok := n.Spec.(*specs.Spec); ok && spec.Process != nil {
c.Config.Env = spec.Process.Env
}
}

if n.Labels[labels.User] != "" {
c.Config.User = n.Labels[labels.User]
}

return c, nil
}

Expand Down
10 changes: 9 additions & 1 deletion pkg/inspecttypes/dockercompat/dockercompat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,14 @@ func TestContainerFromNative(t *testing.T) {
"nerdctl/mounts": "[{\"Type\":\"bind\",\"Source\":\"/mnt/foo\",\"Destination\":\"/mnt/foo\",\"Mode\":\"rshared,rw\",\"RW\":true,\"Propagation\":\"rshared\"}]",
"nerdctl/state-dir": tempStateDir,
"nerdctl/hostname": "host1",
"nerdctl/user": "test-user",
},
},
Spec: &specs.Spec{
Process: &specs.Process{
Env: []string{"/some/path"},
},
},
Spec: &specs.Spec{},
Process: &native.Process{
Pid: 10000,
Status: containerd.Status{
Expand Down Expand Up @@ -101,8 +106,11 @@ func TestContainerFromNative(t *testing.T) {
"nerdctl/mounts": "[{\"Type\":\"bind\",\"Source\":\"/mnt/foo\",\"Destination\":\"/mnt/foo\",\"Mode\":\"rshared,rw\",\"RW\":true,\"Propagation\":\"rshared\"}]",
"nerdctl/state-dir": tempStateDir,
"nerdctl/hostname": "host1",
"nerdctl/user": "test-user",
},
Hostname: "host1",
Env: []string{"/some/path"},
User: "test-user",
},
NetworkSettings: &NetworkSettings{
Ports: &nat.PortMap{},
Expand Down
3 changes: 3 additions & 0 deletions pkg/labels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,7 @@ const (

// DNSSettings sets the dockercompat DNS config values
DNSSetting = Prefix + "dns"

// User is the username of the container
User = Prefix + "user"
)