<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: hagevvashi</title>
    <description>The latest articles on Forem by hagevvashi (@hagevvashi).</description>
    <link>https://forem.com/hagevvashi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F174765%2F08672806-b761-48df-bc7e-2c03ad5aac09.png</url>
      <title>Forem: hagevvashi</title>
      <link>https://forem.com/hagevvashi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/hagevvashi"/>
    <language>en</language>
    <item>
      <title>Don't forget to give "--host 0.0.0.0" to the startup option of webpack-dev-server using docker</title>
      <dc:creator>hagevvashi</dc:creator>
      <pubDate>Sun, 02 Jun 2019 14:10:50 +0000</pubDate>
      <link>https://forem.com/hagevvashi/don-t-forget-to-give-host-0-0-0-0-to-the-startup-option-of-webpack-dev-server-using-docker-1483</link>
      <guid>https://forem.com/hagevvashi/don-t-forget-to-give-host-0-0-0-0-to-the-startup-option-of-webpack-dev-server-using-docker-1483</guid>
      <description>&lt;h1&gt;
  
  
  You must give "--host 0.0.0.0" option to webpack-dev-server in the container
&lt;/h1&gt;

&lt;h2&gt;
  
  
  tl;dr
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  intro
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  the overview I initially developing
&lt;/h2&gt;

&lt;h3&gt;
  
  
  directory structure
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;react-app
  -- src
    -- components
    -- index.html
    -- index.js
  -- Dockerfile
  -- docker-compose.yml
  -- package.json
  -- webpack.config.js
  -- yarn.lock
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Dockerfile
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM node
WORKDIR /app
COPY package.json /app/package.json
COPY yarn.lock /app/yarn.lock
RUN yarn
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  docker-compose.yml
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: '3'
services:
  react:
    build: .
    ports:
      - 8642:8080
    command: yarn start
    volumes:
      - .:/app
    tty: true

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  package.json
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  ...
  "scripts": {
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1",
    "start": "webpack-dev-server --mode development --open --hot",
    "build": "webpack --config webpack.config.js --mode production",
    "serve": "http-server ./dist"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  webpack.config.js
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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')}`
    })
  ]
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  a problem occurred
&lt;/h2&gt;

&lt;p&gt;ok, I'm Ready, so check the server -- input "localhost:8642" to &lt;br&gt;
the browser. I see the awful error message "Can not access this page".&lt;/p&gt;
&lt;h2&gt;
  
  
  the reason why I cannot see the page
&lt;/h2&gt;

&lt;p&gt;In the first setting, the server was not accessible from outside.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;webpack-dev-server --mode development --open --hot
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  solution
&lt;/h2&gt;

&lt;p&gt;From the official manual, &lt;code&gt;If you want your server to be accessible externally, specify it like this:&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;webpack-dev-server --host 0.0.0.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://webpack.js.org/configuration/dev-server/#devserverhost"&gt;https://webpack.js.org/configuration/dev-server/#devserverhost&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  final setting
&lt;/h2&gt;

&lt;h3&gt;
  
  
  package.json
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt; {
   ...
   "scripts": {
     "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1",
&lt;span class="gd"&gt;-    "start": "webpack-dev-server --mode development --open --hot",
&lt;/span&gt;&lt;span class="gi"&gt;+    "start": "webpack-dev-server --mode development --open --hot --host 0.0.0.0"
&lt;/span&gt;     "build": "webpack --config webpack.config.js --mode production",
     "serve": "http-server ./dist"
   }
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By just adding "--host 0.0.0.0", I can see the page served by webpack-dev-server in the docker container.&lt;/p&gt;

&lt;h2&gt;
  
  
  conclusion
&lt;/h2&gt;

&lt;p&gt;In conclusion, you must give "--host 0.0.0.0" option to webpack-dev-server when you create a server on Docker container.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>webpack</category>
    </item>
  </channel>
</rss>
