我正在尝试在docker上运行React Mongo Express应用程序。 docker-compose build
似乎构建了一切正常。运行docker-compose up
后,我得到以下日志:
frontend_1 | > react-frontend@0.1.0 start /usr/src/app
frontend_1 | > webpack -d --watch
frontend_1 |
frontend_1 |
frontend_1 | Webpack is watching the files…
frontend_1 |
frontend_1 | Hash: 23626d3f56ec19db7872
frontend_1 | Version: webpack 3.10.0
frontend_1 | Time: 14872ms
frontend_1 | Asset Size Chunks Chunk Names
frontend_1 | bundle.js 2.55 MB 0 [emitted] [big] main
frontend_1 | [105] ./src/index.jsx 1.63 kB {0} [built]
frontend_1 | + 255 hidden modules
我的文件结构如下所示: 根
// root/docker-compose.yml
version: '2'
services:
mongodb:
image: "mongo"
ports:
- "27017:27017"
backend:
build: ./node-backend/
ports:
- "6200:6200"
volumes:
- ./node-backend:/usr/src/app
depends_on:
- mongodb
frontend:
build: ./react-frontend/
tty: true
ports:
- "3000:3000"
volumes:
- ./react-frontend:/usr/src/app
depends_on:
- backend
节点后端
// root/node-backend/Dockerfile
FROM node:6.11.1
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
RUN npm install -g nodemon
EXPOSE 3000
CMD [ "npm", "start" ]
// root/node-backend/package.json
{
"name": "node-backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon app.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.17.2",
"cors": "^2.8.4",
"express": "^4.15.3",
"mongoose": "^4.11.3"
}
}
React Frontend
// root/react-frontend/Dockerfile
FROM node:6.11.1
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
EXPOSE 3000
CMD [ "npm", "start" ]
// root / react-frontend / package.json
{
"name": "react-frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.16.2",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-es2015": "^6.22.0",
"babel-preset-react": "^6.24.1",
"express": "^4.15.3",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-router-dom": "^4.1.1",
"webpack": "^3.10.0"
},
"scripts": {
"start": "webpack -d --watch",
"build": "webpack --config webpack.config.js"
}
}
// root/react/front-endwebpack.config
var path = require('path');
var SRC_DIR = path.join(__dirname, '/src');
var DIST_DIR = path.join(__dirname, '/public');
module.exports = {
entry: `${SRC_DIR}/index.jsx`,
output: {
filename: 'bundle.js',
path: DIST_DIR
},
module : {
loaders : [
{
test : /\.jsx?/,
include : SRC_DIR,
loader : 'babel-loader',
query: {
presets: ['react', 'es2015']
}
}
]
}
};
当我访问localhost时:6200我看到后端正在运行。 但是当我访问localhost:3000时,屏幕显示“这个网站无法到达”。 控制台中没有日志。当我查看网页的来源时,我看到了一个旧版本的html文件,该文件已经更新。我尝试删除bundle.js并在前端目录中使用npm i重新安装模块。
我应该如何更改docker和webpack配置?