github.com/docker/libcompose@v0.4.1-0.20210616120443-2a046c0bdbf2/project/project_port.go (about)

     1  package project
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"golang.org/x/net/context"
     7  )
     8  
     9  // Port returns the public port for a port binding of the specified service.
    10  func (p *Project) Port(ctx context.Context, index int, protocol, serviceName, privatePort string) (string, error) {
    11  	service, err := p.CreateService(serviceName)
    12  	if err != nil {
    13  		return "", err
    14  	}
    15  
    16  	containers, err := service.Containers(ctx)
    17  	if err != nil {
    18  		return "", err
    19  	}
    20  
    21  	if index < 1 || index > len(containers) {
    22  		return "", fmt.Errorf("Invalid index %d", index)
    23  	}
    24  
    25  	return containers[index-1].Port(ctx, fmt.Sprintf("%s/%s", privatePort, protocol))
    26  }