Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/node_modules
docker-example/node_modules
/Python-3.7.3
/build
/.vscode
Expand Down
2 changes: 2 additions & 0 deletions docker-example/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__
node_modules
26 changes: 26 additions & 0 deletions docker-example/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM nikolaik/python-nodejs:python3.8-nodejs18-slim

WORKDIR /usr/app
#prepare packages to install pynode
RUN apt update -qq && apt install make gcc g++ -y

#prepare npm, shared folder is copied
#prepare pynode
COPY package.json .

RUN npm install @fridgerator/pynode
RUN npm install

#prepare python requirements
COPY requirements.txt .
RUN pip3 install -r requirements.txt

#prepare project files
COPY ./src .

CMD ["node", "index.js"]





69 changes: 69 additions & 0 deletions docker-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#Dockerizing

Dockerizing a pynode application is easy.

This example uses one of nikolaik/python-nodejs image.
nikolaik's image repository has all combinations of python and nodejs versions so thats a good start.
You can use a python base image and compile a node
or
use a base nodejs image and compile python

using python3.8 and node 18
```
FROM nikolaik/python-nodejs:python3.8-nodejs18-slim

WORKDIR /usr/app
```

We need to install additional platform packages in order to build pynode app thus it uses node-addons we should able to compile the module
So we are installing required packages as following

```
RUN apt update -qq && apt install make gcc g++ -y
```

It is not required but making sure that pynode is compiling we explicitly run install command for pynode npm package
```
COPY package.json .
RUN npm install @fridgerator/pynode
RUN npm install
```

It is bespractice to work with virtual environment for python applications.
Once your development has finished, pip freeze > requirements.txt will produces dependency file.
Then copy this file and install the requirements in docker image

```
COPY requirements.txt .
RUN pip3 install -r requirements.txt
```

Now image is ready to execute a pynode application.
You can checkout Dockerfile for details and customize according to your needs.

## Tips

There are multiple python versions depending on the platform and python version, the place of the shared python library could vary and can results problems in the runtime. To prevent this, pynode has dlOpen option to meet required libpython to the application.

If you cant sure where is the python lib, you can use find_libpython python package to locate.
It automatically finds the proper library location according to version and platform.

It can be used as follows to introduce libpython to the pynode application. You can checkout index.js for detailed example.

```
const {spawnSync} = require('child_process');
libpython_proc = spawnSync('find_libpython');
if(libpython_proc.status != 0){
throw Error(libpython_proc.error);
}

path = libpython_proc.stdout.toString().trim();

pynode.dlOpen(path);
```






59 changes: 59 additions & 0 deletions docker-example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions docker-example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "docker-example",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"@fridgerator/pynode": "^0.5.2",
"findlibpython": "^1.0.0"
}
}
3 changes: 3 additions & 0 deletions docker-example/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
find-libpython==0.3.0
numpy==1.24.1
scipy==1.9.3
10 changes: 10 additions & 0 deletions docker-example/src/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import scipy
import os
def test():
#print python version
print("python version:",os.sys.version)
#print current working directory
print("cwd:",os.getcwd())
#print environment variables
print(scipy.rand())
return True
27 changes: 27 additions & 0 deletions docker-example/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//call subprocess and wait answer sync
const pynode = require('@fridgerator/pynode');
const {spawnSync} = require('child_process');


libpython_proc = spawnSync('find_libpython');
if(libpython_proc.status != 0){
// console.log(libpython_proc)
throw Error(libpython_proc.error);
}

path = libpython_proc.stdout.toString().trim();

pynode.dlOpen(path);

pynode.startInterpreter();

//tell pynode where python script are
pynode.appendSysPath(__dirname);

pynode.openFile('example');

// call the python function and get a return value
pynode.call('test', (err, result) => {
if (err) return console.log('error : ', err);
console.log('result : ', result);
});