I have a strange result with docker container ls --format
when I want to shorten the image id or name. I will continue to investigate the issue but I think it is interesting enough to share.
Start a PHP container
docker run -d --name php php:8.0-apache
Note: It doesn’t matter which container I start. I chose this because the name of the image is long enough. You will see.
Show the first 11 characters of the images of the running containers:
docker container ls --all --no-trunc --filter "ancestor=php:8.0-apache" --format '{{ slice .Image 0 11 }}'
Note: I used --filter
to make sure the error message is not caused by another image
The result:
failed to execute template: template: :1:3: executing "" at <slice .Image 0 11>: error calling slice: index out of range: 1
However, when I want to get the first 10 characters it works:
docker container ls --all --no-trunc --filter "ancestor=php:8.0-apache" --format '{{ slice .Image 0 10 }}'
The result:
php:8.0-ap
So i thought I used slice incorrectly but slice works with a static string:
docker container ls --all --no-trunc --filter "ancestor=php:8.0-apache" --format '{{ slice "php:8.0-apache" 0 11 }}'
Then I thought .Image
was actually an object so that was why I coudn’t manipulate it as a string. So I found this:
It is about the last characters not the first but it was the closest I found.
“pyhedgehog” wrote this:
.Image
field is of some strange type (not string), so you should evaluate it to string (using
But using print doesn’t change anything:
docker container ls --all --no-trunc --filter "ancestor=php:8.0-apache" --format '{{ $i:=(print .Image) }}{{ slice $i 0 11 }}'
Result:
failed to execute template: template: :1:27: executing "" at <slice $i 0 11>: error calling slice: index out of range: 11
But… in the template string on stackoverflow there is an “if” statement. So I tried to play with that:
docker container ls --all --no-trunc --filter "ancestor=php:8.0-apache" --format '{{ if ge (.Image|len) 11 }}{{ slice .Image 0 11 }}{{end}}'
Note: I checked if the length of the image name was greater then 11 which always true for this image. It worked:
php:8.0-apa
If I try it with 10 iin the condition nstead of 11, it failes again:
docker container ls --all --no-trunc --filter "ancestor=php:8.0-apache" --format '{{ if ge (.Image|len) 10 }}{{ slice .Image 0 11 }}{{end}}'
Result:
failed to execute template: template: :1:30: executing "" at <slice .Image 0 11>: error calling slice: index out of range: 11
It seems the implicit conversion happens when I check the length of the name. The number in the condition must be greater then 10 to be able to slice more than 10 characters.
I also tried this:
docker container ls --all --no-trunc --filter "ancestor=php:8.0-apache" --format '{{ slice (printf "%s" .Image ) 0 11 }}'
Failed again.
I couldn’t explain it so I tried to use slice with docker image ls --format
. Surprisingly it worked without errors.
docker image ls --filter "reference=php:8.0-apache" --format '{{slice (printf "%s:%s" .Repository .Tag) 0 11}}'
I could use the printf function to get the first characters of the name, but I also want to set an offset since I am working on a demonstration in which I have Image IDs starting with sha256:
which I also want to cut out and I couldn’t do it with printf. This last sentence is just an explanation why I am playing with slice, not a question.
Can anyone explain it? I could use the ugly workaround with the condition but that looks bad in a demonstration.