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!
As you can see, there’s an error with code ENOENT that prevents npm start command from running successfully. The code ENOENT means that npm fails to open a file or directory that’s required for executing the command. The npm start command is used to run the start script in the package.json file.
This is normal, since the Dockerfile for the node:8 image only contains CMD [ "node" ] and no working directory. If you want to run straight from the image you need to at least add a working_dir: /usr/src/app and an entrypoint to the docker-compose.yml.
The note underneath implies that simply performing a docker-compose up will start a container in which the dependencies will be installed via an npm install and that the application will be started by a npm start command.
If you want to run straight from the image you need to at least add a working_dir: /usr/src/app and an entrypoint to the docker-compose.yml. In the entrypoint you'll need to execute a bash script in which you first perform an npm install followed by an npm start.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With