我尝试在docker容器中运行一个nodejs应用程序。一旦我进入docker容器并执行应用程序,一切都如预期的那样。但是,一旦我使用docker exec将命令发送到容器。该错误发生在 TypeError:Object#没有方法'execSync'。如何在docker容器的节点应用程序中使用child_prcess?
root@17dbb03697b0:/apps# cat test.js
var aaa = require('child_process').execSync('echo "aaaaaaaaa"').toString().trim();
console.log(aaa);
root@17dbb03697b0:/apps# node test.js
aaaaaaaaa
root@17dbb03697b0:/apps#
user@ip-172-1-1-121:~/$ docker exec containername bash -c "cd /apps;node test.js"
/apps/test.js:1
module, __filename, __dirname) { var aaa = require('child_process').execSync('
^
TypeError: Object #<Object> has no method 'execSync'
at Object.<anonymous> (/apps/test.js:1:98)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
user@ip-172-1-1-121:~/$ docker exec containername bash -c "cd /apps/;cat test.js"
var aaa = require('child_process').execSync('echo "aaaaaaaaa"').toString().trim();
console.log(aaa);
答案 0 :(得分:0)