2
votes

I am trying to host my app through docker, but got this error:

client_1  | Proxy error: Could not proxy request /api/product/B002QYW8LW from localhost:3000 to http://localhost:5000.
client_1  | See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ECONNREFUSED).

With the below settings, it works for me locally. I searched this through google and it seems to be relevent to the proxy settings. Some people was able to set the proxy as an object, but it gaves me an error saying proxy needs to be a string. That's why I decided to ask this question here again. Can someone please help out?

I have deleted irrelavent stuff in package.json to save space.

package.json for server:

{
  "name": "",
  "version": "",
  "description": "",
  "main": "src/server.js",
  "scripts": {
  },
  "repository": {
  },
  "author": "",
  "license": "",
  "bugs": {
  },
  "homepage": ""
  "dependencies": {},
  "devDependencies": {
  }
}

package.json for client:

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
  },
  "scripts": {

  },
  "eslintConfig": {
  },
  "browserslist": {
    "production": [],
    "development": []
  },
  "proxy": "http://localhost:5000"
}

Containers:

CONTAINER ID        IMAGE                    COMMAND             CREATED             STATUS              PORTS                    NAMES
e4b55b165c9a        productfetching_client   "npm start"         24 hours ago        Up 24 hours         0.0.0.0:3000->3000/tcp   productfetching_client_1
10581d34d189        productfetching_server   "node server.js"    24 hours ago        Up 24 hours         0.0.0.0:5000->5000/tcp   productfetching_server_1
1
What do you mean "works locally"? localhost:5000 is referring to some service on the host machine? If so, it needs to be in a container as well, and then the node app must refer to it by service name, not localhost - OneCricketeer
@cricket_007 works locally means it works if it's not running in docker environment. It works when I just run the script npm run start for frontend app, and node server.js for backend - qrb
Okay, so what process on your host runs on port 3000 or 5000 that is not running in the container? Why specifically do you need a proxy? - OneCricketeer
@qrb, when you run anything in docker container, the localhost refers to the container itself . So if you have A running as a container and B running as container in same server, localhost in A won't refer to B , it refers to container of A. You can use container name or service name instead. - Jimmy
@Jimmy thanks for your answer. So in this case, since my front end is trying to call backend, I should change the proxy from localhost:5000 to productfetching_server:5000 instead? - qrb

1 Answers

1
votes

Use the container name registered in docker-compose file as said in a comment or http://0.0.0.0:PORT instead which point to the local container network address, instead of localhost or 127.0.0.1 which point to your regular machine network.