update podman socket output to include also exposed ports

Fixes https://github.com/containers/podman/issues/25851

Co-authored-by: Brent Baude <bbaude@redhat.com>
Signed-off-by: Cesar Goncalves <mail@cesargoncalves.com>
This commit is contained in:
Cesar Goncalves
2025-04-11 23:15:05 +01:00
parent 48423a615d
commit a969dbd73e
2 changed files with 44 additions and 13 deletions

View File

@ -342,23 +342,44 @@ func LibpodToContainer(l *libpod.Container, sz bool) (*handlers.Container, error
}
}
portMappings, err := l.PortMappings()
inspect, err := l.Inspect(false)
if err != nil {
return nil, err
}
ports := make([]container.Port, len(portMappings))
for idx, portMapping := range portMappings {
ports[idx] = container.Port{
IP: portMapping.HostIP,
PrivatePort: portMapping.ContainerPort,
PublicPort: portMapping.HostPort,
Type: portMapping.Protocol,
ports := []container.Port{}
for portKey, bindings := range inspect.NetworkSettings.Ports {
portNum, proto, ok := strings.Cut(portKey, "/")
if !ok {
return nil, fmt.Errorf("PORT/PROTOCOL format required for %q", portKey)
}
containerPort, err := strconv.Atoi(portNum)
if err != nil {
return nil, err
}
if len(bindings) == 0 {
// Exposed but not published
ports = append(ports, container.Port{
PrivatePort: uint16(containerPort),
Type: proto,
})
} else {
for _, b := range bindings {
hostPortInt, err := strconv.Atoi(b.HostPort)
if err != nil {
return nil, fmt.Errorf("invalid HostPort: %v", err)
}
ports = append(ports, container.Port{
IP: b.HostIP,
PrivatePort: uint16(containerPort),
PublicPort: uint16(hostPortInt),
Type: proto,
})
}
}
}
inspect, err := l.Inspect(false)
if err != nil {
return nil, err
}
n, err := json.Marshal(inspect.NetworkSettings)

View File

@ -427,7 +427,17 @@ t GET containers/json 200 \
.[0].Ports[0].PublicPort=8080 \
.[0].Ports[0].Type="tcp"
podman stop bar
podman rm -f bar
# confirm exposed port 8080 shows up in /containers/json
podman run -d --rm --name bar --expose 8080 $IMAGE top
t GET containers/json 200 \
.[0].Ports[0].PrivatePort=8080 \
.[0].Ports[0].Type="tcp"
podman rm -f bar
#compat api list containers sanity checks
podman run -d --rm --name labelcontainer_with --label slartibart=fast $IMAGE top