1

I am trying to run an async node child_process which runs a docker container.

let runDockerCmd = `docker-compose -f ./vm/docker-compose.yml run --rm vm node temp.js`;
child_process.exec(runDockerCmd, (error, stdout, stderr) => {
  if (error) {
    console.error(`exec error: ${error}`);
    return;
  }
  console.log(`stdout: ${stdout}`);
  console.log(`stderr: ${stderr}`);
});

Where temp.js contains a for loop. Sometimes it runs half the loop, sometimes a third etc. then it terminates.

I can get it to work using child_process.execSync but that is a bit unsavoury. Why does it exit like that, and how do I keep it alive?

Dylan Harness
  • 719
  • 1
  • 9
  • 20
  • If you launch the docker-compose command directly (outside any node process), does it stay alive, or does it exit immediately? – VonC Jun 08 '16 at 07:10
  • @VonC It should exit immediately as he's just running a one-off docker-compose command by telling to run vm service. I'm curious if removing `--rm` should help in this case cause docker would still keep the container even after it's done rather than removing as soon as it's done with `--rm`. – shriek Jun 08 '16 at 13:20
  • Also, @Dylan Harness, if you want to go for truly async I think you should use `spawn` as `exec` is synchronously async but that's off topic for this post. – shriek Jun 08 '16 at 13:22
  • I suspect you are seeing part of the logs of temp.js, But temp.js keep running (to completion) even after child_process.exec callback is called. Can you try something like that to validate: let runDockerCmd = `docker-compose -f ./vm/docker-compose.yml run --rm vm node temp.js > ./output.txt`; Then check output.txt for logs. – Yaniv Peretz Feb 11 '22 at 22:45

0 Answers0