在docker中,运行javascript和python

时间:2018-03-21 12:57:35

标签: python node.js docker

在构建nodejs服务器时,我注意到有些事情“在python中更容易”。所以我刚为这些进程创建了一个python脚本。

这些过程由nodejs通过以下方式产生:

const { spawn } = require('child_process');
const args = [
  'BackgroundServer/main.py',
  _worker.poller_db.id,
  '--log-file=~/var/www/log/background.log'
];
if (maxRuns !== undefined) {
  args.push(`--max-num=${maxRuns}`);
}
const pythonProcess = spawn('BackgroundServer/venv/bin/python', args);

这在我的开发系统中完美无缺; python进程启动,运行和工作整齐。

移动到码头工作但是会出现一个严重的问题,很明显,venv被添加到.dockerignore(就像.gitignore一样)。

解决方案是在现场“构建”虚拟环境。我试过这个,Dockerfile

FROM node:carbon
WORKDIR ~/dockerapp
COPY package*.json ./
RUN npm install && \
virtualenv -p python3 BackgroundServer/venv && \
source BackgroundServer/venv/bin/activate && \
pip install -r BackgroundServer/requirements.txt
COPY . .
EXPOSE 1337
CMD [ "npm", "start" ]

然而,这在“virtualenv”找不到命令时失败了。 - 我如何将“virtualenv”提供给“临时”跑步者?

另一个解决方案是,在构建docker之前构建虚拟环境,然后 not 忽略虚拟环境设法“运行”。 然而,它失败了,声称它找不到几个核心python包无法启动python:

Could not find platform independent libraries <prefix>

Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]

Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'

那么..如何结合两者,如何制作一个“看到”python home install和python路径的nodejs容器。

1 个答案:

答案 0 :(得分:0)

Dockerfile中,您要从特定的基本图像中提取:

FROM node:carbon

系统仅包含已在node:carbon图像中的组件。如果您想添加一些额外的资源(例如virtualenv),您可以使用您在常规系统上使用的标准命令在Dockerfile中安装这些资源。

由于您使用特定的节点映像来构建映像,因此可能缺少一些基本依赖项 - 这些依赖项也必须安装在docker容器中。