【发布时间】:2019-08-08 10:56:20
【问题描述】:
我有这个dockerservice:
client/
.dockerignore
Dockerfile-dev
package.json
node_modules/
react-router-dom/
spotify-web-api-js/
and-many-more/
src/
App.jsx
Spotify.js
其中安装了以下node 模块:
package.json
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.18.0",
"react": "^16.8.2",
"react-dom": "^16.8.2",
"react-router-dom": "^4.3.1",
"react-scripts": "2.1.5",
"spotify-web-api-js": "^0.22.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"devDependencies": {
"enzyme": "^3.8.0",
"enzyme-adapter-react-16": "^1.7.1"
}
}
像这样:
Dockerfile-开发
# base image
FROM node:11.6.0-alpine
# set working directory
WORKDIR /usr/src/app
# add `/usr/src/app/node_modules/.bin` to $PATH
ENV PATH /usr/src/app/node_modules/.bin:$PATH
# install and cache app dependencies
COPY package.json /usr/src/app/package.json
RUN npm install --silent
RUN npm install react-scripts@2.1.2 -g --silent
# start app
CMD ["npm", "start"]
在同一条路径上,src,但是,我观察到两种不同的行为:
1) 这行得通:
App.jsx
import React, { Component } from 'react';
import { Route, Switch } from 'react-router-dom';
2) 但这不起作用:
Spotify.js
import React, { Component } from 'react';
import SpotifyWebApi from 'spotify-web-api-js';
抛出以下错误:
Failed to compile
./src/Spotify.js
Module not found: Can't resolve 'spotify-web-api-js' in '/usr/src/app/src'
docker-compose-dev.yml
client:
build:
context: ./services/client
dockerfile: Dockerfile-dev
volumes:
- './services/client:/usr/src/app'
- '/usr/src/app/node_modules'
ports:
- 3007:3000
environment:
- NODE_ENV=development
- REACT_APP_WEB_SERVICE_URL=${REACT_APP_WEB_SERVICE_URL}
depends_on:
- web
.dockerignore
node_modules
coverage
build
env
htmlcov
.dockerignore
Dockerfile-dev
Dockerfile-prod
我错过了什么?
【问题讨论】:
-
那是你完整的 Dockerfile 吗?看起来你还没有复制你的 src 文件。
-
是的。我还应该做什么?
-
我的意思是你还没有将你的 src/* 复制到 docker。
COPY src/* .在npm install声明之后,但我不确定这是否是导致问题的原因。 -
让我们试试吧..
-
ERROR: Service 'client' failed to build: When using COPY with more than one source file, the destination must be a directory and end with a /
标签: node.js reactjs docker npm