Skip to content

feat: add support for userns #3941

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 1 commit into from
May 7, 2025

Conversation

Shubhranshu153
Copy link
Contributor

Adds support for running containers with custom user namespace mappings through
the --userns flag in 'run' and 'create' commands.

Key Features:

  • Parse and utilize UID/GID mappings from /etc/subuid and /etc/subgid
  • Support multi-entry mappings for user namespace configurations
  • Integrate with containerd's remapping capabilities

Technical Details:

  • Depends containerd.WithUserNSRemapperLabels for namespace configuration. So containerd mainline is required to compile. It can be run on containerd 1.7 if snapshotter support remap-ids.
  • For native snapshotter overlay snapshotter with remapping support is added in containerd 2.x (>2.0.x)

Dependencies:

  • Requires containerd with UserNSRemapperLabels support
  • Compatible with overlay snapshotter in containerd mainline

Testing:

  • Added unit tests for mapping configurations
  • Added integration tests for container isolation

This enhancement improves container isolation by providing
flexible user namespace mapping capabilities.

Size: XL

@@ -279,6 +279,7 @@ func setCreateFlags(cmd *cobra.Command) {
cmd.Flags().String("ipfs-address", "", "multiaddr of IPFS API (default uses $IPFS_PATH env variable if defined or local directory ~/.ipfs)")

cmd.Flags().String("isolation", "default", "Specify isolation technology for container. On Linux the only valid value is default. Windows options are host, process and hyperv with process isolation as the default")
cmd.Flags().String("userns", "", "Support idmapping of containers. This options is only supported on linux. If `host` is passed, no idmapping is done. if a user name is passed, it does idmapping based on the uidmap and gidmap ranges specified in /etc/subuid and /etc/subgid respectively")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add docs/command-reference.md.

Also, the command line seems incompatible with Docker?
Docker doesn't accept a username here, and the name is hardcoded to "dockremap".
Maybe we should have its equivalent as "nerdremap"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Podman accepts --subuidname string --subgidname string to specify a custom user name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the command line seems incompatible with Docker?
if they add host it will behave as docker as we check for that string and create the default snapshot.

For other names it behaves as docker daemon but at a container level rather than at daemon level. Will you suggest we configure it in nerdctl config instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For other names it behaves as docker daemon but at a container level rather than at daemon level. Will you suggest we configure it in nerdctl config instead?

Eventually, the both level should be supported, as in Podman: https://github.com/containers/podman/blob/v5.4.1/docs/source/markdown/options/userns.container.md?plain=1

  • podman run --userns=auto allocates subuids from the "containers" entry in /etc/subuid.
  • When userns=... is specified in containers.conf, Podman enables UserNS globally, unless --userns=host is specified.

nerdctl should probably follow the same convention, but s/containers.conf/nerdctl.toml/

Not all the features need to be implemented at once. Can just begin with the easiest one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does sound good to me in terms of supporting all the things that podman supports but would like to create a separate PR as this one has already been quite long. Can probably get in by 2.1.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to support all the things, but --userns=<USERNAME> is a new syntax that is not adopted by either Docker nor Podman, and is hard to adopt here unless there is a strong reason

Copy link
Contributor Author

@Shubhranshu153 Shubhranshu153 Apr 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dockerd --userns-remap="testuser:testuser"

For docker it is a docker daemon entry although i have not added all the different option and patterns to support. Should i add userns-remap instead of userns?

Docker has a daemon so it applies it to all containers launched for the daemon.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

userns-remap SGTM.

It should be also available in /etc/nerdctl/nerdctl.toml
https://github.com/containerd/nerdctl/blob/main/docs/config.md#properties

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure i can add to the nerdctl.toml

@apostasie
Copy link
Contributor

@Shubhranshu153 I need to do some reading on userns to get a better grasp first, but short term, happy to help you review testing / overall. Hit me up when ready.

@Shubhranshu153 Shubhranshu153 force-pushed the feat-nerdctl-userns branch 4 times, most recently from 4f8ae7b to bcc209d Compare April 21, 2025 17:35
@AkihiroSuda AkihiroSuda added this to the v2.x.x milestone Apr 21, 2025
@Shubhranshu153
Copy link
Contributor Author

Almalinux test seems to be flaky,

@apostasie
Copy link
Contributor

Almalinux test seems to be flaky,

Mmmm... failure is #4132 - but 4 fails out of 4 tries is no longer "flaky", it is a cold hard clusterf...

Do you mind closing and reopening?

@Shubhranshu153
Copy link
Contributor Author

ok this is repeatable.

@Shubhranshu153
Copy link
Contributor Author

		{
			Description: "start",
			NoParallel:  true,
			Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
				return helpers.Command("start", data.Labels().Get("cID"))
			},
			Expected: test.Expects(0, nil, nil),
		},
		{
			Description: "logs",
			NoParallel:  true,
			Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
				return helpers.Command("logs", data.Labels().Get("cID"))
			},
			Expected: test.Expects(0, nil, expect.Contains("foo")),
		},
	}

this test is racy in sense we cant guarantee the echo to complete before checking the logs. Needs to wait or retry

@Shubhranshu153 Shubhranshu153 force-pushed the feat-nerdctl-userns branch from 653a2d3 to 52f8831 Compare May 5, 2025 20:33
@Shubhranshu153
Copy link
Contributor Author

For test create:
Running in attach mode which will ensure the echo hello gets completed and put into stdout along with the logs so checking logs should have the foo text.

@Shubhranshu153 Shubhranshu153 force-pushed the feat-nerdctl-userns branch from 52f8831 to 2552378 Compare May 5, 2025 21:20
@Shubhranshu153
Copy link
Contributor Author

Not to de-sanitize this PR will create a separate PR to improve the TestCreate, for other bugs i still dont know what is the flakiness.

@apostasie
Copy link
Contributor

apostasie commented May 5, 2025

Not to de-sanitize this PR will create a separate PR to improve the TestCreate, for other bugs i still dont know what is the flakiness.

Thanks a lot @Shubhranshu153 - the log you just identified is #3717

Our other main PITA (especially on EL) is #4132

Your latest Docker issue is some networking issue connecting to ghcr.io

The motherload (most known flaky issues) is here: #4120

@Shubhranshu153
Copy link
Contributor Author

@apostasie ok got it, i think this is great to have a list to be able to focus on. I will try to pick some of these.
Will request a assignment so that we are not re-doing things.

@Shubhranshu153
Copy link
Contributor Author

Flaky Test PR's:
#4196
#4198
#4199

@Shubhranshu153
Copy link
Contributor Author

Rerunning the test for flaky test.

@@ -235,6 +235,9 @@ User flags:
- :nerd_face: `--umask`: Set the umask inside the container. Defaults to 0022.
Corresponds to Podman CLI.
- :whale: `--group-add`: Add additional groups to join
- :nerd_face: `--userns-remap=<username>:<groupname>`: Support idmapping of containers. This options is only supported on rootful linux for container create and run if a user name and optionally group name is passed, it does idmapping based on the uidmap and gidmap ranges specified in /etc/subuid and /etc/subgid respectively. Note: `--userns-remap` is not supported for building containers. Nerdctl Build doesn't support userns-remap feature. (format: <name|uid>[:<group|gid>])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a top-level flag?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right after the refactor. let me update it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

@AkihiroSuda AkihiroSuda mentioned this pull request May 7, 2025
Signed-off-by: Shubharanshu Mahapatra <[email protected]>
@Shubhranshu153 Shubhranshu153 force-pushed the feat-nerdctl-userns branch from 2552378 to 38d67f0 Compare May 7, 2025 03:40
Copy link
Member

@AkihiroSuda AkihiroSuda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@AkihiroSuda AkihiroSuda merged commit 24f2bd7 into containerd:main May 7, 2025
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants