无法在 docker 中运行 webpack-dev-server [英] Cannot run webpack-dev-server inside docker

查看:52
本文介绍了无法在 docker 中运行 webpack-dev-server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 docker 镜像,它在容器内部使用 webpack 为一个简单的 React 应用程序提供服务,但我在浏览器中什么也没有.

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.

这是我的配置文件

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"]

什么工作正常

当我运行 npm start 时,webpack-dev-server 运行正常,当我去 http://localhost:8080,我看到了我的页面.

What's working fine

When I run npm start, the webpack-dev-server runs normally, and when I go to http://localhost:8080, I see my page.

当我使用 docker 运行我的服务器时,使用以下命令:

When I run my server using docker, with the following command:

docker build -t anubhav756/app .&&docker run -p 80:8080 anubhav756/app

日志显示容器内部一切正常,但是当我将浏览器指向 http://localhost 时,我得到ERR_CONNECTION_RESET

the logs show everything working normally from inside the container, but when I point my browser to http://localhost, I get ERR_CONNECTION_RESET

示例代码在这里

Sample code's over here

推荐答案

您实际上只是在 localhost 上收听.要从外部访问,请替换 package.json 文件中的以下行:

You are actualy listening on localhost only. To be reachable from outside replace the following line in your package.json file:

"start": "webpack-dev-server --inline --content-base ."

作者:

"start": "webpack-dev-server --host 0.0.0.0 --inline --content-base ."

相关讨论:https://github.com/webpack/webpack-dev-server/issues/147

这篇关于无法在 docker 中运行 webpack-dev-server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆