Reputation: 469
I built an image from a docker file to run node and copy in a simple node express app which works perfectly. The image builds I can run a container and bash in and the local files are copied. When I use this image in a docker-compose file along with a mongo image the containers build but the node container fails to run with an error is the package.json is missing.
The error returned is
report-node exited with code 254
report-node | npm ERR! path /usr/src/app/package.json
report-node | npm ERR! code ENOENT
report-node | npm ERR! errno -2
report-node | npm ERR! syscall open
report-node | npm ERR! enoent ENOENT: no such file or directory, open '/usr/src/app/package.json'
report-node | npm ERR! enoent This is related to npm not being able to find a file.
report-node | npm ERR! enoent
report-node |
report-node | npm ERR! A complete log of this run can be found in:
report-node | npm ERR! /root/.npm/_logs/2019-02-26T10_55_05_562Z-debug.log
report-node exited with code 254
dockerfile
FROM node
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 4000
CMD [ "npm", "run", "dev" ]
Docker-compose.yml looks like
version: '3'
services:
app:
image: gosmith/node10:latest
container_name: report-node
restart: always
volumes:
- ./:/usr/src/app
working_dir: /usr/src/app
depends_on:
- mongo
environment:
NODE_ENV: development
ports:
- 4000:4000
- 8080:8080
command: npm run dev && npm run serve
mongo:
container_name: mongo
image: mongo
expose:
- 27017
ports:
- 27037:27017
volumes:
- ./data:/data/db
How has the package.json gone missing? Thanks for your help!
Upvotes: 12
Views: 18521
Reputation: 702
I also face same problem on Ubuntu 20.04 and docker-desktop 20.10.16 I changes following:
Before Solving Problem:
Dockerfile contain
WORKDIR /app
And docker-compose.yaml file contains:
services:
backend:
volumes:
- ./:/app
After Solving Problem my files look like
Dockerfile
WORKDIR /app/project
docker-compose.yaml
services:
backend:
volumes:
- ./:/app/project
Upvotes: 2
Reputation: 21
I had the same problem.
In your case do it like this:
In volumes changes this - ./:/usr/src/app
for this
volumes:
- ./:/app
the /app
name is this:
services:
app:
Upvotes: 0
Reputation: 158758
When your docker-compose.yml
says
volumes:
- ./:/usr/src/app
it hides everything that gets done in the Dockerfile and replaces it with the current directory. If your current directory doesn't have the package.json
file (maybe you moved everything Docker-related into a subdirectory) it will cause the error you see.
Personally I would just remove these two lines, develop my application locally (without Docker, using the per-project node_modules
directory for isolation), and only build the Docker image when I'm actually ready to deploy it.
Upvotes: 15