MCPcopy
hub / github.com/moby/moby / Exec

Function Exec

integration/internal/container/exec.go:49–86  ·  view source on GitHub ↗

Exec executes a command inside a container, returning the result containing stdout, stderr, and exit code. Note: - this is a synchronous operation; - cmd stdin is closed.

(ctx context.Context, apiClient client.APIClient, id string, cmd []string, ops ...func(*client.ExecCreateOptions))

Source from the content-addressed store, hash-verified

47// - this is a synchronous operation;
48// - cmd stdin is closed.
49func Exec(ctx context.Context, apiClient client.APIClient, id string, cmd []string, ops ...func(*client.ExecCreateOptions)) (ExecResult, error) {
50 // prepare exec
51 execOptions := client.ExecCreateOptions{
52 AttachStdout: true,
53 AttachStderr: true,
54 Cmd: cmd,
55 }
56
57 for _, op := range ops {
58 op(&execOptions)
59 }
60
61 res, err := apiClient.ExecCreate(ctx, id, execOptions)
62 if err != nil {
63 return ExecResult{}, err
64 }
65 execID := res.ID
66
67 // run it, with stdout/stderr attached
68 aresp, err := apiClient.ExecAttach(ctx, execID, client.ExecAttachOptions{})
69 if err != nil {
70 return ExecResult{}, err
71 }
72
73 // read the output
74 s, err := demultiplexStreams(ctx, aresp.HijackedResponse)
75 if err != nil {
76 return ExecResult{}, err
77 }
78
79 // get the exit code
80 inspect, err := apiClient.ExecInspect(ctx, execID, client.ExecInspectOptions{})
81 if err != nil {
82 return ExecResult{}, err
83 }
84
85 return ExecResult{ExitCode: inspect.ExitCode, outBuffer: &s.stdout, errBuffer: &s.stderr}, nil
86}
87
88// ExecT calls Exec() and aborts the test if an error occurs.
89func ExecT(ctx context.Context, t testing.TB, apiClient client.APIClient, id string, cmd []string, ops ...func(*client.ExecCreateOptions)) ExecResult {

Callers 15

TestExecSocketDeniedFunction · 0.92
TestExecUserFunction · 0.92
TestExecWithGroupAddFunction · 0.92
TestExecConsoleSizeFunction · 0.92
TestFailedExecExitCodeFunction · 0.92
TestDaemonHostGatewayIPFunction · 0.92
TestNISDomainnameFunction · 0.92

Calls 4

demultiplexStreamsFunction · 0.85
ExecCreateMethod · 0.65
ExecAttachMethod · 0.65
ExecInspectMethod · 0.65

Tested by 15

TestExecSocketDeniedFunction · 0.74
TestExecUserFunction · 0.74
TestExecWithGroupAddFunction · 0.74
TestExecConsoleSizeFunction · 0.74
TestFailedExecExitCodeFunction · 0.74
TestDaemonHostGatewayIPFunction · 0.74
TestNISDomainnameFunction · 0.74

Used in the wild real call sites across dependent graphs

searching dependent graphs…