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

Method Exec

pkg/compose/watch.go:460–521  ·  pkg/compose/watch.go::tarDockerClient.Exec
(ctx context.Context, containerID string, cmd []string, in io.Reader)

Source from the content-addressed store, hash-verified

458}
459
460func (t tarDockerClient) Exec(ctx context.Context, containerID string, cmd []string, in io.Reader) error {
461 execCreateResp, err := t.s.apiClient().ExecCreate(ctx, containerID, client.ExecCreateOptions{
462 Cmd: cmd,
463 AttachStdout: false,
464 AttachStderr: true,
465 AttachStdin: in != nil,
466 TTY: false,
467 })
468 if err != nil {
469 return err
470 }
471
472 conn, err := t.s.apiClient().ExecAttach(ctx, execCreateResp.ID, client.ExecAttachOptions{
473 TTY: false,
474 })
475 if err != nil {
476 return err
477 }
478 defer conn.Close()
479
480 var eg errgroup.Group
481 if in != nil {
482 eg.Go(func() error {
483 defer func() {
484 _ = conn.CloseWrite()
485 }()
486 _, err := io.Copy(conn.Conn, in)
487 return err
488 })
489 }
490 eg.Go(func() error {
491 _, err := io.Copy(t.s.stdout(), conn.Reader)
492 return err
493 })
494
495 _, err = t.s.apiClient().ExecStart(ctx, execCreateResp.ID, client.ExecStartOptions{
496 TTY: false,
497 Detach: false,
498 })
499 if err != nil {
500 return err
501 }
502
503 // although the errgroup is not tied directly to the context, the operations
504 // in it are reading/writing to the connection, which is tied to the context,
505 // so they won't block indefinitely
506 if err := eg.Wait(); err != nil {
507 return err
508 }
509
510 execResult, err := t.s.apiClient().ExecInspect(ctx, execCreateResp.ID, client.ExecInspectOptions{})
511 if err != nil {
512 return err
513 }
514 if execResult.Running {
515 return errors.New("process still running")
516 }
517 if execResult.ExitCode != 0 {

Callers

nothing calls this directly

Calls 9

apiClientMethod · 0.80
stdoutMethod · 0.80
CloseMethod · 0.65
CopyMethod · 0.65
WaitMethod · 0.65
ExecCreateMethod · 0.45
ExecAttachMethod · 0.45
ExecStartMethod · 0.45
ExecInspectMethod · 0.45

Tested by

no test coverage detected