0

I have a problem. I want to install Python and Node.js in a same image. But there is a problem in copying the package.json. The error says no such file or directory, open '/opt/app/src/package.json'. So what is the probleme here? What did I might wrong?

I looked at Docker-compose up : no such file or directory, open '/usr/src/app/package.json' , but I don't know where my error is.

Dockerfile

FROM python:3.7-slim AS build
RUN mkdir -p /opt/app/src
COPY ./requirements.txt /opt/app/src
RUN pip install -r /opt/app/src/requirements.txt

FROM node:14-slim
RUN mkdir -p /opt/app/src
WORKDIR /opt/app/src
COPY --from=build package-*.json ./
RUN npm install
EXPOSE 4001
CMD npm start

Structure

|-- app.js
|-- requriments.txt
|-- test.js
|-- package.json
|-- routes
|-- |-- model.py
|-- |-- post_price.js

docker-compose.yml

version: '3.8'

services:
    backend:
        container_name: backend_airbnb
        image: backend_airbnb
        expose:
            - "4001"
        ports:
            - "4001:4001"
        networks:
            - backendProxyNetwork

networks:
    backendProxyNetwork:
      external: true

Error


CONTAINERS





Attaching to backend_airbnb

backend_airbnb | npm ERR! code ENOENT

backend_airbnb | npm ERR! syscall open

backend_airbnb | npm ERR! path /opt/app/src/package.json

backend_airbnb | npm ERR! errno -2

backend_airbnb | npm ERR! enoent ENOENT: no such file or directory, open '/opt/app/src/package.json'

backend_airbnb | npm ERR! enoent This is related to npm not being able to find a file.

backend_airbnb | npm ERR! enoent 

backend_airbnb | 

backend_airbnb | npm ERR! A complete log of this run can be found in:

backend_airbnb | npm ERR!     /root/.npm/_logs/2022-02-21T08_33_58_188Z-debug.log

backend_airbnb exited with code 254
Test
  • 571
  • 13
  • 32
  • You are not installing node and python in the same image (your example), you are building a python app in python image, then you tried to copy files to your node image, which is wrong. – Someone Special Feb 21 '22 at 09:12
  • You need to probably use a `alpine` image, then install python and node manually. – Someone Special Feb 21 '22 at 09:13
  • @SomeoneSpecial thank you for the hint! How could I generate Python and Node.js in the same image? Thank you very much. Should I use `python:3.7-alpine` ? – Test Feb 21 '22 at 09:13
  • You need to combine all these into one Dockerfile https://stackoverflow.com/questions/62554991/how-do-i-install-python-on-alpine-linux https://superuser.com/questions/1125969/how-to-install-npm-in-alpine-linux https://hub.docker.com/_/alpine – Someone Special Feb 21 '22 at 09:15
  • I'm not sure what your python stuff do, but if it's an standalone application it probably shouldn't be in the same container – Someone Special Feb 21 '22 at 09:16
  • @SomeoneSpecial The Node.js calls a Python file and this in turn only outputs a value. – Test Feb 21 '22 at 09:17
  • 1
    i add an answer to install python in to your nodejs image, hopefully it works. – Someone Special Feb 21 '22 at 09:21
  • Why not use docker image that has node and python setup both and on top of that use your instructions to create and install app? https://hub.docker.com/r/nikolaik/python-nodejs – Sohan Feb 21 '22 at 09:32

2 Answers2

1

You can probably install python in your nodejs image. This is a very basic demostration of what you can do.

FROM node:14-alpine
WORKDIR /app

# Install Python
RUN apk add --no-cache python3 py3-pip

# Copy all required files    
COPY . .

# Install your python packages
RUN pip install -r /app/requirements.txt

# Install node applications
RUN npm install

# Expose Port
EXPOSE 4001

# Start App
CMD npm start
Someone Special
  • 12,479
  • 7
  • 45
  • 76
0

It's probably because of the COPY. Try this: COPY --from=build package*.json ./.

Difference package*.json vs package-*.json

This will match "package.json" and "package-lock.json" - previously only the latter did and the package.json didn't copy (which is required to install deps).

sandrooco
  • 8,016
  • 9
  • 48
  • 86