简体   繁体   中英

How to install Python on nodejs Docker image

I have a nodejs application that works on my machine since I have python installed and it's in the global env PATH (also in process.env.PATH) so I can run:

const spawn = require("child_process").spawn;
 console.log('PATH:::::');
 console.log(process.env.PATH);
 const pythonProcess = spawn('python', ["./detect_shapes.py", './example2.png']);
 pythonProcess.stdout.on('data', (data) => {
 console.log('DATA::::');
 console.log(data);
 res.render('index', {data});
});

The script above basically runs a separate python script inside my nodejs application and returns a response to it. I can run the basic commands that can be found on any machine like this: const pythonProcess = spawn('ls'); . This line of code will run the ls command and return the files as it is expected to do.

I also have a Dockerfile like this:

FROM node:9-slim
WORKDIR /app 
COPY . /app 
RUN npm install 
EXPOSE 3000 
CMD ["node", "index.js"]

I created nodejs applications with this exact Dockerfile config and it worked, since I am using child_process.spawn functions it maybe doesn't know about python or it's path so I am getting this error:

Error: spawn python ENOENT
 at Process.ChildProcess._handle.onexit (internal/child_process.js:201:19)
 at onErrorNT (internal/child_process.js:379:16)
 at process._tickCallback (internal/process/next_tick.js:178:19)
Emitted 'error' event at:
 at Process.ChildProcess._handle.onexit (internal/child_process.js:207:12)
 at onErrorNT (internal/child_process.js:379:16)
 at process._tickCallback (internal/process/next_tick.js:178:19)

I tried adding a RUN apt-get install python -y in my Dockerfile for it to install python in my docker image and I can use python, but it doesn't work. Do I have to add another FROM <image> that can install python (I am thinking that node:9-slim doesn't know how to install python since it's not used for that) in the Dockerfile so docker knows how to download python so I can use it.

Also when I print process.env.PATH on my docker container I get this: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin . How can I know that I have python working on my image and/or how can I add it to my paths if that is the problem?

I am new to Docker. I learned it yesterday, so if I didn't make things clear or you need more information please PM me or leave a comment.

In fact, this is not a docker question, just a debian question. You need always do apt-get update before install package. So, for you scenario, it should be:

RUN apt-get update || : && apt-get install python -y

As per your comments:

W: Failed to fetch http://deb.debian.org/debian/dists/jessie-updates/InRelease Unable to find expected entry 'main/binary-amd64/Packages' in Release file (Wrong sources.list entry or malformed file) E: Some index files failed to download. They have been ignored, or old ones used instead. The command '/bin/sh -c apt-get update && apt-get install python -y' returned a non-zero code: 100

So, you can add || : || : after apt-get to ignore the error, as at that time python meta data already finished downloading with other previous url hit, so you can bypass the error.

Update:

A whole workable solution in case you need to compare:

a.py:

print("success")

index.js:

const spawn = require("child_process").spawn;
console.log('PATH:::::');

console.log(process.env.PATH);
const pythonProcess = spawn('python', ['/app/a.py']);
pythonProcess.stdout.on('data', (data) => {
    console.log('DATA::::');
    console.log(data.toString());
});

pythonProcess.stderr.on('data', (data) => {
    console.log("wow");
    console.log(data.toString());
});

Dockerfile:

FROM node:9-slim

RUN apt-get update || : && apt-get install python -y

WORKDIR /app
COPY . /app
CMD ["node", "index.js"]

Try command:

orange@orange:~/gg$ docker build -t abc:1 .
Sending build context to Docker daemon  4.096kB
...
Successfully built 756b13952760
Successfully tagged abc:1

orange@orange:~/gg$ docker run abc:1
PATH:::::
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
DATA::::
success

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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