[英]access mongodb running inside docker + wsl2, on Host windows
I am running mongodb instance using docker-compose as docker container, which is running in a WSL2 environment on windows 10 machine.我正在使用 docker-compose 作为 docker 容器运行 mongodb 实例,该容器在 Windows 10 机器上的 WSL2 环境中运行。
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,从我的主机 Windows 10 机器,我能够连接到 nodejs 容器
http://localhost:3001/api/v1
,也使用 mongo-compass 我无法连接到 mongodb 实例,
The error I get is connect ECONNREFUSED 127.0.0.1:27017
.我得到的错误是
connect ECONNREFUSED 127.0.0.1:27017
。
Please help, how can i connect to mongodb instance from host windows machine.请帮忙,我如何从主机 Windows 机器连接到 mongodb 实例。
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.如果你想从你的主机连接到 docker 容器的端口之一,你需要确保这个端口暴露给主机。
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.从我在您的屏幕截图中可以看到,您配置的容器是这样的,即只有
node
容器公开3001
端口,因此您可以通过 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. mongo
的问题是因为您的 docker-compose 配置没有将 mongodb 容器(在您的屏幕截图上命名为mongo
)端口27017
暴露给主机。
So to fix that, you need to set ports
.所以要解决这个问题,你需要设置
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.请注意,在暴露 27017 之前,您需要确保主机上运行的任何其他服务都没有使用 27017。 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 .关于 docker-compose 配置的更多信息在这里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.