Home > Blockchain >  No such file or directory in copying a file
No such file or directory in copying a file

Time:02-21

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

CodePudding user response:

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

CodePudding user response:

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).

CodePudding user response:

you must add this configuration to your docker file

ENV NODE_ENV=production

WORKDIR /app

COPY ["package.json", "package-lock.json*", "./"]

you do not specify where is package.json is in your project directory . after you can test this solution: add to docker your package.json you can add COPY --from=builder package-*.json . or like this: Modern Docker versions allow to use multi-stage builds. Essentially it allows to have many FROM clauses in Dockerfile, but only the last one FROM will be used as a base for our image. It means that all the layers of other stages will be discarded, so the resulting image is going to be small.

FROM rubygem/compass AS builder
COPY ./src/public /dist
WORKDIR /dist
RUN compass compile
# Output: css/app.css

Docker build engine will save resulting files in a temporary image that can be used in COPY expression for our final image:

# Copy compiled CSS styles from builder image.
COPY --from=builder /dist/css ./dist/css

after you can up and run your docker compose by this command :

docker-compose up --build-arg NODE_ENV=<value>

CodePudding user response:

yeah this is simple dockerfile that is true in my test:

# Separate builder stage to compile SASS, so we can copy just the resulting CSS files.
FROM rubygem/compass AS builder
COPY ./src/public /dist
WORKDIR /dist
RUN compass compile
# Output: css/app.css

# Use NodeJS server for the app.
FROM node:12

# Copy files as a non-root user. The `node` user is built in the Node image.
WORKDIR /usr/src/app
RUN chown node:node ./
USER node

# Defaults to production, docker-compose overrides this to development on build and run.
ARG NODE_ENV=production
ENV NODE_ENV $NODE_ENV

# Install dependencies first, as they change less often than code.
COPY package.json package-lock.json* ./
RUN npm ci && npm cache clean --force
COPY ./src ./src

# Copy compiled CSS styles from builder image.
COPY --from=builder /dist/css ./dist/css

# Execute NodeJS (not NPM script) to handle SIGTERM and SIGINT signals.
CMD ["node", "./src/index.js"]

CodePudding user response:

please test this docker file:

FROM node:14.4.0 AS buildCOPY . .
RUN npm install && npm run buildFROM node:slim-14.4.0USER node
EXPOSE 8080COPY --from=build /home/node/app/dist /home/node/app/package.json /home/node/app/package-lock.json ./
RUN npm install --productionCMD [ "node", "dist/app.js" ]
  • Related