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

Method RunOneOffContainer

pkg/compose/run.go:44–89  ·  pkg/compose/run.go::composeService.RunOneOffContainer
(ctx context.Context, project *types.Project, opts api.RunOptions)

Source from the content-addressed store, hash-verified

42}
43
44func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts api.RunOptions) (int, error) {
45 result, err := s.prepareRun(ctx, project, opts)
46 if err != nil {
47 return 0, err
48 }
49
50 // remove cancellable context signal handler so we can forward signals to container without compose exiting
51 signal.Reset()
52
53 sigc := make(chan os.Signal, 128)
54 signal.Notify(sigc)
55 go cmd.ForwardAllSignals(ctx, s.apiClient(), result.containerID, sigc)
56 defer signal.Stop(sigc)
57
58 // If the service has post_start hooks, set up a goroutine that waits for
59 // the container to start and then executes them. This is needed because
60 // cmd.RunStart both starts and attaches to the container in one call,
61 // so we can't run hooks sequentially between start and attach.
62 var hookErrCh chan error
63 if len(result.service.PostStart) > 0 {
64 hookErrCh = make(chan error, 1)
65 go func() {
66 hookErrCh <- s.runPostStartHooksOnEvent(ctx, result.containerID, result.service, result.created)
67 }()
68 }
69
70 err = cmd.RunStart(ctx, s.dockerCli, &cmd.StartOptions{
71 OpenStdin: !opts.Detach && opts.Interactive,
72 Attach: !opts.Detach,
73 Containers: []string{result.containerID},
74 DetachKeys: s.configFile().DetachKeys,
75 })
76
77 // Wait for hooks to complete if they were started
78 if hookErrCh != nil {
79 if hookErr := <-hookErrCh; hookErr != nil && err == nil {
80 err = hookErr
81 }
82 }
83
84 var stErr cli.StatusError
85 if errors.As(err, &stErr) {
86 return stErr.StatusCode, nil
87 }
88 return 0, err
89}
90
91// runPostStartHooksOnEvent listens for the container's start event and executes
92// post_start lifecycle hooks once the container is running.

Callers

nothing calls this directly

Calls 5

prepareRunMethod · 0.95
apiClientMethod · 0.95
configFileMethod · 0.95
StopMethod · 0.65

Tested by

no test coverage detected