@@ -342,23 +342,44 @@ func LibpodToContainer(l *libpod.Container, sz bool) (*handlers.Container, error
342
342
}
343
343
}
344
344
345
- portMappings , err := l .PortMappings ( )
345
+ inspect , err := l .Inspect ( false )
346
346
if err != nil {
347
347
return nil , err
348
348
}
349
349
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
+ }
357
382
}
358
- }
359
- inspect , err := l .Inspect (false )
360
- if err != nil {
361
- return nil , err
362
383
}
363
384
364
385
n , err := json .Marshal (inspect .NetworkSettings )
0 commit comments