Cli: inspect does not like this syntax

Created on 25 May 2018  路  7Comments  路  Source: docker/cli

Description

I cannot use index function of text/template. What am I wrong here?

It seems to me that the quotes " are stripped before getting parsed by text/template

Thank you.

in Powershell

images quan*$% = $ docker image inspect 4a89600a6cab -f '{{index .ContainerConfig.Labels "key"}}'
Template parsing error: template: :1: function "key" not defined

images quan*$% = $ docker image inspect 4a89600a6cab -f '{{index .ContainerConfig.Labels "0"}}'

Template parsing error: template: :1:2: executing "" at <index .ContainerConf...>: error calling index: value has type int; should be string

The pattern works in Go directly

const format = `{{index .ContainerConfig.Labels "key"}}`

image, _, err := dockerClient.ImageInspectWithRaw(ctxBackground, "4a89600a6cab")

t := template.Must(template.New("format").Parse(format))

if err := t.Execute(os.Stdout, image); err != nil {
    log.Println("error", err)
}

Output of docker version:

Client:
 Version:      18.03.1-ce
 API version:  1.37
 Go version:   go1.9.5
 Git commit:   9ee9f40
 Built:        Thu Apr 26 07:12:48 2018
 OS/Arch:      windows/amd64
 Experimental: false
 Orchestrator: swarm

Server:
 Engine:
  Version:      18.03.1-ce
  API version:  1.37 (minimum version 1.12)
  Go version:   go1.9.5
  Git commit:   9ee9f40
  Built:        Thu Apr 26 07:22:38 2018
  OS/Arch:      linux/amd64
  Experimental: false

Output of docker info:

Containers: 49
 Running: 0
 Paused: 0
 Stopped: 49
Images: 354
Server Version: 18.03.1-ce
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 773c489c9c1b21a6d78b5c538cd395416ec50f88
runc version: 4fc53a81fb7c994640722ac585fa9ca548971871
init version: 949e6fa
Security Options:
 seccomp
  Profile: default
Kernel Version: 4.9.87-linuxkit-aufs
Operating System: Docker for Windows
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.837GiB
Name: linuxkit-00155d0bb108
ID: N2ZG:RACO:SXHZ:OULG:VDEF:3FD3:734K:LHGL:V3CX:7Z7C:5SM4:VXY4
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): true
 File Descriptors: 19
 Goroutines: 36
 System Time: 2018-05-25T06:11:23.9581669Z
 EventsListeners: 1
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

Additional environment details (AWS, VirtualBox, physical, etc.):
Docker for Windows

(links https://github.com/moby/moby/issues/37152)

kinquestion platfordesktop platforwindows

Most helpful comment

Ah, yes, that seems to work

docker build -t psdocker -<<EOF
FROM jess/powershell
COPY --from=docker:18.03.1 /usr/local/bin/docker /usr/local/bin/docker
EOF
docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock psdocker 

PS /> docker image inspect --format "{{index .Config.Labels \`"foo\`"}}" imageindexie
bar

To get rid of the backslashes for escaping, single quotes work as well;

PS /> docker image inspect --format '{{index .Config.Labels `"foo`"}}' imageindexie
bar

All 7 comments

Interesting; not able to reproduce this on Docker for Mac;

On a container;

$ docker container create --label foo=bar --name indexie busybox
$ docker container inspect --format '{{index .Config.Labels "foo"}}' indexie 
bar

On an image (note that you are likely looking for .Config.Labels, not .ContainerConfig.Labels):

$ docker image build -t imageindexie --label foo=bar -<<EOF
FROM busybox
LABEL bar=baz
EOF

$ docker image inspect --format '{{index .Config.Labels "foo"}}' imageindexie
bar

$ docker image inspect --format '{{index .Config.Labels "bar"}}' imageindexie
baz

$ docker image inspect --format '{{index .ContainerConfig.Labels "foo"}}' imageindexie
bar

$ docker image inspect --format '{{index .ContainerConfig.Labels "bar"}}' imageindexie
baz

@itscaro do you see the same problem if you try this in a Bash shell? Wondering if it's something in PowerShell that strips the quotes

@thaJeztah in bash, it works, it's PowerShell, but I don't know why the quotes are stripped :(

Not using PowerShell myself, so reading up on what rules it has; https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-6

To make double-quotation marks appear in a string, enclose the entire string in single quotation marks. For example:
'As they say, "live and learn."'

Wonder if it's a bug in PowerShell

In PowerShell the command should be escaped like following

docker image inspect 4a89600a6cab -f "{{index .Config.Labels \`"key\`"}}"

Thank you for your time

Question on PowerShell https://github.com/PowerShell/PowerShell/issues/6935

Ah, yes, that seems to work

docker build -t psdocker -<<EOF
FROM jess/powershell
COPY --from=docker:18.03.1 /usr/local/bin/docker /usr/local/bin/docker
EOF
docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock psdocker 

PS /> docker image inspect --format "{{index .Config.Labels \`"foo\`"}}" imageindexie
bar

To get rid of the backslashes for escaping, single quotes work as well;

PS /> docker image inspect --format '{{index .Config.Labels `"foo`"}}' imageindexie
bar

Still wondering why that doesn't match the documentation https://github.com/docker/cli/issues/1082#issuecomment-391986900

Was this page helpful?
0 / 5 - 0 ratings