Description
I need to List all port bindings in Docker so I found an example in documentation but in my case it throws an error.
Steps to reproduce the issue:
$ docker run --rm -itd --name documentation-issue mysql:5.7 bash
d24af5958f714cede788ae7db04abae2fc4e515d8fa32ce038583eebe8a9d540
$ docker inspect format='{{range $p, $conf := .NetworkSettings.Ports}}{{$p}} -> {(index $conf 0).HostPort}}{{end}}' documentation-issue
To stop container, execute:
$ docker stop documentation-issue
documentation-issue
Describe the results you received:
Template parsing error: template: :1:75: executing "" at <index $conf 0>: error calling index: index of untyped nil
Describe the results you expected:
In go templates there is with action, using that everything works as expected:
$ docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{with $conf}}{{(index . 0).HostPort}}{{else}}none{{end}} {{end}}' documentation-issue
mysql:5.7 3306/tcp -> none 33060/tcp -> none
$ docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{with $conf}}{{(index . 0).HostPort}}{{end}} {{end}}' documentation-issue
mysql:5.7 3306/tcp -> 33060/tcp ->
Output of docker version:
Client:
Version: 20.10.17
API version: 1.41
Go version: go1.17.11
Git commit: 100c701
Built: Mon Jun 6 22:56:42 2022
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.17
API version: 1.41 (minimum version 1.12)
Go version: go1.17.11
Git commit: a89b842
Built: Mon Jun 6 23:01:23 2022
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.6
GitCommit: 10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
runc:
Version: 1.1.2
GitCommit: v1.1.2-0-ga916309
docker-init:
Version: 0.19.0
GitCommit: de40ad0
Output of docker info:
Client:
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc., v0.8.2)
compose: Docker Compose (Docker Inc., v2.9.0)
Server:
Containers: 126
Running: 8
Paused: 0
Stopped: 118
Images: 330
Server Version: 20.10.17
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: systemd
Cgroup Version: 2
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
runc version: v1.1.2-0-ga916309
init version: de40ad0
Security Options:
apparmor
seccomp
Profile: default
cgroupns
Kernel Version: 5.10.0-13-amd64
Operating System: Debian GNU/Linux 11 (bullseye)
OSType: linux
Architecture: x86_64
CPUs: 8
Total Memory: 23.37GiB
Name: max-pc
ID: D5HN:MTM3:BVNY:UFK2:62BI:EJLW:V3MG:FFKO:L3W4:JWH7:D5GV:TCYM
Docker Root Dir: /var/lib/docker
Debug Mode: false
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.):
Description
I need to List all port bindings in Docker so I found an example in documentation but in my case it throws an error.
Steps to reproduce the issue:
To stop container, execute:
Describe the results you received:
Describe the results you expected:
In go templates there is
withaction, using that everything works as expected:Output of
docker version:Output of
docker info:Additional environment details (AWS, VirtualBox, physical, etc.):