Skip to content
Advertisement

ls sort order inside container

Running ls -d to list directories, print directories in different order if trailing / is present in file name. Why is that? What sorting rules apply? and why does this happen only with docker?

With trailing /

$ docker run --rm ubuntu:16.04 /bin/bash -c "mkdir foo ; mkdir foo-bar ; ls -d foo/ foo-bar/"

foo-bar/
foo/

Without trailing /

$ docker run --rm -it ubuntu:16.04 /bin/bash -c "mkdir foo ; mkdir foo-bar ; ls -d foo foo-bar"

foo
foo-bar

Advertisement

Answer

I found out I get the same behavior using sort command

docker run --rm ubuntu:16.04 /bin/bash -c "echo -e 'foo/nfoo-bar/' | sort"

But the sorting order changes when using sort -d

docker run --rm ubuntu:16.04 /bin/bash -c "echo -e 'foo/nfoo-bar/' | sort -d"

Thanks to David for pointing me in the right direction, this is caused by the locale settings as described here

On bare ubuntu container, POSIX locale is used which has different sorting rules then en_US. I solved my problem by installing en_US locale in the docker image, and sorting works as expected again.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement