Docker Community Forums

Share and learn in the Docker community.

Changing the version of Python in Dockerfile

Hi all

I am using Ubuntu 16.04; I am trying to run a nodejs program from docker. Within this program I need to be able to spawn a python script (which is only compatible with python 3.6 and above).

In order to test that the correct version of python is being used from within the node program I have a very simple piece of code

 const { spawn } = require('child_process');
 var proc = spawn('python3', ['--version']);

 proc.stdout.on('data', (data) => {
        console.log(`stdout: ${data}`);
 });

However, try as I might, this program always produces the output:

stdout: Python 3.5.3

I have been unable to find a way to get the version of python up to 3.6

My Dockerfile:

FROM ubuntu:16.04
RUN apt-get update && apt-get install -y software-properties-common && add-apt-repository ppa:deadsnakes/ppa && apt-get update && apt-get install -y python3.7 python3.7-dev python3-pip

RUN ln -sfn /usr/bin/python3.7 /usr/bin/python3 && ln -sfn /usr/bin/python3 /usr/bin/python && ln -sfn /usr/bin/pip3 /usr/bin/pip

FROM node:12
ADD demo ./demo
ADD aries_cloudagent ./aries_cloudagent
ADD ./demo/runners/nodejs/ ./nodejs
ADD bin ./bin

RUN npm install --prefix ./nodejs

CMD node ./nodejs/alice.js    

`
I get the same result regardless of whether the first two RUN lines are there: they seem to have no effect.

I am at a loss to know how to fix this. Any ideas?

Hi

The reason for this is that you use multistage builds
And you dont actually benefit from it, these kind of builds is good if you compile/build something in the first stage, and then copy that to the last stage, that way you remove a bit of program/files used for the building part.

So, this wont work (imo) for you.

What i would do, is to take the node 12 image, that uses debian. fx: node:12-buster, and only that, and install python whinin the node image, if i remember correctly, the default python version in buster is 3.7

Hope it makes sense :slight_smile:

Thank you Martin, I tried the same Dockerfile with node:13.12.0-buster and it now uses python 3.7.3 correctly.

You’re right about the multistage build, I think that was just something I put in as one of those “try it and see” moments.

Much appreciated
Mike

1 Like