Replies: 2 comments 1 reply
-
Hi, we have a few examples of this in dockerode tests :) |
Beta Was this translation helpful? Give feedback.
0 replies
-
Replicating import stream from "stream";
import streamPromises from "stream/promises";
import Docker from "dockerode";
const docker = new Docker();
/**
* Interface for the output of a Docker exec command.
*/
interface DockerExecOutput {
/**
* Exit code of the process.
*/
exitCode: number | null;
/**
* Stdout of the process as a string.
*/
stderr: string;
/**
* Stderr of the process as a string.
*/
stdout: string;
}
/**
* Execute a command in a running Docker container.
*
* @param container container to execute the command in
* @param cmd command to execute
* @param opts options passed to the Docker Engine API
*/
export async function exec(
container: Docker.Container,
cmd: string[],
opts?: Docker.ExecCreateOptions,
): Promise<DockerExecOutput> {
const dockerExec = await container.exec({
...opts,
AttachStderr: true,
AttachStdout: true,
Cmd: cmd,
});
const dockerExecStream = await dockerExec.start({});
const stdoutStream = new stream.PassThrough();
const stderrStream = new stream.PassThrough();
docker.modem.demuxStream(dockerExecStream, stdoutStream, stderrStream);
dockerExecStream.resume();
await streamPromises.finished(dockerExecStream);
const stderr = stderrStream.read() as Buffer | undefined;
const stdout = stdoutStream.read() as Buffer | undefined;
const dockerExecInfo = await dockerExec.inspect();
return {
exitCode: dockerExecInfo.ExitCode,
stderr: stderr?.toString(),
stdout: stdout?.toString(),
};
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've used your example on running container exec.
https://github.com/apocas/dockerode/blob/v3.3.5/examples/exec_running_container.js
in the log, i can see the result of the execution, but i want to be able to use the result. But right now, it looks like it's just a log.
my code:
result is:
the last json containing ID, Publickey, etc. is what i need, but that is not part of the returned value from runExec function.
is there a way to receive this result as a return value of a runExec function?
Beta Was this translation helpful? Give feedback.
All reactions