DEV Community

hagevvashi
hagevvashi

Posted on

Don't forget to give "--host 0.0.0.0" to the startup option of webpack-dev-server using docker

You must give "--host 0.0.0.0" option to webpack-dev-server in the container

tl;dr

It is necessary to give "--host 0.0.0.0" to the startup option of webpack-dev-server in the docker container in which server you want to access via http protocol by your browser.

intro

I will develop react application with docker because I want to keep the developing environment clean. My local developing environment is highly customized with globally installed npm packages to enhance the development experience. Coding is comfortable though, I'm afraid of the development being influenced by such packages so I feel the necessity of sand-box environment like docker container to execute and test application in genuine environment.

the overview I initially developing

directory structure

react-app
  -- src
    -- components
    -- index.html
    -- index.js
  -- Dockerfile
  -- docker-compose.yml
  -- package.json
  -- webpack.config.js
  -- yarn.lock
Enter fullscreen mode Exit fullscreen mode

Dockerfile

FROM node
WORKDIR /app
COPY package.json /app/package.json
COPY yarn.lock /app/yarn.lock
RUN yarn
Enter fullscreen mode Exit fullscreen mode

docker-compose.yml

version: '3'
services:
  react:
    build: .
    ports:
      - 8642:8080
    command: yarn start
    volumes:
      - .:/app
    tty: true

Enter fullscreen mode Exit fullscreen mode

package.json

{
  ...
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack-dev-server --mode development --open --hot",
    "build": "webpack --config webpack.config.js --mode production",
    "serve": "http-server ./dist"
  }
}
Enter fullscreen mode Exit fullscreen mode

webpack.config.js

const { resolve } = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  devtool: 'source-map',
  entry: './src/index.js',
  output: {
    path: resolve(__dirname, '/dist'),
    filename: 'index_bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: `${resolve(__dirname, '/src/index.html')}`
    })
  ]
};

Enter fullscreen mode Exit fullscreen mode

a problem occurred

ok, I'm Ready, so check the server -- input "localhost:8642" to
the browser. I see the awful error message "Can not access this page".

the reason why I cannot see the page

In the first setting, the server was not accessible from outside.

webpack-dev-server --mode development --open --hot
Enter fullscreen mode Exit fullscreen mode

solution

From the official manual, If you want your server to be accessible externally, specify it like this:

webpack-dev-server --host 0.0.0.0
Enter fullscreen mode Exit fullscreen mode

https://webpack.js.org/configuration/dev-server/#devserverhost

final setting

package.json

 {
   ...
   "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1",
-    "start": "webpack-dev-server --mode development --open --hot",
+    "start": "webpack-dev-server --mode development --open --hot --host 0.0.0.0"
     "build": "webpack --config webpack.config.js --mode production",
     "serve": "http-server ./dist"
   }
 }
Enter fullscreen mode Exit fullscreen mode

By just adding "--host 0.0.0.0", I can see the page served by webpack-dev-server in the docker container.

conclusion

In conclusion, you must give "--host 0.0.0.0" option to webpack-dev-server when you create a server on Docker container.

Top comments (6)

Collapse
 
bisadev profile image
Bisa

Lifesaver !!!

Collapse
 
defman profile image
Sergey Kislyakov

A more generic conclusion would be: if you want your app to be accessible outside from docker container network, you'd use 0.0.0.0.

Collapse
 
elvispt profile image
Elvis Pestana

Such a small detail that I lost a couple of errors on it. Thanks.

Collapse
 
rppudo profile image
rppudo

Life saver <3

Collapse
 
a_ssuresh profile image
Suresh S

Thankyou ! This saved my time.

Now, what should i do to access the page from my local browser. It says 'site can’t be reached' in chrome.

But curl on the host machine working fine.

Collapse
 
godcrampy profile image
Sahil Bondre

Someone give this person a gold medal! You my friend save my life 🤗