Reputation: 6946
I am running mongodb instance using docker-compose as docker container, which is running in a WSL2 environment on windows 10 machine.
From my host windows 10 machine, i am able to connect to nodejs container http://localhost:3001/api/v1
, also using mongo-compass I can't connect to mongodb instance,
The error I get is connect ECONNREFUSED 127.0.0.1:27017
.
Please help, how can i connect to mongodb instance from host windows machine.
Upvotes: 1
Views: 2139
Reputation: 1764
If you want to connect from your host machine to one of the docker container's ports you need to make sure this port is exposed to the host.
From what I can see on your screenshot, your containers configured is such a way, that only node
container exposes 3001
port, therefore you can reach it from your host via localhost.
The problem with mongo
occurs because your docker-compose configuration doesn't expose mongodb container's (named mongo
on your screenshot) port 27017
to the host machine.
So to fix that, you need to set ports
. As an example:
...
services:
...
mongo:
...
ports:
- "27017"
...
Please note, you need to make sure 27017 are not used by any other service running on your host before exposing it. IF this port is busy and you don't want to stop the service, you can simply use another port on your host:
...
mongo:
...
ports:
- "27018:27017"
...
More about docker-compose configs is here.
Upvotes: 5