Today we had the same problem when working with the format in the docker inspect command. The easiest way to get the last item without a Docker fix was (the expression was broken into lines for readability):
{{ $image := "" }} {{ range split .ContainerImageName "/" }} {{ $image = . }}{{ end }} {{ index (split $image ":") 0 }}
So, in our case, we needed the image name without the address and registry version. For example, the name of the image, for example, registry.domain.local / images / nginx: the latter becomes nginx.
PS: You need Go> = 1.11 to get the job done ( https://github.com/golang/go/issues/10608 )
PPS: The question was about the Go template, but for those who had the same problems with Docker, here are some configuration examples:
1) Using the Go template in daemon.json
cat /etc/docker/daemon.json
{ "log-driver": "syslog", "log-opts": { "syslog-address": "udp://127.0.0.1:20627", "tag": "{{ $image := \"\" }}{{ range split .ContainerImageName \"/\" }}{{ $image = . }}{{ end }}{{ index (split $image \":\") 0 }}/{{.Name}}" }
2) Using the Go pattern with the -f option:
docker inspect \ -f '{{ $image := "" }}{{ range split .Config.Image "/" }}{{ $image = . }}{{ end }}{{ index (split $image ":") 0 }}' \ <container>
source share