0

How can I write to the container's stdin, when using dockerode library? I tried doing it in a multiple ways, but nothing seems to work.

My current code that is not able to write to stdin:

export async function nameToStdName(
  pluginName: string,
  pluginDescription: string,
  pluginId: number,
  numberOfDuplicates: number
) {
  const docker = new Docker();
  const input = `${pluginName}; ${pluginDescription}`;
  // Run the docker container and pass input from a string
  const dockerImageName = 'name-to-stdname';
  const dockerCmd = ['python', '/app/main.py', '-i', pluginId.toString(), '-v', numberOfDuplicates.toString()];
  const options = {
    cmd: dockerCmd,
    AttachStdin: true,
    AttachStdout: true,
    Tty: false,
  };

  const container = await docker.createContainer({
    Image: dockerImageName,
    ...options,
  });

  await container.start();

  const stream = await container.attach({
    stream: true,
    stdin: true,
    stdout: true,
  });

  // Handle output from container's stdout
  let name = "";
  stream.on('data', (data: Stream) => {
        console.log(`Received output: ${data.toString()}`);
    name += data.toString();
  });

  // Pass input to container's stdin
  stream.write(input);

  await container.wait();

  return name;
}

0 Answers0