Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run complex command in node js spawn?

I am developing a lib for docker command line in nodejs, I am still in starting face, I just tried basic docker run command using spawn in node js - everything works fine but it's not working for complex cases like the one below.

I want to run docker run --rm -it julia:0.3.6 julia -E "[x^2 for x in 1:100]" in nodejs, but I am gettting below error -

the input device is not a TTY

Docker Shell existed with status = 1

Below Code -

    const
        spawn = require('child_process').spawn,
        dockerDeamon = spawn("docker", ["run","--rm", "-it", "julia:0.3.6", "-E",   "\" [x^2 for x in 1:100]\""] );

    dockerDeamon.stdout.on('data', data => {
        console.log(`${data}`);

    });

    dockerDeamon.stderr.on('data', data => {
        console.log(`${data}`);

    });

    dockerDeamon.on('close', code => {
        console.log(`Docker Shell existed with status = ${code}`);

    });

Is there any better way to execute the above script ?

like image 707
ajayramesh Avatar asked Aug 08 '17 04:08

ajayramesh


1 Answers

You're passing the -t (--tty) flag to Docker, which tells it that it should expect the input and output to be attached to a terminal (TTY). However, when you're using spawn, you're instead attaching it to a Node.js stream in your program. Docker notices this and therefore gives the error Input device is not a TTY. Therefore, you shouldn't be using the -t flag in this case.


Also, note that you don't need nested quotes in your last argument, "\" [x^2 for x in 1:100]\"". The purpose of the quotes is to preserve the spaces and other special characters in the argument when running in a shell, but when you use spawn you're not using a shell.

So your statement should be something like:

dockerDeamon = spawn("docker", ["run","--rm", "-i", "julia:0.3.6", "julia", "-E", "[x^2 for x in 1:100]"] );
like image 101
Frxstrem Avatar answered Nov 01 '22 07:11

Frxstrem