File: start.go

package info (click to toggle)
docker-compose 2.32.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,300 kB
  • sloc: makefile: 113; sh: 2
file content (355 lines) | stat: -rw-r--r-- 10,984 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
   Copyright 2020 Docker Compose CLI authors

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package compose

import (
	"context"
	"errors"
	"fmt"
	"strings"
	"time"

	containerType "github.com/docker/docker/api/types/container"
	"github.com/docker/docker/errdefs"

	"github.com/docker/compose/v2/pkg/api"
	"github.com/docker/compose/v2/pkg/progress"
	"github.com/docker/compose/v2/pkg/utils"

	"github.com/compose-spec/compose-go/v2/types"
	moby "github.com/docker/docker/api/types"
	"github.com/docker/docker/api/types/filters"
	"golang.org/x/sync/errgroup"
)

func (s *composeService) Start(ctx context.Context, projectName string, options api.StartOptions) error {
	return progress.Run(ctx, func(ctx context.Context) error {
		return s.start(ctx, strings.ToLower(projectName), options, nil)
	}, s.stdinfo())
}

func (s *composeService) start(ctx context.Context, projectName string, options api.StartOptions, listener api.ContainerEventListener) error {
	project := options.Project
	if project == nil {
		var containers Containers
		containers, err := s.getContainers(ctx, projectName, oneOffExclude, true)
		if err != nil {
			return err
		}

		project, err = s.projectFromName(containers, projectName, options.AttachTo...)
		if err != nil {
			return err
		}
	}

	// use an independent context tied to the errgroup for background attach operations
	// the primary context is still used for other operations
	// this means that once any attach operation fails, all other attaches are cancelled,
	// but an attach failing won't interfere with the rest of the start
	eg, attachCtx := errgroup.WithContext(ctx)
	if listener != nil {
		_, err := s.attach(attachCtx, project, listener, options.AttachTo)
		if err != nil {
			return err
		}

		eg.Go(func() error {
			// it's possible to have a required service whose log output is not desired
			// (i.e. it's not in the attach set), so watch everything and then filter
			// calls to attach; this ensures that `watchContainers` blocks until all
			// required containers have exited, even if their output is not being shown
			attachTo := utils.NewSet[string](options.AttachTo...)
			required := utils.NewSet[string](options.Services...)
			toWatch := attachTo.Union(required).Elements()

			containers, err := s.getContainers(ctx, projectName, oneOffExclude, true, toWatch...)
			if err != nil {
				return err
			}

			// N.B. this uses the parent context (instead of attachCtx) so that the watch itself can
			// continue even if one of the log streams fails
			return s.watchContainers(ctx, project.Name, toWatch, required.Elements(), listener, containers,
				func(container moby.Container, _ time.Time) error {
					svc := container.Labels[api.ServiceLabel]
					if attachTo.Has(svc) {
						return s.attachContainer(attachCtx, container, listener)
					}

					// HACK: simulate an "attach" event
					listener(api.ContainerEvent{
						Type:      api.ContainerEventAttach,
						Container: getContainerNameWithoutProject(container),
						ID:        container.ID,
						Service:   svc,
					})
					return nil
				}, func(container moby.Container, _ time.Time) error {
					listener(api.ContainerEvent{
						Type:      api.ContainerEventAttach,
						Container: "", // actual name will be set by start event
						ID:        container.ID,
						Service:   container.Labels[api.ServiceLabel],
					})
					return nil
				})
		})
	}

	var containers Containers
	containers, err := s.apiClient().ContainerList(ctx, containerType.ListOptions{
		Filters: filters.NewArgs(
			projectFilter(project.Name),
			oneOffFilter(false),
		),
		All: true,
	})
	if err != nil {
		return err
	}

	err = InDependencyOrder(ctx, project, func(c context.Context, name string) error {
		service, err := project.GetService(name)
		if err != nil {
			return err
		}

		return s.startService(ctx, project, service, containers, listener, options.WaitTimeout)
	})
	if err != nil {
		return err
	}

	if options.Wait {
		depends := types.DependsOnConfig{}
		for _, s := range project.Services {
			depends[s.Name] = types.ServiceDependency{
				Condition: getDependencyCondition(s, project),
				Required:  true,
			}
		}
		if options.WaitTimeout > 0 {
			withTimeout, cancel := context.WithTimeout(ctx, options.WaitTimeout)
			ctx = withTimeout
			defer cancel()
		}

		err = s.waitDependencies(ctx, project, project.Name, depends, containers, 0)
		if err != nil {
			if errors.Is(ctx.Err(), context.DeadlineExceeded) {
				return fmt.Errorf("application not healthy after %s", options.WaitTimeout)
			}
			return err
		}
	}

	return eg.Wait()
}

// getDependencyCondition checks if service is depended on by other services
// with service_completed_successfully condition, and applies that condition
// instead, or --wait will never finish waiting for one-shot containers
func getDependencyCondition(service types.ServiceConfig, project *types.Project) string {
	for _, services := range project.Services {
		for dependencyService, dependencyConfig := range services.DependsOn {
			if dependencyService == service.Name && dependencyConfig.Condition == types.ServiceConditionCompletedSuccessfully {
				return types.ServiceConditionCompletedSuccessfully
			}
		}
	}
	return ServiceConditionRunningOrHealthy
}

type containerWatchFn func(container moby.Container, t time.Time) error

// watchContainers uses engine events to capture container start/die and notify ContainerEventListener
func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
	projectName string, services, required []string,
	listener api.ContainerEventListener, containers Containers, onStart, onRecreate containerWatchFn,
) error {
	if len(containers) == 0 {
		return nil
	}
	if len(required) == 0 {
		required = services
	}

	unexpected := utils.NewSet[string](required...).Diff(utils.NewSet[string](services...))
	if len(unexpected) != 0 {
		return fmt.Errorf(`required service(s) "%s" not present in watched service(s) "%s"`,
			strings.Join(unexpected.Elements(), ", "),
			strings.Join(services, ", "))
	}

	// predicate to tell if a container we receive event for should be considered or ignored
	ofInterest := func(c moby.Container) bool {
		if len(services) > 0 {
			// we only watch some services
			return utils.Contains(services, c.Labels[api.ServiceLabel])
		}
		return true
	}

	// predicate to tell if a container we receive event for should be watched until termination
	isRequired := func(c moby.Container) bool {
		if len(services) > 0 && len(required) > 0 {
			// we only watch some services
			return utils.Contains(required, c.Labels[api.ServiceLabel])
		}
		return true
	}

	var (
		expected = utils.NewSet[string]()
		watched  = map[string]int{}
		replaced []string
	)
	for _, c := range containers {
		if isRequired(c) {
			expected.Add(c.ID)
		}
		watched[c.ID] = 0
	}

	ctx, stop := context.WithCancel(ctx)
	err := s.Events(ctx, projectName, api.EventsOptions{
		Services: services,
		Consumer: func(event api.Event) error {
			defer func() {
				// after consuming each event, check to see if we're done
				if len(expected) == 0 {
					stop()
				}
			}()
			inspected, err := s.apiClient().ContainerInspect(ctx, event.Container)
			if err != nil {
				if errdefs.IsNotFound(err) {
					// it's possible to get "destroy" or "kill" events but not
					// be able to inspect in time before they're gone from the
					// API, so just remove the watch without erroring
					delete(watched, event.Container)
					expected.Remove(event.Container)
					return nil
				}
				return err
			}
			container := moby.Container{
				ID:     inspected.ID,
				Names:  []string{inspected.Name},
				Labels: inspected.Config.Labels,
			}
			name := getContainerNameWithoutProject(container)
			service := container.Labels[api.ServiceLabel]
			switch event.Status {
			case "stop":
				if inspected.State.Running {
					// on sync+restart action the container stops -> dies -> start -> restart
					// we do not want to stop the current container, we want to restart it
					return nil
				}
				if _, ok := watched[container.ID]; ok {
					eType := api.ContainerEventStopped
					if utils.Contains(replaced, container.ID) {
						utils.Remove(replaced, container.ID)
						eType = api.ContainerEventRecreated
					}
					listener(api.ContainerEvent{
						Type:      eType,
						Container: name,
						ID:        container.ID,
						Service:   service,
						ExitCode:  inspected.State.ExitCode,
					})
				}

				delete(watched, container.ID)
				expected.Remove(container.ID)
			case "die":
				restarted := watched[container.ID]
				watched[container.ID] = restarted + 1
				// Container terminated.
				willRestart := inspected.State.Restarting
				if inspected.State.Running {
					// on sync+restart action inspected.State.Restarting is false,
					// however the container is already running before it restarts
					willRestart = true
				}

				eType := api.ContainerEventExit
				if utils.Contains(replaced, container.ID) {
					utils.Remove(replaced, container.ID)
					eType = api.ContainerEventRecreated
				}

				listener(api.ContainerEvent{
					Type:       eType,
					Container:  name,
					ID:         container.ID,
					Service:    service,
					ExitCode:   inspected.State.ExitCode,
					Restarting: willRestart,
				})

				if !willRestart {
					// we're done with this one
					delete(watched, container.ID)
					expected.Remove(container.ID)
				}
			case "start":
				count, ok := watched[container.ID]
				mustAttach := ok && count > 0 // Container restarted, need to re-attach
				if !ok {
					// A new container has just been added to service by scale
					watched[container.ID] = 0
					expected.Add(container.ID)
					mustAttach = true
				}
				if mustAttach {
					// Container restarted, need to re-attach
					err := onStart(container, event.Timestamp)
					if err != nil {
						return err
					}
				}
			case "create":
				if id, ok := container.Labels[api.ContainerReplaceLabel]; ok {
					replaced = append(replaced, id)
					err = onRecreate(container, event.Timestamp)
					if err != nil {
						return err
					}
					if expected.Has(id) {
						expected.Add(inspected.ID)
						expected.Add(container.ID)
					}
					watched[container.ID] = 1
				} else if ofInterest(container) {
					watched[container.ID] = 1
					if isRequired(container) {
						expected.Add(container.ID)
					}
				}
			}
			return nil
		},
	})
	if errors.Is(ctx.Err(), context.Canceled) {
		return nil
	}
	return err
}