MCPcopy
hub / github.com/docker/compose / getSpecifiedContainer

Method getSpecifiedContainer

pkg/compose/containers.go:90–124  ·  pkg/compose/containers.go::composeService.getSpecifiedContainer
(ctx context.Context, projectName string, oneOff oneOff, all bool, serviceName string, containerIndex int)

Source from the content-addressed store, hash-verified

88}
89
90func (s *composeService) getSpecifiedContainer(ctx context.Context, projectName string, oneOff oneOff, all bool, serviceName string, containerIndex int) (container.Summary, error) {
91 defaultFilters := getDefaultFilters(projectName, oneOff, serviceName)
92 if containerIndex > 0 {
93 defaultFilters.Add("label", containerNumberFilter(containerIndex))
94 }
95 res, err := s.apiClient().ContainerList(ctx, client.ContainerListOptions{
96 Filters: defaultFilters,
97 All: all,
98 })
99 if err != nil {
100 return container.Summary{}, err
101 }
102 containers := res.Items
103 if len(containers) < 1 {
104 if containerIndex > 0 {
105 return container.Summary{}, fmt.Errorf("service %q is not running container #%d", serviceName, containerIndex)
106 }
107 return container.Summary{}, fmt.Errorf("service %q is not running", serviceName)
108 }
109
110 // Sort by container number first, then put one-off containers at the end
111 sort.Slice(containers, func(i, j int) bool {
112 numberLabelX, _ := strconv.Atoi(containers[i].Labels[api.ContainerNumberLabel])
113 numberLabelY, _ := strconv.Atoi(containers[j].Labels[api.ContainerNumberLabel])
114 IsOneOffLabelTrueX := containers[i].Labels[api.OneoffLabel] == "True"
115 IsOneOffLabelTrueY := containers[j].Labels[api.OneoffLabel] == "True"
116
117 if IsOneOffLabelTrueX || IsOneOffLabelTrueY {
118 return !IsOneOffLabelTrueX && IsOneOffLabelTrueY
119 }
120
121 return numberLabelX < numberLabelY
122 })
123 return containers[0], nil
124}
125
126// containerPredicate define a predicate we want container to satisfy for filtering operations
127type containerPredicate func(c container.Summary) bool

Callers 7

LogsMethod · 0.95
AttachMethod · 0.95
exportMethod · 0.95
getExecTargetMethod · 0.95
commitMethod · 0.95
PortMethod · 0.95

Calls 5

apiClientMethod · 0.95
getDefaultFiltersFunction · 0.85
containerNumberFilterFunction · 0.85
AddMethod · 0.45
ContainerListMethod · 0.45

Tested by

no test coverage detected