[英]Cannot run webpack-dev-server inside docker
I have created a docker image which serves a simple react app using webpack from inside the container, but I get nothing in the browser.我创建了一个 docker 映像,它使用容器内部的 webpack 提供一个简单的反应应用程序,但我在浏览器中什么也没有。
Here are my config files这是我的配置文件
package.json
{
"name": "invas_client",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack --inline --content-base ."
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^0.14.7",
"react-dom": "^0.14.7",
"react-router": "^2.0.0"
},
"devDependencies": {
"babel-core": "^6.5.1",
"babel-loader": "^6.2.2",
"babel-preset-es2015": "^6.5.0",
"babel-preset-react": "^6.5.0",
"http-server": "^0.8.5",
"webpack": "^1.12.13",
"webpack-dev-server": "^1.14.1"
}
}
webpack.config.js
module.exports = {
entry: './index.js',
output: {
filename: 'bundle.js',
publicPath: ''
},
module: {
loaders: [
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader?presets[]=es2015&presets[]=react' }
]
}
}
Dockerfile
# Use starter image
FROM node:argon
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install
# Bundle app source
COPY . /usr/src/app
# Expose port
EXPOSE 8080
# Default command to run
CMD ["npm", "start"]
When I run npm start
, the webpack-dev-server
runs normally, and when I go to http://localhost:8080 , I see my page.当我运行
npm start
, webpack-dev-server
运行正常,当我转到http://localhost:8080时,我看到了我的页面。
When I run my server using docker, with the following command:当我使用 docker 运行我的服务器时,使用以下命令:
docker build -t anubhav756/app . && docker run -p 80:8080 anubhav756/app
the logs show everything working normally from inside the container, but when I point my browser to http://localhost , I get ERR_CONNECTION_RESET
日志显示容器内部一切正常,但是当我将浏览器指向http://localhost时,我得到
ERR_CONNECTION_RESET
You are actualy listening on localhost only.您实际上只在本地主机上收听。 To be reachable from outside replace the following line in your package.json file:
要从外部访问,请替换 package.json 文件中的以下行:
"start": "webpack-dev-server --inline --content-base ."
by :经过 :
"start": "webpack-dev-server --host 0.0.0.0 --inline --content-base ."
Related discussion : https://github.com/webpack/webpack-dev-server/issues/147相关讨论: https ://github.com/webpack/webpack-dev-server/issues/147
I just want to add something to Raphayol answer if you couldn't enable hot-reloading of webpack-dev-server inside container.如果您无法在容器内启用 webpack-dev-server 的热重载,我只想在 Raphayol 答案中添加一些内容。
I couldn't make webpack or webpack-dev-server watch (--watch) mode work even after mounting my project folder into container.即使将我的项目文件夹安装到容器中,我也无法使 webpack 或 webpack-dev-server watch (--watch) 模式工作。
To fix this you need to understand how webpack detects file changes within a directory.要解决这个问题,您需要了解 webpack 如何检测目录中的文件更改。
It uses one of 2 softwares that add OS level support for watching for file changes called inotify and fsevent.它使用添加操作系统级别支持以监视文件更改的 2 个软件之一,称为 inotify 和 fsevent。 Standard Docker images usually don't have these (specially inotify for linux) preinstalled so you have to install it in your Dockerfile.
标准 Docker 镜像通常没有预装这些(特别是 Linux 的 inotify),所以你必须将它安装在你的 Dockerfile 中。
Look for inotify-tools package in your distro's package manager and install it.在发行版的包管理器中查找 inotify-tools 包并安装它。 fortunately all alpine, debian, centos have this.
幸运的是所有 alpine、debian、centos 都有这个。
When using webpack-dev-server with Encore and exposing it through Docker, you we'll need to use --host 0.0.0.0
and --public localhost:8080
so files are served even on browsers not navigating to 0.0.0.0 adresses .当使用带有Encore的 webpack-dev-server 并通过 Docker 公开它时,我们需要使用
--host 0.0.0.0
和--public localhost:8080
以便文件即使在没有导航到 0.0.0.0 地址的浏览器上也能提供服务。
Here is what I used :这是我使用的:
webpack-dev-server --hot --host=0.0.0.0 --public=localhost:8080
to complement what Hugo H said, for me i had to do the following to let encore dev-server working fine on docker in a distant server where dev-server running on a declared adresse:为了补充 Hugo H 所说的,对我来说,我必须执行以下操作才能让 encore dev-server 在远程服务器上的 docker 上正常工作,其中 dev-server 在声明的地址上运行:
1 ) in webpack.config.js: 1 ) 在 webpack.config.js 中:
.configureDevServerOptions(options => {
options.allowedHosts = 'all';
options.host = '0.0.0.0';
options.client = {
webSocketURL: 'https://encore-webserver.domain/ws'
};
})
"dev-server": "encore dev-server --public https://encore-webserver.domain --port 80 ",
where encore-webserver.domain
point with to a node docker container exposing port 80 who simply run npm run dev-server
其中
encore-webserver.domain
指向一个节点 docker 容器,该容器暴露端口 80,只需运行npm run dev-server
this pages helped me to solve my problem:这个页面帮助我解决了我的问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.