简体   繁体   English

在主机窗口上访问在 docker + wsl2 中运行的 mongodb

[英]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.

相关问题 从 WSL2 中执行的 docker 访问 windows 服务器中的 MONGODB 数据库 - Access MONGODB database in windos server from docker executing in WSL2 使用 Mongo Compass 访问 WSL2 中 docker 上运行的 mongo 数据库 - Access mongo database running on docker in WSL2 using Mongo Compass 无法连接到 WSL2 内部 docker 内部的 mongo - Cannot connect to mongo that's inside docker inside WSL2 在 Docker 中连接到 Deno MongoDb 抛出未捕获的 promise(在 wsl2 中) - Connecting to Deno MongoDb in Docker throws uncaught promise (in wsl2) WSL2 的 MongoDB 指南针问题 - MongoDB Compass issue with WSL2 如何在 WSL2 上连接和使用 MongoDB Compass 从 Windows 到 MongoDb 服务器 - How can I connect and use MongoDB Compass from Windows to MongoDb server on WSL2 使用Java连接到Docker内部运行的MongoDB副本集(Windows) - Connect to MongoDB Replica Set running inside Docker with Java (Windows) 如何从运行在同一台机器上的 Docker 内部加载运行在主机中的 MongoDB 中的数据? - How to load data in MongoDB running in host from inside a Docker running on the same machine? 连接到在Docker中运行的mongodb - Connect to mongodb running inside Docker 从主机连接到运行MongoDB的Docker容器 - Connect from host to Docker container running MongoDB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM