Skip to content

Sort containers by Labels.com.docker.compose.container-number #679

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

Closed
AkramiPro opened this issue May 29, 2025 · 6 comments · Fixed by #682
Closed

Sort containers by Labels.com.docker.compose.container-number #679

AkramiPro opened this issue May 29, 2025 · 6 comments · Fixed by #682
Labels

Comments

@AkramiPro
Copy link

Thank you for the great work on docker-gen.

I want to sort my containers using container-number, but I think this is not supported yet, or I might be doing it wrong.

Image

sort by Labels.com.docker.compose.container-number

{{ $backends := sortObjectsByKeysAsc (whereLabelValueMatches . "com.example.haproxy.backend" "true") "Labels.com.docker.compose.container-number" }}

result:

    server app-3 172.25.0.3:443
    server app-9 172.25.0.4:443
    server app-8 172.25.0.5:443
    server app-4 172.25.0.6:443
    server app-7 172.25.0.7:443
    server app-10 172.25.0.8:443
    server app-1 172.25.0.9:443
    server app-2 172.25.0.10:443
    server app-6 172.25.0.11:443
    server app-5 172.25.0.12:443

sort by Name

{{ $backends := sortObjectsByKeysAsc (whereLabelValueMatches . "com.example.haproxy.backend" "true") "Name" }}

result:

    server app-1 172.25.0.9:443
    server app-10 172.25.0.8:443
    server app-2 172.25.0.10:443
    server app-3 172.25.0.3:443
    server app-4 172.25.0.6:443
    server app-5 172.25.0.12:443
    server app-6 172.25.0.11:443
    server app-7 172.25.0.7:443
    server app-8 172.25.0.5:443
    server app-9 172.25.0.4:443
@SchoNie
Copy link

SchoNie commented Jun 5, 2025

It's being sorted lexicographically (as strings), which causes "10" to come before "2" (because "1" < "2").
This happens because sortObjectsByKeysAsc performs string-based sorting.

Could you pad the container numbers labels with a 0? So 1 becomes 01, 2 becomes 02 and so on.

@buchdag buchdag added the bug label Jun 8, 2025
@buchdag
Copy link
Member

buchdag commented Jun 8, 2025

@AkramiPro that's actually a bug, docker-gen treat the full string passed to sortObjectsByKeysAsc as a key path expression, but for Labels this is not the case and everything after Labels. should be treated as a single key string.

In other words sortObjectsByKeysAsc $containers "Labels.com.docker.compose.container-number" currently expect something like this:

{
  "Labels": {
    "com": {
      "docker":{
        "compose": {
          "container_number": 1
        }
      }
    }
  }
}

While in reality you have:

{
  "Labels": {
    "com.docker.compose.container_number": 1
  }
}

@buchdag
Copy link
Member

buchdag commented Jun 8, 2025

@AkramiPro I've got a fix but that will get you to the same point as what @SchoNie describe : since sortObjectsByKeysAsc performs string based sorting, sorting on unpadded integer (like the com.docker.compose.container-number label) will give you the same result as sorting by container Name.

Edit: I got that fixed too.

@AkramiPro
Copy link
Author

@buchdag thanks for fixing ❤️🙏

@buchdag
Copy link
Member

buchdag commented Jun 8, 2025

@AkramiPro could you test the nginxproxy/docker-gen:fix-679 image and confirm that it fixes your issue ?

@AkramiPro
Copy link
Author

@AkramiPro could you test the nginxproxy/docker-gen:fix-679 image and confirm that it fixes your issue ?

yes it works
thank you @buchdag

    server app-1 172.25.0.9:443
    server app-2 172.25.0.10:443
    server app-3 172.25.0.3:443
    server app-4 172.25.0.6:443
    server app-5 172.25.0.12:443
    server app-6 172.25.0.11:443
    server app-7 172.25.0.7:443
    server app-8 172.25.0.5:443
    server app-9 172.25.0.4:443
    server app-10 172.25.0.8:443

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants