Search code examples
dockerdocker-multi-stage-build

Multistage build for python and nodejs in the same container


I need to have access to both npm and pipenv in the same container. I'm thinking the best way to accomplish this is with a multistage build.

If I do something like this:

FROM python:3.7
COPY Pipfile /app/Pipfile
RUN pip install pipenv

FROM node:8
npm install

How do I make sure that the pipenv binary is not discarded? What files do I need to copy from the previous stage so that pipenv is available in the final image?


Solution

  • multi-stage build is not required in your case. Start from base image python:3.7 and install node in it , will be the straightforward solution

    FROM python:3.7
    COPY Pipfile /app/Pipfile
    RUN pip install pipenv
    
    # Using Debian, as root
    RUN curl -sL https://deb.nodesource.com/setup_11.x | bash -
    RUN apt-get install -y nodejs
    

    how do you know the image python:3.7 is debian?

    $ docker run -ti --rm python:3.7 bash
    root@eb654212ef67:/# cat /etc/*release
    PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
    NAME="Debian GNU/Linux"
    VERSION_ID="9"
    VERSION="9 (stretch)"
    ID=debian
    HOME_URL="https://www.debian.org/"
    SUPPORT_URL="https://www.debian.org/support"
    BUG_REPORT_URL="https://bugs.debian.org/"
    root@eb654212ef67:/#
    

    Reference:

    https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions-enterprise-linux-fedora-and-snap-packages

    https://github.com/nodesource/distributions/blob/master/README.md

    Node Installation instructions

    Node.js v11.x:
    
    # Using Ubuntu
    curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
    # Using Debian, as root
    curl -sL https://deb.nodesource.com/setup_11.x | bash -
    apt-get install -y nodejs
    Node.js v10.x:
    
    # Using Ubuntu
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
    # Using Debian, as root
    curl -sL https://deb.nodesource.com/setup_10.x | bash -
    apt-get install -y nodejs
    Node.js v8.x:
    
    # Using Ubuntu
    curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
    # Using Debian, as root
    curl -sL https://deb.nodesource.com/setup_8.x | bash -
    apt-get install -y nodejs