docker-compose无法以npm ERR开头! enoent ENOENT:没有这样的文件或目录,打开'/usr/src/app/package.json' [英] docker-compose fails to start with npm ERR! enoent ENOENT: no such file or directory, open '/usr/src/app/package.json'
问题描述
我从docker文件构建了一个映像,以运行节点并复制到一个完美运行的简单节点express应用中。映像构建后,我可以运行容器并进行bash并复制本地文件。当我在docker-compose文件中使用此映像以及mongo映像时,容器会构建,但节点容器无法运行并出现错误,原因是package.json丢失。
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.
返回的错误是
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
dockerfile
FROM node
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 4000
CMD [ "npm", "run", "dev" ]
Docker-compose.yml看起来像
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
package.json丢失了吗?谢谢您的帮助!
How has the package.json gone missing? Thanks for your help!
推荐答案
当您的 docker-compose.yml
说
volumes:
- ./:/usr/src/app
它隐藏在Dockerfile中完成的所有 并将其替换为当前目录。如果您当前的目录中没有 package.json
文件(也许您已将与Docker相关的所有内容都移到了子目录中),则会引起错误。
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.
我个人将只删除这两行,在本地开发我的应用程序(没有Docker,使用每个项目的 node_modules
目录进行隔离),并且仅在我准备好部署它时才构建Docker映像。
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.
这篇关于docker-compose无法以npm ERR开头! enoent ENOENT:没有这样的文件或目录,打开'/usr/src/app/package.json'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!