'No such file or directory in copying a file
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
Solution 1:[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
Solution 2:[2]
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).
Solution 3:[3]
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>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | Saeed Bahmanabadi |
