简体   繁体   中英

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 ?

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]"] );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM