MCPcopy
hub / github.com/coder/mux / streamDockerRun

Function streamDockerRun

src/node/runtime/DockerRuntime.ts:184–252  ·  view source on GitHub ↗

* Run docker run with streaming output (for image pull progress). * Streams stdout/stderr to initLogger for visibility during image pulls.

(
  containerName: string,
  image: string,
  initLogger: InitLogger,
  options?: { abortSignal?: AbortSignal; shareCredentials?: boolean; timeoutMs?: number }
)

Source from the content-addressed store, hash-verified

182 * Streams stdout/stderr to initLogger for visibility during image pulls.
183 */
184function streamDockerRun(
185 containerName: string,
186 image: string,
187 initLogger: InitLogger,
188 options?: { abortSignal?: AbortSignal; shareCredentials?: boolean; timeoutMs?: number }
189): Promise<DockerCommandResult> {
190 const { abortSignal, shareCredentials, timeoutMs = 600000 } = options ?? {};
191
192 return new Promise((resolve) => {
193 let stdout = "";
194 let stderr = "";
195 let resolved = false;
196
197 const finish = (result: DockerCommandResult) => {
198 if (resolved) return;
199 resolved = true;
200 clearTimeout(timer);
201 abortSignal?.removeEventListener("abort", abortHandler);
202 resolve(result);
203 };
204
205 // Build docker run args
206 const dockerArgs = ["run", "-d", "--name", containerName];
207 if (shareCredentials) {
208 dockerArgs.push(...buildCredentialArgs());
209 }
210 dockerArgs.push(image, "sleep", "infinity");
211
212 // Use spawn for streaming output - array args don't need shell escaping
213 const child = spawn("docker", dockerArgs);
214
215 const timer = setTimeout(() => {
216 child.kill();
217 void runDockerCommand(`docker rm -f ${containerName}`, 10000);
218 finish({ exitCode: -1, stdout, stderr: "Container creation timed out" });
219 }, timeoutMs);
220
221 const abortHandler = () => {
222 child.kill();
223 // Container might have been created before abort - clean it up
224 void runDockerCommand(`docker rm -f ${containerName}`, 10000);
225 finish({ exitCode: -1, stdout, stderr: "Aborted" });
226 };
227 abortSignal?.addEventListener("abort", abortHandler);
228
229 child.stdout?.on("data", (data: Buffer) => {
230 const text = data.toString();
231 stdout += text;
232 // docker run -d outputs container ID to stdout, not useful to stream
233 });
234
235 child.stderr?.on("data", (data: Buffer) => {
236 const text = data.toString();
237 stderr += text;
238 // Stream pull progress to init logger
239 for (const line of text.split("\n").filter((l) => l.trim())) {
240 initLogger.logStdout(line);
241 }

Callers 1

provisionContainerMethod · 0.85

Calls 8

buildCredentialArgsFunction · 0.85
runDockerCommandFunction · 0.85
onMethod · 0.80
logStdoutMethod · 0.80
finishFunction · 0.70
pushMethod · 0.65
killMethod · 0.65
addEventListenerMethod · 0.45

Tested by

no test coverage detected