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

Method runHook

pkg/compose/hook.go:34–97  ·  pkg/compose/hook.go::composeService.runHook
(ctx context.Context, ctr container.Summary, service types.ServiceConfig, hook types.ServiceHook, listener api.ContainerEventListener)

Source from the content-addressed store, hash-verified

32)
33
34func (s *composeService) runHook(ctx context.Context, ctr container.Summary, service types.ServiceConfig, hook types.ServiceHook, listener api.ContainerEventListener) error {
35 wOut := utils.GetWriter(func(line string) {
36 listener(api.ContainerEvent{
37 Type: api.HookEventLog,
38 Source: getContainerNameWithoutProject(ctr) + " ->",
39 ID: ctr.ID,
40 Service: service.Name,
41 Line: line,
42 })
43 })
44 defer wOut.Close() //nolint:errcheck
45
46 detached := listener == nil
47 exec, err := s.apiClient().ExecCreate(ctx, ctr.ID, client.ExecCreateOptions{
48 User: hook.User,
49 Privileged: hook.Privileged,
50 Env: ToMobyEnv(hook.Environment),
51 WorkingDir: hook.WorkingDir,
52 Cmd: hook.Command,
53 AttachStdout: !detached,
54 AttachStderr: !detached,
55 })
56 if err != nil {
57 return err
58 }
59
60 if detached {
61 return s.runWaitExec(ctx, exec.ID, service, listener)
62 }
63
64 attachOptions := client.ExecAttachOptions{
65 TTY: service.Tty,
66 }
67 if service.Tty {
68 height, width := s.stdout().GetTtySize()
69 attachOptions.ConsoleSize = client.ConsoleSize{
70 Width: width,
71 Height: height,
72 }
73 }
74 attach, err := s.apiClient().ExecAttach(ctx, exec.ID, attachOptions)
75 if err != nil {
76 return err
77 }
78 defer attach.Close()
79
80 if service.Tty {
81 _, err = io.Copy(wOut, attach.Reader)
82 } else {
83 _, err = stdcopy.StdCopy(wOut, wOut, attach.Reader)
84 }
85 if err != nil {
86 return err
87 }
88
89 inspected, err := s.apiClient().ExecInspect(ctx, exec.ID, client.ExecInspectOptions{})
90 if err != nil {
91 return err

Callers 5

restartMethod · 0.95
startServiceContainerMethod · 0.95
stopContainerMethod · 0.95
TestRunHook_ConsoleSizeFunction · 0.80

Calls 11

apiClientMethod · 0.95
runWaitExecMethod · 0.95
stdoutMethod · 0.95
GetWriterFunction · 0.92
ToMobyEnvFunction · 0.85
CloseMethod · 0.65
CopyMethod · 0.65
ExecCreateMethod · 0.45
ExecAttachMethod · 0.45
ExecInspectMethod · 0.45

Tested by 1

TestRunHook_ConsoleSizeFunction · 0.64