child_process不能执行docker运行

使用像ls,pwd这样的简单命令,甚至打开一个外部应用程序我已经成功地使用了subprocess,但是在内置的电子应用程序中使用exec执行docker命令时,出现以下错误:

exec Error: Command failed: docker exec -it 6bec55e9e86e touch home.html the input device is not a TTY 

这里是代码:

 var exec = require('child_process').exec; exec('docker exec -it 6bec55e9e86e touch casa.html', function (error, stdout, stderr) { console.log('stdout: ' + stdout); console.log('stderr: ' + stderr); if (error !== null) { console.log('exec error: ' + error); } }); 

请删除-t标志。 所以你的命令应该是docker exec -i 6bec55e9e86e touch casa.html

这个错误the input device is not a TTY意味着你的input设备不是一个Teletypes(terminal),并在docker的命令, -t标志符号terminal ,所以他们是冲突的。 所以只要删除它。

使用spawn和设置options.stdio inherit将工作:

 const spawn = require('child_process').spawn; spawn('docker', ['exec', '-it', '6bec55e9e86e', 'touch', 'casa.html'], { stdio: 'inherit' })