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

Open
AkramiPro opened this issue May 29, 2025 · 1 comment
Open

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

AkramiPro opened this issue May 29, 2025 · 1 comment

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.

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

No branches or pull requests

2 participants