【发布时间】:2022-01-07 14:38:57
【问题描述】:
我有文件
docker-compose.yml
...
example:
build:
context: ./example
ports:
- 3300:3000
networks:
- backend
volumes:
- ../example/:/var/www
container_name: example
...
./example/Dockerfile
FROM node:16
WORKDIR /var/www
RUN npm install -g react-scripts
RUN npm install
EXPOSE 3000
CMD [ "npm", "start" ]
当我跑步时
docker-compose build
得到这个错误
...
npm ERR! path /var/www/package.json
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/var/www/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
但如果我评论#RUN npm install,脚本会看到 package.json 和“npm start”工作,但我得到错误,因为没有 node_modules
“npm install”时挂载文件夹的内容在哪里?
【问题讨论】:
-
在映像构建之后才会进行卷装载。如果你想处理本地代码,我建议使用主机节点作为比 Docker 更合适、更简单的工具。你需要将
COPYpackage.json等源代码放入你的镜像之前RUN npm ci;然后你应该删除volumes:挂载以避免在运行容器时覆盖该代码。