docker-compose无法以npm ERR开头! enoent ENOENT:没有此类文件或目录,请打开“ /usr/src/app/package.json”

时间:2019-02-26 11:31:39

标签: docker docker-compose package.json

我从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如何丢失?感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

当您的docker-compose.yml

volumes:
  - ./:/usr/src/app

它隐藏在Dockerfile中完成的所有 并将其替换为当前目录。如果您当前的目录中没有package.json文件(也许您已将所有与Docker相关的文件都移到了子目录中),则会导致您看到错误。

我个人将只删除这两行,在本地开发我的应用程序(无需Docker,使用每个项目的node_modules目录进行隔离),仅在我准备好部署Docker映像时才构建它