简体   繁体   English

Node.js生成的进程在Docker容器中不起作用

[英]Node.js spawn process not working inside Docker container

I have a server.js file in Node that calls python scripts as follows: 我在Node中有一个调用python脚本的server.js文件,如下所示:

// call python scripts 
var spawn = require("child_process").spawn;
var process = spawn('python',["test.py", function_args]);

process.stdout.on('data', function (data){
    res.json({
       "answer" : from_python
    })
}); 

This works perfectly when simply running node as usual: 当像往常一样简单地运行节点时,这非常有效

node server.js 

But when I place everything inside a docker container, the application never enters the process.stdout.on 但是当我将所有内容放在docker容器中时,应用程序永远不会进入process.stdout.on

Everything else works perfectly. 其他一切都很完美。 I can serve static files, call express endpoints, etc. It is just the process that is not getting called. 我可以提供静态文件,调用快速端点等。这只是没有被调用的进程。

I have tried placing child_process inside my package.json file as a dependency. 我已经尝试将child_process作为依赖项放在我的package.json文件中。

Here is my Dockerfile: 这是我的Dockerfile:

FROM node:carbon

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./

RUN npm install

# Bundle app source
COPY . .

EXPOSE 80
CMD ["npm","start"]

This is my package.json file: 这是我的package.json文件:

{
  "name": "my_app_name",
  "version": "1.0.0",
  "description": "my_desc",
  "author": "my_name",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.16.2",
    "path": "^0.12.7",
    "fs": "^0.0.1-security",
    "child_process": "^1.0.2"
  }
}

Note that running server.js is not the problem. 请注意,运行server.js不是问题。 This file runs the other pieces such as serving static files and exposing express endpoints. 此文件运行其他部分,例如提供静态文件和公开快速端点。 The problem seems to just be with the child_process not running. 问题似乎是child_process没有运行。 It is not that Python isn't getting called, it is that node will not even enter the process part of server.js. 并不是Python没有被调用,而是节点甚至不会进入server.js的进程部分。

Also note that the Python prints its output via stdout. 另请注意,Python通过stdout打印其输出。 Not sure of docker has an issue registering standard outputs. 不确定docker是否存在注册标准输出的问题。

I think 我认为

FROM node:carbon

should be 应该

FROM node:9.3

Carbon is the LTS (currently still 8. . ). 碳是LTS(目前还在8)。 You are using libraries from latest v9.3.0. 您正在使用最新的v9.3.0中的库。

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

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