首页 > 解决方案 > Docker 无法识别 package.json

问题描述

对于 docker 文件系统,我完全迷失了。在挣扎了几天并阅读了文档之后,我仍然不明白这个问题的根本原因。

最小可重现示例:

码头工人-compose.yml:

 version: "3"
 services:
  web:
   build:
    context: ./webapp
    dockerfile: ./Dockerfile
   volumes:
   - './:/webapp'
   - '/webapp/node_modules'
   ports:
   - '3000:3000'
   stdin_open: true

网络应用程序/Dockerfile:

FROM node:fermium

#COPY scripts/ /scripts
COPY ./ /rxc-voice

COPY ./package.json /rxc-voice 

WORKDIR /rxc-voice

RUN npm install -q

EXPOSE 3000

CMD ["npm", "start"]

我当前的目录结构是这样的:

.
├── backend
│   ├── Dockerfile
│   └── api
├── docker-compose.yml
├── README.md
├── webapp
│   ├── Dockerfile
│   ├── etc
│   ├── init.ts
│   ├── node_modules
│   ├── package.json
│   ├── package-lock.json
│   ├── public
│   ├── README.md
│   ├── src
│   └── tsconfig.json
├── scripts # used for building in other directories
    └── deploy

这将拉出此错误:

web_1  | npm ERR! code ENOENT
web_1  | npm ERR! syscall open
web_1  | npm ERR! path /rxc-voice/package.json
web_1  | npm ERR! errno -2
web_1  | npm ERR! enoent ENOENT: no such file or directory, open '/rxc-voice/package.json'
web_1  | npm ERR! enoent This is related to npm not being able to find a file.
web_1  | npm ERR! enoent 

尽管应该将所有内容都复制到图像中,但尝试运行时

理想情况下,docker-compose 使用.上下文并在 Dockerfile 中切换 pwd,但我也无法让它工作。

有趣的是,目录docker build .内部webapp工作正常(在修改 Dockerfile 以使用不同的目录结构做同样的事情之后。

标签: javascriptnode.jsdockernpmserver

解决方案


推荐阅读