Skip to content

Commit 857cfb9

Browse files
Merge pull request #25861 from cesargoncalves/main
update podman socket output to include also exposed ports
2 parents 0ed3df8 + a969dbd commit 857cfb9

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

pkg/api/handlers/compat/containers.go

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -342,23 +342,44 @@ func LibpodToContainer(l *libpod.Container, sz bool) (*handlers.Container, error
342342
}
343343
}
344344

345-
portMappings, err := l.PortMappings()
345+
inspect, err := l.Inspect(false)
346346
if err != nil {
347347
return nil, err
348348
}
349349

350-
ports := make([]container.Port, len(portMappings))
351-
for idx, portMapping := range portMappings {
352-
ports[idx] = container.Port{
353-
IP: portMapping.HostIP,
354-
PrivatePort: portMapping.ContainerPort,
355-
PublicPort: portMapping.HostPort,
356-
Type: portMapping.Protocol,
350+
ports := []container.Port{}
351+
for portKey, bindings := range inspect.NetworkSettings.Ports {
352+
portNum, proto, ok := strings.Cut(portKey, "/")
353+
if !ok {
354+
return nil, fmt.Errorf("PORT/PROTOCOL format required for %q", portKey)
355+
}
356+
357+
containerPort, err := strconv.Atoi(portNum)
358+
if err != nil {
359+
return nil, err
360+
}
361+
362+
if len(bindings) == 0 {
363+
// Exposed but not published
364+
ports = append(ports, container.Port{
365+
PrivatePort: uint16(containerPort),
366+
Type: proto,
367+
})
368+
} else {
369+
for _, b := range bindings {
370+
hostPortInt, err := strconv.Atoi(b.HostPort)
371+
if err != nil {
372+
return nil, fmt.Errorf("invalid HostPort: %v", err)
373+
}
374+
375+
ports = append(ports, container.Port{
376+
IP: b.HostIP,
377+
PrivatePort: uint16(containerPort),
378+
PublicPort: uint16(hostPortInt),
379+
Type: proto,
380+
})
381+
}
357382
}
358-
}
359-
inspect, err := l.Inspect(false)
360-
if err != nil {
361-
return nil, err
362383
}
363384

364385
n, err := json.Marshal(inspect.NetworkSettings)

test/apiv2/20-containers.at

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,17 @@ t GET containers/json 200 \
442442
.[0].Ports[0].PublicPort=8080 \
443443
.[0].Ports[0].Type="tcp"
444444

445-
podman stop bar
445+
podman rm -f bar
446+
447+
# confirm exposed port 8080 shows up in /containers/json
448+
449+
podman run -d --rm --name bar --expose 8080 $IMAGE top
450+
451+
t GET containers/json 200 \
452+
.[0].Ports[0].PrivatePort=8080 \
453+
.[0].Ports[0].Type="tcp"
454+
455+
podman rm -f bar
446456

447457
#compat api list containers sanity checks
448458
podman run -d --rm --name labelcontainer_with --label slartibart=fast $IMAGE top

0 commit comments

Comments
 (0)