我从docker文件构建了一个映像,以运行节点并复制到一个完美运行的简单节点express应用中。映像构建后,我可以运行容器并进行bash并复制本地文件。当我在docker-compose文件中使用此映像以及mongo映像时,容器会构建,但节点容器无法运行并出现错误,原因是package.json丢失。
返回的错误是
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看起来像
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如何丢失?感谢您的帮助!
答案 0 :(得分:1)
当您的docker-compose.yml
说
volumes:
- ./:/usr/src/app
它隐藏在Dockerfile中完成的所有 并将其替换为当前目录。如果您当前的目录中没有package.json
文件(也许您已将所有与Docker相关的文件都移到了子目录中),则会导致您看到错误。
我个人将只删除这两行,在本地开发我的应用程序(无需Docker,使用每个项目的node_modules
目录进行隔离),仅在我准备好部署Docker映像时才构建它