Skip to content

Add support for regexp in --filter name=<value> #4323

Closed

Description

@jedevc

What is the problem you're trying to solve

In the docker CLI, filters can use regexp. However, in nerdctl, the filters generally need to be an exact match.

For example, using the name filter in docker:

❯ docker ps
❯ docker ps --filter name="foo-"
CONTAINER ID   IMAGE     COMMAND           CREATED          STATUS          PORTS     NAMES
9dd7fabec4fc   alpine    "sleep 1000000"   14 seconds ago   Up 14 seconds             foo-bar
❯ docker ps --filter name="foo-.*"
CONTAINER ID   IMAGE     COMMAND           CREATED          STATUS          PORTS     NAMES
9dd7fabec4fc   alpine    "sleep 1000000"   22 seconds ago   Up 22 seconds             foo-bar

And in nerdctl:

❯ nerdctl ps
CONTAINER ID    IMAGE                              COMMAND            CREATED               STATUS    PORTS    NAMES
d21c34b4b65a    docker.io/library/alpine:latest    "sleep 1000000"    About a minute ago    Up                 
❯ nerdctl ps --filter name="foo-"
CONTAINER ID    IMAGE                              COMMAND            CREATED               STATUS    PORTS    NAMES
d21c34b4b65a    docker.io/library/alpine:latest    "sleep 1000000"    About a minute ago    Up                 foo-bar
❯ nerdctl ps --filter name="foo-.*"
CONTAINER ID    IMAGE    COMMAND    CREATED    STATUS    PORTS    NAMES

It would be nice to be able to use regexp in filters, or even to check for an exact match (AFAICT, that's not possible based on the logic here).

Describe the solution you'd like

At least the various name filters in different places (specifically container/list.go and volume/list.go) should support regexp, not sure about the others (maybe labels?).

Not entirely sure what the backwards compatibility guidelines are, since just switching the logic would be a backwards incompatible change.

Additional context

Would be happy to throw together a patch if this sounds good!

Activity

  1. jpadams commented on Jun 18, 2025

    @jpadams

    I'd be happy to see this for nerdctl ps --filter at least.

    cc @pendo324

  2. apostasie commented on Jun 18, 2025

    @apostasie
    Contributor

    Curious if you folks use nerdctl at Dagger?

  3. apostasie commented on Jun 18, 2025

    @apostasie
    Contributor

    Not entirely sure what the backwards compatibility guidelines are, since just switching the logic would be a backwards incompatible change.

    @jedevc IMO it is fine to break backward compatibility IF we are aligning with docker behavior (not being docker compatible is usually considered a bug).

    Wanna go for it? Alternatively I can look into it too.

  4. jedevc commented on Jun 18, 2025

    @jedevc
    ContributorAuthor

    We don't does nerdctl directly, but we do a couple of shell invocations of the docker cli to provision the dagger engine (sort of like the moby connhelpers).

    We want to support whatever environments users are using! 🎉 and increasingly often, that's nerdctl (through tooling like finch, though also standalone).

    Wanna go for it? Alternatively I can look into it too.

    Yeah, I'll take a look! I haven't had a chance to work on anything in nerdctl yet, so it'll be a fun excuse 😁

  5. apostasie commented on Jun 18, 2025

    @apostasie
    Contributor

    Go for it! :-)

    Tentatively, something like that would do just fine for foldNameFilter (I believe that would still be backward compatible):

    func (cl *containerFilterContext) foldNameFilter(_ context.Context, filter, value string) error {
    	cl.nameFilterFuncs = append(cl.nameFilterFuncs, func(name string) bool {
    		if value == "" || name == value {
    			return true
    		}
    		match, _ := regexp.MatchString(value, name)
    		return match
    	})
    	return nil
    }

    Of course, we should have it wherever Docker supports it (not just name), and yeah, we need tests :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions