I created a docker container but cannot seem to parse its properties with docker-cli and its format parameter (which uses Go templates).
Any idea greatly appreciated.
Start a docker container, e.g. go to https://birthday.play-with-docker.com/cli-formating/ and click on the docker command in the Prepare the environment section
Choose a property for parsing, e.g. Ports. Printing it with
docker container ls --format '{{ .Ports }}'should yield0.0.0.0:80->80/tcp.Trying to get the port range part after the colon, split the property at ":" (
docker container ls --format '{{ split .Ports ":" }}') which yields array[0.0.0.0 80->80/tcp]The return type (
docker container ls --format '{{ printf "%T" (split .Ports ":") }}') is[]string.The string array has a length of 2 (
docker container ls --format '{{ len (split .Ports ":") }}') .Accessing index value 0 (
docker container ls --format '{{ index (split .Ports ":") 0 }}') yields0.0.0.0as expected.Accessing index value 1 (
docker container ls --format '{{ index (split .Ports ":") 1 }}') yields failed to execute template: template: :1:2: executing "" at <index (split .Ports ":") 1>: error calling index: reflect: slice index out of range instead of80->80/tcp.
In order to access string array elements, I found a solution. As this question focuses on the Go templates part in the
--formatparameter, I will just write about that.The
{{ index (split .Ports ":") 1 }}yields an error becauseindexsimply is the wrong function in this case. To access an array element, useslice:Access the second array element (zero-based index) with
{{ slice (split .Ports ":") 1 }}, this yields[80->80/tcp].If you need
stringoutput, you can convert thesliceobject withjoinusing""as separator, removing the square brackets in the output:{{ join ( slice (split .Image ":") 1 ) "" }}yields80->80/tcp.The complete command is
docker container ls --format '{{ join ( slice ( split .Ports ":" ) 1 ) "" }}'. Keep in mind that Go templates use a kind of prefix notation which might not seem that common.