1

I have a react app which is using the webpack dev server. The server proxies into another web api for CRUD. I can use it when running locally but when building the container, the app does not connect.

Error

Webpack config

  devServer: {
    contentBase: resolve(__dirname, 'dist'),
    host: 'localhost',
    port: 3001,
    hot: true,
    open: true,
    inline: true,
    proxy: {
      '/api': {
        target: 'http://localhost:4567/streams',
        secure: false,
        pathRewrite: { '^/api': '' },
        changeOrigin: true,
      },
    },
  },

dockerfile

FROM node:12-alpine

WORKDIR /app

COPY ./package*.json ./

RUN npm ci

COPY . ./

docker-compose

version: '3.7'

services:
  web:
    container_name: fm-admin
    restart: always
    build:
      context: .
    ports:
      - '3001:3001'
    command: npm start
    environment:
      - CHOKIDAR_USEPOLLING=true
    stdin_open: true

Further more, when i swapped the host from localhost to 0.0.0.0, I am getting the following error

[HPM] Error occurred while trying to proxy request  from 0.0.0.0:3001 to http://localhost:4567/streams (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)

But the streams api is running.

Hoping i can get some help here.

Yuvi
  • 528
  • 8
  • 18
  • 1
    Did you expect port 5000 or 4567 to work with `3001:3001`? – OneCricketeer Aug 02 '20 at 19:12
  • @OneCricketeer I have realised that the logs was not correct. I have updated it. I am expecting this app to proxy into ```http://localhost:4567/streams``` – Yuvi Aug 02 '20 at 20:51

1 Answers1

0

The whole context is not described very precisely in the question, especially the service listening at 'http://localhost:4567.

I assume that target: 'http://localhost:4567/streams' is executed inside the container that is listening on :3000 in the attempt to proxy to :4567.
If this is true, then it is normal to lack the connectivity. When localhost is used inside the container, the container will try to proxy into itself on :4567.

If you are trying to reach a service that is listening on :4567 on the host machine, you probably need to use the IP address of the docker0 interface, instead of localhost as suggested in this post.

Neo Anderson
  • 5,957
  • 2
  • 12
  • 29
  • 1
    The scenerio is what you have described. Thank you for the link. I will go through it and will try resolving it. – Yuvi Aug 02 '20 at 20:56
  • 1
    most likely, you need to replace `localhost` with `172.17.0.1`, which is the default bridge subnet address. You can simply run `ip a` and look for the address that is assigned to the docker0 network interface – Neo Anderson Aug 02 '20 at 21:04