简体   繁体   English

如何在docker容器中运行的nodejs应用程序内使用child_process

[英]How to use child_process inside nodejs application running inside docker container

I try to run one nodejs application inside of docker container. 我尝试在docker容器中运行一个nodejs应用程序。 Everything is as expect once I get into docker container and execute the application. 进入docker容器并执行应用程序后,一切都按预期进行。 However, once I use docker exec to send the command to container. 但是,一旦我使用docker exec将命令发送到容器。 The error is occured as TypeError: Object # has no method 'execSync' . 发生错误的原因是TypeError:对象#没有方法'execSync' How can I use child_prcess in my node application from docker container? 如何在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);

execSync() was introduced in the v0.11.12 release . execSync()v0.11.12版本中引入。

Check your node version. 检查您的节点版本。

docker exec containername bash -c "node -v"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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