Reputation: 1542
Both Application are running on Docker Container
When I received this error, I decide investigate:
App listening at http://localhost:3000
(node:1) UnhandledPromiseRejectionWarning: FetchError: request to http://localhost:5000/auth failed, reason: connect ECONNREFUSED 127.0.0.1:5000
at ClientRequest.<anonymous> (/app/node_modules/node-fetch/lib/index.js:1461:11)
at ClientRequest.emit (events.js:314:20)
at Socket.socketErrorListener (_http_client.js:428:9)
at Socket.emit (events.js:314:20)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
(node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:1) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Running docker inspect app1||app2
(command fictitious), both container aren't not in same bridge. I tried to create add network argument in docker run...
docker run -d -p 3000:3000 --network=bridge --name app1 nodejs
docker run -d -p 5000:5000 --network=bridge --name app2 python
I tried to --link
too:
docker run -d -p 3000:3000 --link=app2 --name app1 nodejs
But none of them worked. Is there any way too create this network between my application? Where am I going wrong?
Upvotes: 1
Views: 534
Reputation: 312028
There are a couple of problems here.
First, don't use --link
. It only works on the default bridge network, it's fragile, and it's deprecated.
Second, localhost
refers to "this container". Since your apps are running in two different containers you're not going to be able to reach your services at localhost
.
What you want to do is first create a new user-defined network:
docker network create mynetwork
Launch your containers in that network:
docker run -d -p 3000:3000 --network=mynetwork --name app1 nodejs
docker run -d -p 5000:5000 --network=mynetwork --name app2 python
With these two changes, your containers can refer to each other by name: inside your container app1
you can connect to http://app2:5000
, and in container app2
you can connect to http://app1:3000
(well, assuming that both are presenting http services).
What you really want to do is start using docker-compose
instead of doing this all by hand. Docker-compose will set up a user defined network for you automatically, and saves you from having to type long docker run
command lines.
Upvotes: 1