diff --git a/pkg/api/handlers/compat/containers.go b/pkg/api/handlers/compat/containers.go index e6fae78e84..541f1b4352 100644 --- a/pkg/api/handlers/compat/containers.go +++ b/pkg/api/handlers/compat/containers.go @@ -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) diff --git a/test/apiv2/20-containers.at b/test/apiv2/20-containers.at index fa4c6ae46f..57c96331f6 100644 --- a/test/apiv2/20-containers.at +++ b/test/apiv2/20-containers.at @@ -442,7 +442,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