github.com/xiaobinqt/libcompose@v1.1.0/docker/container/functions.go (about)

     1  package container
     2  
     3  import (
     4  	"github.com/docker/docker/api/types"
     5  	"github.com/docker/docker/api/types/filters"
     6  	"github.com/docker/docker/client"
     7  	"golang.org/x/net/context"
     8  )
     9  
    10  // ListByFilter looks up the hosts containers with the specified filters and
    11  // returns a list of container matching it, or an error.
    12  func ListByFilter(ctx context.Context, clientInstance client.ContainerAPIClient, containerFilters ...map[string][]string) ([]types.Container, error) {
    13  	filterArgs := filters.NewArgs()
    14  
    15  	// FIXME(vdemeester) I don't like 3 for loops >_<
    16  	for _, filter := range containerFilters {
    17  		for key, filterValue := range filter {
    18  			for _, value := range filterValue {
    19  				filterArgs.Add(key, value)
    20  			}
    21  		}
    22  	}
    23  
    24  	return clientInstance.ContainerList(ctx, types.ContainerListOptions{
    25  		All:     true,
    26  		Filters: filterArgs,
    27  	})
    28  }
    29  
    30  // Get looks up the hosts containers with the specified ID
    31  // or name and returns it, or an error.
    32  func Get(ctx context.Context, clientInstance client.ContainerAPIClient, id string) (*types.ContainerJSON, error) {
    33  	container, err := clientInstance.ContainerInspect(ctx, id)
    34  	if err != nil {
    35  		if client.IsErrNotFound(err) {
    36  			return nil, nil
    37  		}
    38  		return nil, err
    39  	}
    40  	return &container, nil
    41  }